【问题标题】:How to add row number or Serial no in laravel datatable如何在 laravel 数据表中添加行号或序列号
【发布时间】:2017-02-08 08:57:35
【问题描述】:

这是 bill_info 表,我需要像 1 2 这样序列化行号。 . . . . . . . . . . . .n

有数据列表返回,如何在数据表列表视图中获取serial_no自定义字段。

    $data = BillInfo::get(['bill_info.*']);

    return Datatables::of($data)
                    ->removeColumn('id')
                    ->make(true);

【问题讨论】:

  • 我在您的数据库中没有看到任何 serial_no 字段。使用身份证。还是你想要invoice_no
  • serial_no 字段在数据库中不存在,但它将表示给定列表的记录号。有 8 条记录可用,因此序列号将从 1 到 8 开始
  • 为什么不使用 id 字段?或者只是在循环中计算它并打印每次迭代。
  • 请进一步解释您的问题,我相信它可以很快很好地解决。
  • id 字段值从 168 到 175 开始记录,但我需要从 1 到 8 记录给定记录

标签: laravel serialization datatable numbers row


【解决方案1】:

如果你使用的是 yajra laravel 数据表

只需添加 ->addIndexColumn()

return DataTables::of($data)
            ->addIndexColumn()
            ->make(true);

在您的 javascript 中,您可以将第一行设置为这样的序列号

columns: [
            { data: 'DT_RowIndex', name: 'DT_RowIndex', orderable: false, searchable: false },
            { data: 'name', name: 'name' },
            { data: 'action', name: 'action' }
        ]

对于旧的 yajra 数据表版本,使用 DT_Row_Index 而不是 DT_RowIndex

【讨论】:

  • 在javascript中,使用DT_RowIndex而不是DT_Row_Index
  • 在 yajra v.9 中,如果我们将 orderable 和 searchable 添加为 false ``` { data: 'DT_RowIndex', name: 'DT_RowIndex' , orderable: false, searchable: false} ,这将正常工作, ```
【解决方案2】:

使用 Yajra Laravel 数据表时

return DataTables::of($result)
            ->addIndexColumn()
            ->make(true);

"columns": [
                {
                    "data": 'DT_RowIndex',
                    orderable: false, 
                    searchable: false
                },
]

【讨论】:

    【解决方案3】:

    在查询的开头设置变量 rownum。然后在查询中设置增量过程。

    DB::statement(DB::raw('set @rownum=0'));
    
    $data = BillInfo::get(['bill_info.*', 
                        DB::raw('@rownum  := @rownum  + 1 AS rownum')]);
    
    return Datatables::of($data)
                    ->removeColumn('id')
                    ->make(true);
    

    在这里,您可以获得 rownum 作为给定记录的序列号 [1 . . . 8]。

    【讨论】:

    • 我在查询中使用 order by,但这不适用于 order by,例如,如果我有 3 条记录,那么它将给出 4,5,6 作为 rownum,如果我届时将删除 order它给出了 1,2,3
    【解决方案4】:

    在 Laravel Yajra Datatables v9.x 作为 Service 实现中,我在 getColumns 函数中添加了这个,并且在排序、搜索和换页。

    'id' => ['title' => 'N.', 'orderable' => false, 'searchable' => false, 'render' => function() {
                return 'function(data,type,fullData,meta){return meta.settings._iDisplayStart+meta.row+1;}';
            }],
    

    【讨论】:

      【解决方案5】:

      你可以看到我的代码:

      <table id="datatable" class="table table-bordered table-striped">
                                  <thead>
                                  <tr>
                                      <th>Sl No</th>
                                      <th>Invoice No</th>
                                      <th>Customer</th>
                                      <th>Total Price</th>
                                      <th>Paid</th>
                                      <th>Due</th>
                                      <th>Discount</th>
                                      <th>Action</th>
                                  </tr>
                                  </thead>
                                  <tbody>
      
                                  </tbody>
                              </table>
      

      还有脚本部分:

      <script type="text/javascript">
              $(function() {
                  var i = 1;
                  var table = $('#datatable').DataTable({
                      processing: true,
                      serverSide: true,
                      ajax: "{{ route('collections') }}",
                      columns: [{
                          "render": function() {
                              return i++;
                          }
                      },
                          {
                              data: 'issue_no',
                              name: 'issue_no'
                          },
                          {
                              data: 'customer_name',
                              name: 'customer_name'
                          },
                          {
                              data: 'total_order_price',
                              name: 'total_order_price'
                          },
                          {
                              data: 'paid_amount',
                              name: 'paid_amount'
                          },
                          {
                              data: 'due_amount',
                              name: 'due_amount'
                          },
                          {
                              data: 'discount_amount',
                              name: 'discount_amount'
                          },
                          {
                              data: 'action',
                              name: 'action',
                              orderable: false,
                              searchable: false
                          }
                      ],
                      createdRow: function ( row, data, index ) {
                          if (data['due_amount'] > 0) {
                              $('td', row).eq(4).css('background-color', '#f4511e','color','#ffffff');
                              $('td', row).eq(4).css('color','#ffffff');
                          } else {
      
                          }
                          $('td', row).eq(3).addClass('text-right');
                          $('td', row).eq(4).addClass('text-right');
                          $('td', row).eq(5).addClass('text-right');
                          $('td', row).eq(6).addClass('text-right');
                      }
                  });
      
              });
          </script>
      

      在渲染部分,可以添加自增字段

      {
        "render": function() {
            return i++;
           }
      

      },

      【讨论】:

        猜你喜欢
        • 2021-01-08
        • 1970-01-01
        • 2020-08-22
        • 2011-10-15
        • 2013-05-26
        • 2019-03-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多