gpt4 book ai didi

ajax - 通过 Ajax Laravel DataTable 返回 html 内容(使用 yajrabox 包)

转载 作者:行者123 更新时间:2023-12-02 22:02:43 25 4
gpt4 key购买 nike

我正在使用这个包https://datatables.yajrabox.com/starter在我的 laravel 应用程序中实现 ajax 数据表。

在我的 Controller 类中,我有以下方法来返回数据表的数据,如下所示:

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);
}

在 View 上,我渲染表格并启动 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,而不是渲染 Font Awesome 图标,如下所示:

enter image description here

有什么想法可以解决这个问题吗?我也尝试返回这样的列数据:

return '{!! <i class="fa fa-'. ($user->is_admin ? 'check' : 'times') .'" aria-hidden="true"></i> !!}';

这也没有帮助。

最佳答案

好吧,这个问题似乎是新的 7.x 版本库中的一个未记录的重大更改:https://github.com/yajra/laravel-datatables/issues/949

就我而言,我是这样修复的:

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);
}

关于ajax - 通过 Ajax Laravel DataTable 返回 html 内容(使用 yajrabox 包),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42032068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com