Laravel 5.3-有条件图像,带有数据表



我有一个简单的表显示了用户表,在该表中,我添加了一个'Active'字段,为活动添加了一个值为1的值,为intimative。

为0。

我有以下视图(jQuery和DataTables并在应用程序视图中拉动):

@extends('layouts.app')
@section('content')
<div class="panel panel-default" style="padding-left: 2em; padding-right: 2em">
  <div class="panel-heading">Users</div>
  <div class="panel-body"> 
     <table class="datatable" id="dtable" width="90%" align="center">
        <thead>
          <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Email</th>
            <th>Created at</th>
            <th>active</th>
          </tr>
        </thead>
        <tbody>
        </tbody>
      </table>
  </div>
</div>
  <script>
    $(document).ready(function(){
      $('#dtable').DataTable({
            processing: true,
            serverSide: true,
            ajax: '{{ route('users.serverSide') }}'
        });
    });
  </script>
@endsection

在Web.php中,我有以下方法来获取数据:

Route::get('/users/serverSide', [
    'as'   => 'users.serverSide',
    'uses' => function () {
        $users = AppUser::select(['id', 'name', 'email', 'created_at', 'active']);
        return Datatables::of($users)->make();
    }
]);

所有这些都可以正常工作,但是我想用tick或cross在活动表中替换1或0,使用站点图标或fontawesome。

有人指向正确的方向吗?我知道我可以在mySQL中设置一个视图,但它们往往很慢。

您可以使用columns.render选项为各种操作生成替代内容。

例如:

$('#dtable').DataTable({
    processing: true,
    serverSide: true,
    ajax: '{{ route('users.serverSide') }}',
    columns: [
       { data: 'id' },
       { data: 'name' },
       { data: 'email' },
       { data: 'created_at' },
       { 
          data: 'active',
          render: function(data, type, row, meta){
             if(type === 'display'){
                if(data == 1){
                   data = '<span class="fa fa-check"></span>';
                } 
             }
             return data;
          }
       }
    ]
});

最新更新