如何根据第二个数据进行排序?目前的排序仅基于ID。但在我的表格中,我添加了一个徽章计数,我想按照它来排序。
这是我的控制器
return DataTables::of($users)
->addIndexColumn()
->addColumn('Inventories', function ($row) {
if($row->status != 'Dumped2') {
return '<a href="'.route('admin.clients.show-client-inventories2', $row->id).'" class="btn btn-info btn-sm">'.__("Inventories").' <span class="right badge badge-success" title="Total Lost">'.(abs($row->inv_total_lost_qty) ?? 'N/A').'</span> </a>';
}
})
->addColumn('Returns', function ($row) {
if($row->status != 'Dumped2') {
return ' <a href="'.route('admin.clients.show-client-returns', $row->id) .'" class="btn btn-info btn-sm">'.__('Returns').' <span class="right badge badge-warning" title="Total Returns">'.(abs($row->overall_returns_count) ?? 'N/A').'</span> </a>';
}
$row->status->orderBy(abs($row->overall_returns_count));
})
->addColumn('inbound_shipments', function ($row) {
if($row->status != 'Dumped2') {
return '<a href="'. route('admin.clients.show-client-inbound-shipments', $row->id).'" class="btn btn-info btn-sm">'. __('Inbound Shipments').'<span class="right badge badge-danger" title="Total Overcharged Fees">'. abs($row->shipment_quantity_diff).'</span> </a>';
}
})
->addColumn('overcharged', function ($row) {
if($row->status != 'Dumped2') {
return '<a href="'. route('admin.clients.show-client-overcharged-fee', $row->id).'" class="btn btn-info btn-sm">'.__('Overcharged Fee') .' <span class="right badge badge-primary" title="Total Overcharged Fees">'.(abs($row->overall_overcharged_fees_count) ?? 'N/A').'</span> </a>';
}
})
->addColumn('ALL', function ($row) {
if($row->status != 'Dumped2') {
$arr = array(abs($row->overall_overcharged_fees_count), abs($row->overall_returns_count), abs($row->inv_total_lost_qty),abs($row->shipment_quantity_diff));
return array_sum($arr);
}
})
->addColumn('credentials', function ($row) {
if($row->status != 'Dumped2') {
return '<button onclick="set_credentials(this)" class="btn btn-danger btn-sm"> '.__("Set Credentials").'</button>';
}
})
->addColumn('reim', function ($row) {
if($row->status != 'Dumped2') {
return '<a href="'. route('admin.clients.show-client-reimbursements', $row->id).'" class="btn btn-danger btn-sm">'.__('Reimbursements') .'</a>';
}
})
->rawColumns(['Inventories','action','Returns','inbound_shipments','overcharged','ALL','credentials','reim'])
->make(true);
}
return view('admin.clients.index', compact('users'));
我想要排序的列不是
$row->id
,我想要的是第二个,比如abs($row->overall_returns_count)
。在我的Blade代码中,我写了这样的东西:
//Update start
{width: "10%", data: 'ALL', name: 'ALL', orderable: true, searchable: false},
{width: "10%", data: 'Inventories', name: 'Inventories', orderable: true, searchable: false},
{width: "10%", data: 'Returns', name: 'Returns', orderable: true, searchable: false},
{width: "15%", data: 'inbound_shipments', name: 'Inbound Shipments', orderable: true, searchable: false},
{width: "10%", data: 'overcharged', name: 'Overchared Fee', orderable: true, searchable: false},
如您所见,如果我点击标题,它不会对徽章进行排序。相反,它会对id
进行排序,您可以在悬停按钮时在左下角看到(从2开始的id)。
系统详细信息 操作系统 Windows 10
PHP 版本 7.2
Laravel 版本 6.0
Laravel-Datatables 版本 ^9.6
$users
是一个查询构建器还是一个集合? - Muhammad Dyas Yaskur添加的列被认为是计算列而不是数据库的一部分。因此,这些列的搜索/排序将被禁用。如果需要它们,请改用editColumn api。
如果您使用官方文档中的按多列排序的示例,可能会更容易些。 - Daniel Protopopov