問題是.tablesorter
插件不會在您的模型中對self.users
數組進行排序,它僅對實際的<tr>
元素進行排序。你列分揀機必須調用observableArray.sort
法明確定製的比較器作爲參數:
self.sortCol = function(colName, event) {
// get sorting direction
var th = event.target;
th.sortDir = (th.sortDir == 'asc' ? 'desc' : 'asc');
// array item comparator
function comp(a, b){
// get values to compare
var av = a[colName](), bv = b[colName]();
// compare using current direction
if (th.sortDir == 'asc') {
return av == bv ? 0 : av > bv ? 1 : -1;
} else {
return av == bv ? 0 : av < bv ? 1 : -1;
}
}
// do sort
self.users.sort(comp);
}
工作小提琴:http://jsfiddle.net/a8jUj/31/
附:舉個例子,我通過函數綁定了.sortCol()
以獲得對事件對象的訪問權限。您可以重寫模型以在模型屬性中存儲當前的排序參數。
由於馬丁內斯......這讓我在正確的方向。 –