这是我第一次尝试进行数据表多列排序,我只需要一些帮助,看看我指定的内容是否会按照我想要的方式运行。
我有下表。
我想要的数据表是:
这是我到目前为止所尝试的内容,但如果可能的话,我会提供一些帮助。另外我如何指定排序的desc部分(目前我只能算出asc。
$("#storeHealthTbl").dataTable ({
"bSort": true,
"bLengthChange": false,
"bPaginate": false,
"aoColumnDefs": [ {
"aTargets": [ 2 ],
"sType": "string",
"aDataSort": [ 2, 4, 6 ] /* Want 2,4,6 to be desc but don't know how*/
}, {
"aTargets": [ 4 ],
"sType": "string",
"aDataSort": [ 4, 6 ] /* Want 4,6 to be desc but don't know how*/
}, {
"aTargets": [ 6 ],
"sType": "string",
"aDataSort": [ 6 ] /* Want 6 to be desc but don't know how*/
}, {
"aTargets": [ 0 ],
"sType": "numeric",
"aDataSort": [ 0 ] /* This one is asc */
}, ]
});
我还应该提一下,带有图像的列在某些时候会有一个字母“X”,因此这就是我期望排序的工作方式。如果没有“X”并且它们都是绿色图像(即不是字母字符“那么我想跳到我指定的下一个类别。)
希望这是有道理的。
我正在使用数据表1.9.4。
感谢
答案 0 :(得分:1)
我找不到任何可以完成此级别条件排序的内置功能。如果你不介意使用锤子的程序化等效物,下面应该可以做到这一点。
请注意,我可能没有您想要的排序顺序,但您可以轻松地更改它。
// function to check our conditions and set the proper sort order
function getSortOrder(sortCols, sortVars) {
var set = false;
// loop through our array of columns to check
$.each(sortCols, function(ind, elm) {
// create a var to hold the value in this column on the first row
var firstVal;
// loop through each row
$("#datatable").find('tr').each(function(tri, tre) {
// stop checking if we already found a column with different values
if (!set) {
// otherwise, et get the value in this column on this row
var curCellVal = $(this).find('td').eq(sortCols[ind]).text();
// row 0 is a header row, do nothing
if (tri == 0) {
} else if (tri == 1) { // for first actual row, set `firstVal` to its value
firstVal = curCellVal;
} else { // if after first row, check current row's cell value against 'firstVal'
// if the values are different, this column can be sorted,
if (curCellVal != firstVal) {
// flag as set so the function stops looking
// this could probably be done better
set = true;
// initialize datatable with the sort order at the current index of `sortVars`
initDTWithSortOrder(sortVars[ind]);
}
}
}
});
});
// default initialization if not caught by our function
var datatable = set ? null : $("#datatable").dataTable();
};
// `getSortOrder()` takes two parameters
// parameter 1: array: columns to check
// parameter 2: array: sort values (each being a muti-dimensional array: [column, direction] )
getSortOrder([2, 6, 8, 0], [
[[2, 'desc'],[4, 'desc'],[6, 'desc']], // sort order to use is column 3 is not all the same value
[[4, 'desc'],[6, 'desc']], // sort order to use is column 7 is not all the same value
[[8, 'desc']], // sort order to use is column 9 is not all the same value
[[0, 'asc']], // sort order to use is column 1 is not all the same value
]);
function initDTWithSortOrder(sortOrder) {
$("#datatable").dataTable({
"bSort": true,
"bLengthChange": false,
"bPaginate": false,
"aaSorting": sortOrder
});
}