通过 Ajax Laravel DataTable 返回 html 内容(使用 yajrabox 包)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42032068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Return html content via Ajax Laravel DataTable (using yajrabox package)
提问by Latheesan
I am using this package https://datatables.yajrabox.com/starterto implement ajax datatable in my laravel application.
我正在使用这个包https://datatables.yajrabox.com/starter在我的 Laravel 应用程序中实现 ajax 数据表。
In my controller class, I have the following method to return the data for the datatables like this:
在我的控制器类中,我有以下方法来返回数据表的数据,如下所示:
function ajaxList()
{
// Load users with users
$users = User::with('group', 'organisation');
// Finished
return Datatables::eloquent($users)
->editColumn('is_admin', function(User $user) {
return '<i class="fa fa-'. ($user->is_admin ? 'check' : 'times') .'" aria-hidden="true"></i>';
})
->make(true);
}
On the view, I render the table and initiate the ajax request like this:
在视图上,我呈现表并像这样启动 ajax 请求:
<table id="users-table" class="table table-hover table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>User ID</th>
<th>Is Admin?</th>
<th>First Name</th>
<th>Last Name</th>
<th>Created At</th>
<th>Updated At</th>
<th>Action</th>
</tr>
</thead>
</table>
<script>
$('#users-table').DataTable({
processing: true,
serverSide: true,
ajax: '/users/ajaxList',
columns: [
{data: 'id', searchable: false },
{data: 'is_admin', searchable: false },
{data: 'first_name'},
{data: 'last_name'},
{data: 'created_at', searchable: false },
{data: 'updated_at', searchable: false },
{data: 'action', searchable: false, orderable: false }
]
});
</script>
When this renders, the "is_admin" column appears to show the raw html rather than rendering the font awesome icon like this:
渲染时,“is_admin”列似乎显示原始 html,而不是像这样渲染字体真棒图标:
Any ideas how to fix this? I tried returning the column data like this also:
任何想法如何解决这一问题?我也尝试返回这样的列数据:
return '{!! <i class="fa fa-'. ($user->is_admin ? 'check' : 'times') .'" aria-hidden="true"></i> !!}';
This didn't help either.
这也没有帮助。
回答by Latheesan
Okay, the issue appears to be a undocumented breaking change in the new 7.x version of the library: https://github.com/yajra/laravel-datatables/issues/949
好的,该问题似乎是库的新 7.x 版本中未记录的重大更改:https: //github.com/yajra/laravel-datatables/issues/949
In my case, I've fixed it like this:
就我而言,我已将其修复如下:
function ajaxList()
{
// Load users with users
$users = User::with('group', 'organisation');
// Finished
return Datatables::eloquent($users)
->editColumn('is_admin', function(User $user) {
return '<i class="fa fa-'. ($user->is_admin ? 'check' : 'times') .'" aria-hidden="true"></i>';
})
->rawColumns(['is_admin'])
->make(true);
}
回答by Mr.Senhaji
I'm working on a project right now and face the same problem and fix it using ->escapeColumns([])
我现在正在做一个项目并面临同样的问题并使用修复它 ->escapeColumns([])
$users = DB::table('users')->select('*');
return datatables()->of($users)
->removeColumn('password')
->editColumn('created_at', function ($user) {
return date('d-m-Y', strtotime($user->created_at));
})
->editColumn('role_id', function ($user) {
return Role::findOrFail( $user->role_id)->name;
})
->editColumn('is_active', function ($user) {
return ($user->is_active == 1) ? 'active' : 'suspended';
})
->addColumn('actions', function ($user) {
return '<a href="#edit-'.$user->id.'" class="btn btn-xs btn-primary"><i class="glyphicon glyphicon-edit"></i> Edit</a>';
})
->escapeColumns([])
->make(true);
Source: yajra/laravel-datatables
回答by king
Add another column labeled rawcolumn
with the name you assign to the individual buttons as indicated below to fix the issue:
添加标rawcolumn
有您分配给各个按钮的名称的另一列,如下所示以解决问题:
Route::get('user-data', function() {
$model = App\User::query();
return DataTables::eloquent($model)
->addColumn('link', '<a href="#">Html Column</a>')
->addColumn('action', 'path.to.view')
->rawColumns(['link', 'action'])
->toJson();
});
This rawColumns(['link', 'action'])
is what does the magic. Your last statement might end with ->make(true)
, the rawColumn
is what does the magic.
这rawColumns(['link', 'action'])
就是魔术的作用。您的最后一条语句可能以 结束->make(true)
,这rawColumn
就是魔术的作用。
回答by Rikard Olsson
I did a project once with mostly the exact same code (using a link instead). Don't ask me why but I use Datatables::of
instead of Datatables::eloquent
. Yet I have no idea of why, but it do work:
我用几乎完全相同的代码做了一个项目(使用链接代替)。不要问我为什么,但我使用Datatables::of
代替Datatables::eloquent
. 然而我不知道为什么,但它确实有效:
$articles = ArticlesList::all();
return Datatables::of($articles)
->editColumn('name', function($article) {
return '<a href="/article/'.$article->id.'/show">'.$article->name.'</a>';
})
->make(true);