【发布时间】:2017-06-21 06:52:35
【问题描述】:
我正在使用这个包https://datatables.yajrabox.com/starter 在我的 laravel 应用程序中实现 ajax 数据表。
在我的控制器类中,我有以下方法来返回数据表的数据,如下所示:
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);
}
在视图上,我渲染表格并像这样发起 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>
渲染时,“is_admin”列似乎显示原始 html,而不是像这样渲染字体真棒图标:
任何想法如何解决这个问题?我也尝试像这样返回列数据:
return '{!! <i class="fa fa-'. ($user->is_admin ? 'check' : 'times') .'" aria-hidden="true"></i> !!}';
这也没有帮助。
【问题讨论】:
-
我曾经做过一个项目,其中大部分代码都是相同的(使用链接代替)。不要问我为什么,但我使用
of而不是eloquent。我不知道为什么,但它有效:return Datatables::of($articles) ->editColumn('name', function($article) { return '<a href="/article/'.$article->id.'/show">'.$article->name.'</a>'; }) ->make(true); -
我会将它添加为答案,以便更容易看到
标签: ajax laravel laravel-5 datatable