加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 02:54:41 所属栏目:百科 来源:网络整理
导读:我正在使用这个包 https://datatables.yajrabox.com/starter在我的laravel应用程序中实现ajax数据表. 在我的控制器类中,我有以下方法来返回数据表的数据,如下所示: function ajaxList(){ // Load users with users $users = User::with('group','organisati
我正在使用这个包 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',{data: 'first_name'},{data: 'last_name'},{data: 'created_at',{data: 'updated_at',{data: 'action',searchable: false,orderable: false }
        ]
    });
</script>

当这个呈现时,“is_admin”列显示为显示原始html而不是像这样渲染字体真棒图标:

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',function(User $user) {
            return '<i class="fa fa-'. ($user->is_admin ? 'check' : 'times') .'" aria-hidden="true"></i>';
        })
        ->rawColumns(['is_admin'])
        ->make(true);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读