在 ViewModel 我有一个 observableArray 和一个计算返回在视图中管理的数组:
var operators = ko.observableArray([]);
var records = ko.computed(function () {
return operators();
});
这是我的排序方法;
Order = function (data, event, colName) {
var th = $('#th-' + colName),
attr = th.data('sort');
setAttribute(th, attr); //switch data-sort between 'asc' and 'desc'
if (attr === 'asc') {
operators = operators.sort(function (a, b) {
return (a.fullname === b.fullname) ? 0 : (a.fullname < b.fullname ? -1 : 1);
});
}
else {
operators = operators.sort(function (a, b) {
return (a.fullname === b.fullname) ? 0 : (a.fullname > b.fullname ? -1 : 1);
});
}
};
这是观点:
<table id="table-hour">
<thead>
<tr>
<th data-bind="click: function(data, event) { Order(data, event, 'fullname')}" data-sort="asc" id="th-fullname">Fullname</th>
</tr>
</thead>
<tbody>
<!-- ko foreach: records -->
<tr>
<td>
<span data-bind="text: fullname"></span>
</td>
</tr>
<!-- /ko -->
</tbody>
好吧,排序功能就像一个魅力,实际上用浏览器调试我可以看到数组以两种方式排序,但视图只是第一次更新。我应该怎么做才能解决这个问题?