【问题标题】:Laravel Datatables: How to sort column with the second dataLaravel Datatables:如何使用第二个数据对列进行排序
【发布时间】:2021-04-01 02:59:52
【问题描述】:

如何根据第二个数据进行排序?排序仅适用于 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-&gt;id,我要的是像abs($row-&gt;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 进行排序,当我悬停按钮时,您可以在左下角看到。 (id从2开始)

系统详情 操作系统 Windows 10

PHP 7.2 版

Laravel 6.0 版

Laravel-Datatables 版本 ^9.6

【问题讨论】:

  • $users 是查询构建器还是集合?
  • 您是否已经考虑过,您的命名路线可能是错误的?
  • 也许这是因为DataTables manual顶部的大红色通知? Added columns are assumed to be computed columns and not part of the database. Thus, search/sort will be disabled on those columns. If you need them, use the editColumn api instead.如果你使用官方文档'example of sorting by multiple columns可能会更容易。
  • 试过但还是不行@DanielProtopopov

标签: php laravel datatable yajra-datatable laravel-datatables


【解决方案1】:

尝试在下面的行中添加数据顺序属性

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>';
  }

【讨论】:

  • 这是什么意思?
  • 它将按该属性值对列进行排序
  • 还是一样的先生
  • 检查行并检查您是否在数据顺序属性中获取计数值
  • 是的,我正在获取计数值,但它没有排序。你能帮我解决这个问题吗?
【解决方案2】:

您的数据源是什么?如果您的数据源是 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));
    })

然后在 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 作为数据源会导致性能变慢并且某些功能看起来很乱,所以我建议你使用 Query Builder 作为数据源。

这将返回集合:

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

这将返回查询生成器:

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

【讨论】:

  • 所以我现在可以使用这个对徽章进行排序了吗?不是身份证?
  • 根据列排序,在您的情况下为overall_returns_count
  • 我测试了两个选项(JS 和 PHP 渲染)都可以工作。但该列应该来自选择查询,如select('column_name')withCount,而不是来自自定义模型属性。
  • 它的工作,但它只返回一个字符串。按钮和徽章未显示。我遵循了第二个选项先生,如果我修改它也会返回一个 html 字符串
  • 哦,我忘了最后一件事,把它添加到 rawColumns,-&gt;rawColumns(['overall_returns_count'])
猜你喜欢
  • 2013-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-26
  • 1970-01-01
相关资源
最近更新 更多