我正在尝试对包含父行和子行的表进行排序。应始终根据父行执行排序,但子行应始终紧跟父行。表数据的格式为
[
{name: 'xxx', group: 'A', type: 'parent'},
{name: 'yyy', group: 'B', type: 'parent'},
{name: 'zzz', group: 'A', type: 'child'},
{name: 'qqq', group: 'A', type: 'child'}
]
因此,按名称排序,正确的顺序是xxx,qqq,zzz,yyy。
数据来自Laravel / Eloquent ajax查询并显示在datatables表中,因此对客户端或服务器端进行排序就可以了。
与multisort无关(下面的评论)
答案 0 :(得分:0)
使用orderFixed
选项始终在任何其他列之前/之后对某个列应用排序。
例如:
var table = $('#example').DataTable({
ajax: {
url:'https://api.myjson.com/bins/4n6ey',
dataSrc: ''
},
orderFixed: {
pre: [[1, 'asc'], [2, 'desc']]
},
columns: [
{ data: 'name' },
{ data: 'group' },
{ data: 'type'}
]
});
请参阅this jsFiddle以获取代码和演示。
答案 1 :(得分:0)
无法弄清楚如何在SQL,DataTables回调/排序器中进行操作(尽管插件是可行的选项),Eloquent回调或Yajra DataTables适配器选项。所以我只是采取了蛮力的方式。
查找每个子行的主记录以获取正确的排序索引,并为“true”订单创建新的索引列
$dataTable_column_map = $claimPortfolio->getColumnMap();
$claimPortfolioLines = $claimPortfolio->lines()->orderBy($dataTable_column_map[$request->get('order')[0]['column']]['name'], $request->get('order')[0]['dir'])->get();
$claimPortfolioLines = ClaimPortfolioService::orderGuarantees($claimPortfolioLines);
$claimPortfolioLines = ClaimPortfolioService::filterLines($claimPortfolioLines, Input::get('search.value'));
Session::put('claimPortfolioLineIdList', $claimPortfolioLines->lists('id')->toArray());
return Datatables::of($claimPortfolioLines)
->order(function() {
return true; // cancel built-in ordering & filtering
})
->filter(function() {
return true;
})
->make(true);
public static function orderGuarantees(Collection $claimPortfolioLines)
{
$claimPortfolioLinesGuarantees = $claimPortfolioLines->filter(function ($claimPortfolioLine) {
return $claimPortfolioLine->line_type == 'GUARANTEE';
});
$claimPortfolioLines = $claimPortfolioLines->filter(function ($claimPortfolioLine) {
return $claimPortfolioLine->line_type == 'DEBT';
});
foreach ($claimPortfolioLines as $idx_line => $claimPortfolioLine)
{
$claimPortfolioLine->sortOrder = $idx_line;
foreach ($claimPortfolioLinesGuarantees as $idx_guaranteeLine => $claimPortfolioLineGuarantee)
{
if ($claimPortfolioLineGuarantee->contract_no == $claimPortfolioLine->contract_no && $claimPortfolioLine->line_type == 'DEBT')
{
$claimPortfolioLineGuarantee->sortOrder = "{$idx_line}.{$idx_guaranteeLine}";
$claimPortfolioLines->push($claimPortfolioLineGuarantee);
}
}
}
$claimPortfolioLines = $claimPortfolioLines->sortBy('sortOrder');
return $claimPortfolioLines;
}
答案 2 :(得分:0)
SQL的一般解决方案是通过多个列进行自我连接和排序,包括是否为父列。在OP的情况下,假设数据表为d
(t
的别名是 table ,而s
的含义是 sort ):< / p>
SELECT t.*
FROM d AS t
INNER JOIN d AS s
ON s.group = t.group
AND t.type = 'parent'
ORDER BY s.name, t.type = 'parent' DESC, t.name