微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我正在使用这个包 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);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐