[英]dgrid custom sort with secondary sort column
我當前在dgrid上使用自定義排序功能(粘貼在下面)。 它不會徹底改變排序,只對一個特定的列進行唯一排序,對其他不區分大小寫的列進行排序。 我想通過名為“預定”的列添加輔助排序,以便在對任何其他列進行排序時將其添加到排序中。 我只是不確定如何去做。 我已經看到了如何重寫排序以按兩列進行排序,但是當進行自定義排序時卻沒有。 二級排序將始終存在,無論單擊什么其他列。
供參考,我正在運行dojo 1.10和dgrid 1.0。 數據來自RequestMemory DStore,我確實希望這種情況發生在網格上,而不是返回到存儲級別。 任何幫助,將不勝感激。
currGrid.on('dgrid-sort', function (event) { event.preventDefault(); var sort = event.sort[0]; currGrid.set('sort', function (a, b) { if (sort.property == "thisField") { //special sort for thisField if (a[sort.property] !== 'undefined' && typeof a[sort.property] == "string") { var colorA = a[sort.property].split("|"); var aValue = colorA[0].toLowerCase(); } if (b[sort.property] !== 'undefined' && typeof b[sort.property] == "string") { var colorB = b[sort.property].split("|"); var bValue = colorB[0].toLowerCase(); } if (String(aValue) == String(bValue)) { var result = 0; } else if (dojo.string.trim(aValue) == "") { var result = true ? 1 : -1; } else if (dojo.string.trim(bValue) == "") { var result = true ? -1 : 1; } else { var result = aValue > bValue ? 1 : -1; } return result * (sort.descending ? -1 : 1); } else { //Sort for all other fields same as always (except toLowerCase) if (a[sort.property] !== 'undefined' && typeof a[sort.property] == "string") { var aValue = a[sort.property].toLowerCase(); } else { var aValue = ""; } if (b[sort.property] !== 'undefined' && typeof b[sort.property] == "string") { var bValue = b[sort.property].toLowerCase(); } else { var bValue = ""; } var result = aValue > bValue ? 1 : -1; return result * (sort.descending ? -1 : 1); } }); currGrid.updateSortArrow(event.sort, true); }); currGrid.startup();
您可以執行以下操作。
currGrid.on('dgrid-sort', function (event) {
event.preventDefault();
var sortSet = [];
sortSet.push(event.sort[0]);
sortSet.push({property: "scheduled"});
currGrid.set('sort', function (a, b) {
var aValue, bValue, result = 0;
for(var i = 0; i < sortSet.length; i++){
var sort = sortSet[i];
if (sort.property == "thisField") {
//special sort for thisField
if (a[sort.property] !== 'undefined' && typeof a[sort.property] == "string") {
var colorA = a[sort.property].split("|");
aValue = colorA[0].toLowerCase();
}
if (b[sort.property] !== 'undefined' && typeof b[sort.property] == "string") {
var colorB = b[sort.property].split("|");
bValue = colorB[0].toLowerCase();
}
if (String(aValue) == String(bValue)) {
result = 0;
} else if (dojo.string.trim(aValue) == "") {
result = true ? 1 : -1;
} else if (dojo.string.trim(bValue) == "") {
result = true ? -1 : 1;
} else {
result = aValue > bValue ? 1 : -1;
}
return result * (sort.descending ? -1 : 1);
} else {
//Sort for all other fields same as always (except toLowerCase)
if (a[sort.property] !== 'undefined' && typeof a[sort.property] == "string") {
aValue = a[sort.property].toLowerCase();
} else {
aValue = "";
}
if (b[sort.property] !== 'undefined' && typeof b[sort.property] == "string") {
bValue = b[sort.property].toLowerCase();
} else {
bValue = "";
}
//You need this check here
if(aValue != bValue){
result = aValue > bValue ? 1 : -1;
return result * (sort.descending ? -1 : 1);
}
}
}
return 0;
});
currGrid.updateSortArrow(event.sort, true);
});
currGrid.startup();
我對您的代碼有一些擔憂,變量result, aValue and bValue
都在if語句內局部存在,但它們在語句外使用。 如果在全局空間中使用相同的名稱定義了其他一些變量,則可能導致錯誤的結果。 所以我修改了它們。
因此,第二部分需要檢查aValue == bValue
是否返回0。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.