laravel 如何在laravel数据表中添加行号或序列号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42108615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add row number or Serial no in laravel datatable
提问by Majbah Habib
This is the bill_info table, for which i need to serialized row no like 1 2 . . . . . . . . . . . . .n
这是 bill_info 表,我需要序列化第 1 2 行。. . . . . . . . . . . .n
There is data list returned, how I can get serial_nocustom field in datatable list view.
返回了数据列表,如何在数据表列表视图中获取serial_no自定义字段。
$data = BillInfo::get(['bill_info.*']);
return Datatables::of($data)
->removeColumn('id')
->make(true);
回答by Abhinav Saraswat
If you are using yajra laravel datatables
如果您使用的是 yajra laravel 数据表
Just add ->addIndexColumn()
只需添加->addIndexColumn()
return DataTables::of($data)
->addIndexColumn()
->make(true);
In your javascript, you can set the first row as a serial number like this
在您的javascript中,您可以将第一行设置为这样的序列号
columns: [
{ data: 'DT_Row_Index', name: 'DT_Row_Index' },
{ data: 'name', name: 'name' },
{ data: 'action', name: 'action' }
]
use DT_RowIndex instead of DT_Row_Index for latest yajra datatable version
对于最新的 yajra 数据表版本,使用 DT_RowIndex 而不是 DT_Row_Index
回答by Ishrat Sharmin
Set the variable rownum at the beginning of your query. Then set the increment process in your query.
在查询开始时设置变量 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);
Here you can get rownum as serial no of the given records [1 . . . 8].
在这里,您可以获得 rownum 作为给定记录的序列号 [1 。. . 8]。
回答by Mirza Alim M
In Laravel Yajra Datatablesv9.x as ServiceImplementation, I add this in getColumnsfunction and works well in sorting, searching, and page changing.
在 Laravel Yajra Datatablesv9.x 作为服务实现中,我将它添加到getColumns函数中,并且在排序、搜索和页面更改方面效果很好。
'id' => ['title' => 'N.', 'orderable' => false, 'searchable' => false, 'render' => function() {
return 'function(data,type,fullData,meta){return meta.settings._iDisplayStart+meta.row+1;}';
}],
回答by Thanushka
When using Yajra Laravel datatables
使用 Yajra Laravel 数据表时
return DataTables::of($result)
->addIndexColumn()
->make(true);
"columns": [
{
"data": 'DT_RowIndex',
orderable: false,
searchable: false
},
]