【发布时间】:2021-09-12 07:40:49
【问题描述】:
我正在为 Laravel 8 应用程序使用 Yajra-DataTable。除了一个小问题外,所有 Yajra DT 功能都可以正常工作。我的第一列是 DT_RowIndex 并已添加为 ->添加索引列()。这些是行序列号(1、2、3 ...)。在此列上自然排序是错误的。但是,当其他列被排序时,这些序列号也会被排序。我希望此列在表格排序时保持不变。任何人都可以为这个问题提出解决方案。下面是我正在使用的代码。
控制器:
public function index(Request $request)
{
if ($request->ajax())
{
$hospitals = Hospital::join('District', 'District.dt_id', '=', 'Hospital.dt_id')
->join('Tehsil', 'Tehsil.teh_id', '=', 'Hospital.teh_id')
->orderBy('District.dt_name', 'asc')->orderBy('Tehsil.teh_name', 'asc')
->get(['Hospital.hl_id', 'Hospital.hl_name','Tehsil.teh_name', 'District.dt_name', 'Hospital.assessment_status']);
return Datatables::of($hospitals)
->addIndexColumn()
->addColumn('action', function($row){
$btn = '<a href="/hospital/'.$row->hl_id.'" class="btn btn-info btn-sm text-center">View</a> ';
$btn = $btn.'<a href="/hospital/'.$row->hl_id.'/edit" class="edit btn btn-primary btn-sm text-center">Edit</a> ';
$btn = $btn.'<a href="'.route('HospitalDeleteRoute', $row->hl_id).'" class="btn btn-danger btn-sm" onclick="return confirm(\'Are you sure you want to delete the hospital: '.$row->hl_name.'\')">Delete</a>';
return $btn;
})
->rawColumns(['action'])
->make(true);
}
return view('hospitals.index');
}
索引 php 文件
<table class="table table-sm table-striped table-bordered hospital-DT" id="tbl_hosp">
<thead class="thead-dark" align="center">
<tr class="th_sort_color">
<th>S/No</th>
<th>Hospital Name</th>
<th>Tehsil</th>
<th>District</th>
<th>Assessment Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
脚本:
$(function () {
var table = $('.hospital-DT').DataTable({
processing: true,
serverSide: false,
searchable: true,
orderable:true,
order: [],
lengthMenu: [[10, 25, 50, 100, 500], [10, 25, 50, 100, 500]],
ajax: "{{ route('hospital.index') }}",
'columns': [
{data: 'DT_RowIndex', name: '', orderable: false, searchable: false},
{data: 'hl_name', name: 'name'},
{data: 'dt_name', name: 'name'},
{data: 'teh_name', name: 'name'},
{data: 'assessment_status', name: 'name'},
{data: 'action', name: 'action', orderable: false, searchable: false}
],
// 'columnDefs': [ {
// 'targets': [0,5], /* column index */
// 'orderable': false, /* true or false */
// }]
});
});
【问题讨论】: