当我对可观察数组进行排序时,我遇到问题,数组得到更新但没有更改UI
码
型号代码:
function OrderByOptionModel(field, displayText, isSelected, dispIndex) {
var self = this;
self.field = field;
self.dispText = displayText;
self.isSelected = ko.observable(isSelected);
self.dispIndex = dispIndex;
self.click = function (data) {
if (data == null) return;
data.isSelected(!data.isSelected());
};
self.getOrderByOptions = function () {
return [
new OrderByOptionModel(OrderByTypes.INDUSTRY, 'Industry', false, 1),
new OrderByOptionModel(OrderByTypes.SUPERSECTOR, 'Super Sector', false, 2),
new OrderByOptionModel(OrderByTypes.SECTOR, 'Sector', false, 3),
new OrderByOptionModel(OrderByTypes.SUBSECTOR, 'Sub Sector', false, 4),
new OrderByOptionModel(OrderByTypes.COUNTRY, 'Country', false, 5),
new OrderByOptionModel(OrderByTypes.MARKETCAP, 'Market Cap', false, 6),
new OrderByOptionModel(OrderByTypes.ATOZ, 'A - Z', false, 7),
new OrderByOptionModel(OrderByTypes.DATETIME, 'Date Time', false, 8)
];
};
return self;
}
可观察数组:
self.orderByOptions = ko.observableArray(OrderByOptionModel().getOrderByOptions());
排序代码:
self.orderByOptions().sort(function (l, r) {
return l.dispIndex > r.dispIndex ? 1 : -1;
});
但UI没有得到更新我不知道问题出在哪里?
答案 0 :(得分:2)
删除observableArray赋值中的括号。
self.orderByOptions = ko.observableArray(OrderByOptionModel().getOrderByOptions);
具有敲除的东西是它在将事物绑定到可观察对象时代表您调用函数。有时只需将括号移除到返回数据并绑定到函数本身的函数就可以修复它。