The situation is this, I have a column of data I want to render. The data is retrieved by Ajax and the rendered value is not what I want to sort by.
Approach
- Build all your data including the sort value
- Attach multiple values to a field
- Sort and render
Build your data
I put together my details in Laravel using Laravel Datatables
$results = [0 => ['item' => 'A', 'orderable' => 1, 'data' =>'some data'],1 => ['item' => 'B', 'orderable' => 2, 'data' => 'some more data],...];$data = Datatables::of($results)->toJson();
We can add an edit column method here to attach data to a column
Datatables::of($results)->editColumn('item', function($item) {
return [
'display' => $item['item'];
'data' => $item['data'];
'orderable' => $item['orderable'];
];
})->toJson();
This means we can now access this from the render method in columns on the frontend
columns: [{ data: 'item', name: 'Item', render: {
_: (item) => {
//This is the rendering bit, you can access all sub parts of your data with dots
return item.display + item.data;
},
sort: 'orderable' //This is the column attached that you want to use to sort on
}}]
This is some of the search phrases I used to try and sort this problem but ended up working a way around it
- datatables datasort
- datatable sort column by another column
- datatables sortable render
- ajax datatable sortable
- render html laravel datatables and sorting
Helpful links: