【问题标题】:Laravel datatable sort not working how to use on joinLaravel 数据表排序不起作用如何在连接中使用
【发布时间】:2017-04-12 10:51:14
【问题描述】:

我正在使用 Laravel 数据表,排序不起作用,谁能帮帮我。

控制器

Table::select(array( DB::raw('table2.con_title'),
    DB::raw('........
Datatables::of(----)->make();

查看

.dataTable({ “bProcessing”:是的, “bServerSide”:是的, "sAjaxSource": ajaxurl,

"aoColumnDefs": [ { mData:'table2.con_title' , aTargets: [0]},.......

错误 DataTables 警告(表 id = '-----'):从第 0 行的数据源请求未知参数 'table2.con_title'

【问题讨论】:

  • AJAX 响应是什么样的?
  • JSON {"sEcho":1,"iTotalRecords":33,"iTotalDisplayRecords":33,"aaData":[["-----","------ --------","
  • 我希望 DB::raw( 一定是问题制造者,如何使用 DB::raw( 进行排序
  • table2.con_title 不是您数据中的字段。 (我可以在您粘贴的数据中看到。)
  • 所以我是否需要将其作为 mData: 0

标签: laravel yajra-datatable


【解决方案1】:

您需要确保您的表格列正确映射到您的数据。

https://datatables.net/manual/tech-notes/4

【讨论】:

  • 我添加了 { mData: 0 , aTargets: [0]} 我没有收到错误,但排序不起作用。
  • 我没有使用 true (->make(true)) 所以我的数据响应将作为索引,现在我确定我使用 { mData: 0 , aTargets: [0]} 是正确的, 但排序不起作用。
【解决方案2】:

最近我在使用 Laravel 数据表时遇到了类似的情况,数据正在加载到列中,但排序不起作用,我的数据表正在从多个数据库 (DB) 表中加载数据。以下是我的发现:

  • 确保按照文档https://laravel.com/docs/5.7/eloquent-relationships 设置您的数据库表关系
  • 如果您使用的是 DB::raw($your_sql) - 请确保您在数据表列配置中引用了正确的 DB 列名。例如:

        $sql_query = "
        SELECT
           id AS primary_key,
           first_name,
           last_name
        FROM
           Contacts           
          ";
    
         $data = collect(DB::select(DB::raw($sql_query)));
         $list = Datatables::of($data);
         $list->make(true); 
    
  • 在你的刀片文件中,像这样进行数据表列配置

            <table id="name-list">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>First Name</th>
                    <th>Last Name</th>
                </tr>
            </thead>
            <tbody>
    
            </tbody>
            </table>
    
         $('#name-list').dataTable({
             "processing": true,
             "serverSide": true,
             "ajax": "{{route('path_to_your_server_code')}}",
             "columns": [
                   {data: 'primary_key', name: 'primary_key', orderable: true, searchable: true, visible: true},
                   {data: 'first_name', name: 'first_name', orderable: true, searchable: true, visible: true},
                   {data: 'last_name', name: 'last_name', orderable: true, searchable: true, visible: true}],
             "order":[[1, 'desc']]
        });
    
  • 如果您在 SQL 中使用 Eloquent-Relationships 并急切加载多个关系(即多个 DB 表),请确保您通过 Eloquent-Relationships 引用 DB 列,例如关系.column_name。您的数据表列配置将如下所示:

         //column configuration
         {data: 'some_relation.db_column', name: 'some_relation.db_column', orderable: true, searchable: true, visible: true} 
    
         //complete example code
         $('#name-list').dataTable({
         "processing": true,
         "serverSide": true,
         "ajax": "{{route('path_to_your_server_code')}}",
         "columns": [
               {data: 'primary_key', name: 'primary_key', orderable: true, searchable: true, visible: true},
               {data: 'first_name', name: 'first_name', orderable: true, searchable: true, visible: true},
               {data: 'last_name', name: 'last_name', orderable: true, searchable: true, visible: true},
               {data: 'some_relation.db_column', name: 'some_relation.db_column', orderable: true, searchable: true, visible: true}],
         "order":[[1, 'desc']]
    });
    

我已经尝试了以上两种方法,并且排序在这两种情况下都对我有用,即使用 DB::raw($your_sql) 和 Eloquent-Relationships。

【讨论】:

    猜你喜欢
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    • 2017-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多