【问题标题】:Laravel DataTables does not render HTMLLaravel DataTables 不呈现 HTML
【发布时间】:2017-01-27 14:20:32
【问题描述】:

我正在使用 Laravel Datatables 7,但我的表格没有呈现 HTML 代码。它以前渲染 HTML,但是当我将新的 Laravel DataTables 从 6 更新到 7 时,它停止在列中渲染 HTML。 http://prntscr.com/e11n84

这是 Laravel DataTables 6 - http://prntscr.com/e11ph0

$(function() {
    $('#users-table').DataTable({
        processing: true,
        serverSide: true,
        ajax: {
            url: '{{ route("admin.access.user.get") }}',
            type: 'post',
            data: {status: 1, trashed: false}
        },
        columns: [
            {data: 'id', name: '{{config('access.users_table')}}.id'},
            {data: 'name', name: '{{config('access.users_table')}}.name', render: $.fn.dataTable.render.text()},
            {data: 'email', name: '{{config('access.users_table')}}.email', render: $.fn.dataTable.render.text()},
            {data: 'confirmed', name: '{{config('access.users_table')}}.confirmed'},
            {data: 'roles', name: '{{config('access.roles_table')}}.name', sortable: false},
            {data: 'created_at', name: '{{config('access.users_table')}}.created_at'},
            {data: 'updated_at', name: '{{config('access.users_table')}}.updated_at'},
            {data: 'actions', name: 'actions', searchable: false, sortable: false}
        ],
        order: [[0, "asc"]],
        searchDelay: 500
    });
});

【问题讨论】:

    标签: php laravel datatable


    【解决方案1】:

    添加带有 html 的列,如其他答案所示,然后在 ->toJson() 之前添加此 ->rawColumns(['html_column', 'another_html_column']) 以呈现服务器发送的所有 html 列

    【讨论】:

      【解决方案2】:

      尝试使用 toJson() 方法从控制器以 JSON 格式提供数据。

      $data= User::all();
      return datatables()->of($data)
             ->addColumn('action', function ($row) {
                $html = '<a href="/users/'.$row->id.'">Edit</a> ';
                $html .= '<button data-rowid="'.$row->id.'">Del</button>';
                return $html;
              })->toJson();
      

      参考:https://laravelarticle.com/laravel-yajra-datatables

      【讨论】:

        【解决方案3】:
              //im also using yajra data tables..
              //it's easy to render html from controller...
        
              example: 
              $query = Appointment::all();
              $table = Datatables::of($query);
              $table->editColumn('user_id', function ($row) {
                    return $row->user_id ? '<span style="color: green;">ACCEPTED</span>' : 
                                           '<span style="color: red;">PENDING</span>';           
                    
                });
              //then you need to add the column you want to render html
               $table->rawColumns(['user_id']); 
        

        【讨论】:

          猜你喜欢
          • 2017-01-05
          • 1970-01-01
          • 1970-01-01
          • 2017-06-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多