如何在laravel数据表中添加行号或序列号

xdnvmnnf  于 2023-02-17  发布在  其他
关注(0)|答案(6)|浏览(257)

这是bill_info表,对于该表,我需要序列化第12 ............ n行

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

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

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

4ngedf3f1#

如果使用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

h22fl7wq

h22fl7wq2#

使用Yajra Laravel数据表时

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

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

q0qdq0h23#

在查询开始时设置变量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]的序列号。

wribegjk

wribegjk4#

在LaravelYajra Datatablesv9.x asServiceImplementation中,我将其添加到getColumns函数中,在排序、搜索和页面更改方面运行良好。

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

siv3szwd5#

你可以看到我的代码:

<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++;
     }

},

pes8fvy9

pes8fvy96#

简单的使用渲染函数脚本在yajra数据表,这是我的代码:

protected function getColumns()
{
    return [
            Column::make('row_number')
                    ->title('#')
                    ->render('meta.row + meta.settings._iDisplayStart + 1;')
                    ->width(50)
                    ->orderable(false),
           ];
}

相关问题