数据表多列排序 - 规范所需的帮助
Datatables multi-column sort - help required with specification
这是我第一次尝试对数据table进行多列排序,我只需要一些帮助来确定我指定的内容是否会按我希望的方式工作。
我有以下 table.
我想要的是数据table到:
- 按第三列 (Conn Status) desc 排序,然后是二次排序。如果第三列是 sortable,则不需要应用为后续列指定的排序规则。
- 如果第三列具有所有相同的值,因此无法应用排序,那么我希望它按第 5 列 desc (mdi) 排序,然后进行二次排序。如果第五列是 sortable,则不需要应用为后续列指定的排序规则。
- 如果第五列具有所有相同的值,因此无法应用排序,我希望它按第 7 列 (DSM) desc 排序,然后进行第二次排序。如果第七列是 sortable,则不需要应用为后续列指定的排序规则。
- 如果第七列具有所有相同的值,因此无法应用排序,那么我希望它按第 1 列 (SiteId) asc 排序。
这是我迄今为止尝试过的方法,但如果可能的话,我希望得到一些帮助。另外我如何指定排序的 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" 并且它们都是绿色图像(即不是字母字符”,那么我想跳转到我指定的下一个类别)。
希望这是有道理的。
我正在使用数据 tables 1.9.4.
谢谢
我找不到任何可以完成这种级别的条件排序的内置功能。如果您不介意使用相当于锤子的程序化工具,下面的代码应该可以解决问题。
请注意,我的排序顺序可能不适合您的需要,但您可以很容易地进行更改。
这是一个working jsfiddle
// 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
});
}
这是我第一次尝试对数据table进行多列排序,我只需要一些帮助来确定我指定的内容是否会按我希望的方式工作。
我有以下 table.
我想要的是数据table到:
- 按第三列 (Conn Status) desc 排序,然后是二次排序。如果第三列是 sortable,则不需要应用为后续列指定的排序规则。
- 如果第三列具有所有相同的值,因此无法应用排序,那么我希望它按第 5 列 desc (mdi) 排序,然后进行二次排序。如果第五列是 sortable,则不需要应用为后续列指定的排序规则。
- 如果第五列具有所有相同的值,因此无法应用排序,我希望它按第 7 列 (DSM) desc 排序,然后进行第二次排序。如果第七列是 sortable,则不需要应用为后续列指定的排序规则。
- 如果第七列具有所有相同的值,因此无法应用排序,那么我希望它按第 1 列 (SiteId) asc 排序。
这是我迄今为止尝试过的方法,但如果可能的话,我希望得到一些帮助。另外我如何指定排序的 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" 并且它们都是绿色图像(即不是字母字符”,那么我想跳转到我指定的下一个类别)。
希望这是有道理的。
我正在使用数据 tables 1.9.4.
谢谢
我找不到任何可以完成这种级别的条件排序的内置功能。如果您不介意使用相当于锤子的程序化工具,下面的代码应该可以解决问题。
请注意,我的排序顺序可能不适合您的需要,但您可以很容易地进行更改。
这是一个working jsfiddle
// 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
});
}