【发布时间】:2015-12-14 03:42:04
【问题描述】:
我正在尝试toggle css class table head。但是表头已经有了click binding:我引用了docs here:
默认情况下,我需要在表头(thead) 上有一个类,然后在单击表头时添加/删除(切换)该类。目标是使用该类向可排序的表头添加视觉描述:
下面是进行排序的代码:NOTE: 这个代码和想法是从Ryan Rahlf blog 复制而来的。 Article here:
我认为可以从排序函数调用 css 绑定,以便在单击列头时应用或删除它:
self.sort = function(header, event){
// This is the existing click binding (sort)
// Place css logic in here so that individual thead class is toggled when clicked.
if(self.activeSort === header) {
header.asc = !header.asc;
} else {
self.activeSort = header;
}
var prop = self.activeSort.sortPropertyName;
var ascSort = function(a,b){ return a[prop] < b[prop] ? -1 : a[prop] > b[prop] ? 1 : a[prop] == b[prop] ? 0 : 0; };
var descSort = function(a,b){ return a[prop] > b[prop] ? -1 : a[prop] < b[prop] ? 1 : a[prop] == b[prop] ? 0 : 0; };
var sortFunc = self.activeSort.asc ? ascSort : descSort;
self.Artist.sort(sortFunc);
};
【问题讨论】:
-
连头部的点击也不能切换吗?
-
不是我尝试过的,我需要将 css 绑定到头部,但按照 Knockout 文档中的说明添加 css 绑定不起作用。
标签: javascript jquery knockout.js knockout-2.0