Laravel 数据表:如何使用第二个数据对列进行排序

Laravel Datatables: How to sort column with the second data

如何根据第二条数据进行排序?排序仅适用于 ID。但是在我的 table 中,我添加了徽章计数,这就是我想要排序的依据。

这是我的控制器

    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},

如您所见,如果我单击 header,它不会对徽章进行排序。相反,它对 id 进行排序,正如您在我将鼠标悬停在按钮上时在左下角看到的那样。 (id 从 2 开始)

系统详细信息 操作系统 Windows 10

PHP 版本 7.2

Laravel 版本 6.0

Laravel-Datatables版本^9.6

您的数据来源是什么?如果您的数据源是 laravel collection,您应该在使用 render: function (data, action, row) 初始化数据表时在 JavaScript 上呈现。应该是这样的:

{
    width: "10%", data: 'overall_returns_count', name: 'overall_returns_count', orderable: true, searchable: false,
    render: function (data, action, row) {
        if (row.status != 'Dumped2') {
            return ' <a href="/admin/client/' + row.id + '" class="btn btn-info btn-sm">' +
                'Returns <span class="right badge badge-warning" title="Total Returns">' + (Math.abs(data)) +
                '</span> </a>';
        }
    },
},

如果您的数据源是查询生成器,您可以使用 PHP 呈现,但您应该使用 editColumn 而不是 addColumn。示例:

    ->editColumn('overall_returns_count', 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));
    })

然后在 js 列中:

{width: "10%", data: 'overall_returns_count', name: 'overall_returns_count', orderable: true, searchable: false},

PHP 渲染仅在您使用 eloquent/DB 查询构建器作为数据源时有效,但 JS 渲染 适用于所有数据源,包括 Laravel Collection 和 eloquent builder.

仅供参考,Yajra Datatable 支持多个数据源,包括 Collection 和查询生成器。有时如果你使用 Collection 作为数据源会降低性能并且一些功能看起来很乱,所以我建议你使用 Query Builder 作为数据源。

这将 return Collection :

$users = User::withCount('overall_returns')->get(); 

这将 return 查询生成器:

$users = User::withCount('overall_returns'); 

尝试在行中添加数据顺序属性,如下所示

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" data-order = '.abs($row->overall_returns_count) ?? '0'.' title="Total Returns">'.(abs($row->overall_returns_count) ?? 'N/A').'</span> </a>';
  }