Laravel Datatables:如何按第二列数据排序

3

如何根据第二个数据进行排序?目前的排序仅基于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},

enter image description here

如您所见,如果我点击标题,它不会对徽章进行排序。相反,它会对id进行排序,您可以在悬停按钮时在左下角看到(从2开始的id)。

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

PHP 版本 7.2

Laravel 版本 6.0

Laravel-Datatables 版本 ^9.6


$users 是一个查询构建器还是一个集合? - Muhammad Dyas Yaskur
你是否已经考虑过,你的命名路由可能是错误的? - Tschitsch
也许这是因为在DataTables手册顶部有一个大红色通知的缘故?添加的列被认为是计算列而不是数据库的一部分。因此,这些列的搜索/排序将被禁用。如果需要它们,请改用editColumn api。如果您使用官方文档中的按多列排序的示例,可能会更容易些。 - Daniel Protopopov
尝试过了,但仍然不起作用 @DanielProtopopov - draw134
2个回答

6
尝试在行中添加 data-order 属性,如下所示。
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>';
  }

这是什么意思? - draw134
它将根据该属性值对列进行排序。 - Huzaifa Qidwai
仍然一样,先生。 - draw134
检查行并检查是否在data-order属性中获得了计数值。 - Huzaifa Qidwai
是的,我得到了计数值,但它没有排序。你能帮我进一步解决这个问题吗? - draw134
我现在明白了。谢谢你的帮助。data-order就是答案。 - draw134

3
你的数据源是什么?如果你的数据源是laravel集合,你应该在使用 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));
    })

在 JavaScript 中的列:

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

只有使用eloquent/DB查询构建器作为数据源时,PHP渲染才能正常工作,但JS渲染可以在所有数据源上工作,包括Laravel集合和eloquent构建器。

仅供您参考,Yajra Datatable支持多种数据源,包括集合和查询构建器。有时候如果您使用集合作为数据源会导致性能变慢,并且一些功能看起来比较混乱,因此我建议您使用查询构建器作为数据源。

这将返回集合:

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

这将返回查询构建器:
$users = User::withCount('overall_returns'); 

那么现在我可以使用这个来排序徽章了吗?而不是使用ID? - draw134
按照列排序,以您的情况为例 overall_returns_count - Muhammad Dyas Yaskur
它能工作,但只返回一个字符串。按钮和徽章没有显示出来。我按照第二个选项做了,先生,但如果我进行修改,它也会返回一个HTML字符串。 - draw134
哦,我忘了最后一件事,将其添加到rawColumns中,-> rawColumns(['overall_returns_count']) - Muhammad Dyas Yaskur
它仍然无法对徽章进行排序,而是对$row->id进行排序。 - draw134
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接