我目前正在使用Durandal进行数据网格渲染,通过单击列标题,它应该对结果进行排序。我遇到问题的唯一问题是如果数据结果超过1页,当我点击下一页时,排序后的结果将恢复正常。 请协助。 谢谢,
var searchTerm = ko.observable("");
var pagination = { current: ko.observable(1), pages: ko.observableArray() };
var sortables = {
barcode: {
id: ko.observable('barcode'),
active: ko.observable(true),
asc: ko.observable(true)
},
itemcode: {
id: ko.observable('barcodeMap.item.code'),
active: ko.observable(false),
asc: ko.observable(true)
},
description: {
id: ko.observable('barcodeMap.item.description'),
active: ko.observable(false),
asc: ko.observable(true)
},
quantity: {
id: ko.observable('quantity'),
active: ko.observable(false),
asc: ko.observable(true)
},
store: {
id: ko.observable('store.name'),
active: ko.observable(false),
asc: ko.observable(true)
},
fixture: {
id: ko.observable('fixture'),
active: ko.observable(false),
asc: ko.observable(true)
},
count: {
id: ko.observable('count'),
active: ko.observable(false),
asc: ko.observable(true)
},
createdBy: {
id: ko.observable('createdBy'),
active: ko.observable(false),
asc: ko.observable(true)
},
dateCreated: {
id: ko.observable('dateCreated'),
active: ko.observable(false),
asc: ko.observable(true)
}
};
var filters = { store: ko.observable(null), count: ko.observable(null), user: ko.observable(null), fixture: ko.observable(null) };
var items = ko.observableArray();
var activate = function () {
if (appsecurity.user().isAuthenticated) {
pagination.current(1); pagination.pages([]); // reset pagination
return repo.getRemote("stockId, barcode", breeze.Predicate.create("barcode", breeze.FilterQueryOp.Equals, null), pagination.current())
.then(function (obj) {
items(obj.data);
setPages(obj.inlineCount);
});
}
else
return Q.resolve();
};
var sort = function (id) {
for (var key in sortables) {
sortables[key].active(false);
}
var sortable = sortables[id];
sortable.active(true);
sortable.asc(!sortable.asc());
if (items().length > 0) {
var local = repo.getLocally(sortable.id().concat(sortable.asc() ? "" : " desc"), getPredicate(), pagination.current());
if (local.length > 0)
return items(local);
}
var changePage = function (page) {
if (!indicator.isLoading() && (page >= 1 && page <= pagination.pages().length && page != pagination.current())) {
indicator.isLoading(true);
pagination.current(page);
ko.utils.arrayForEach(pagination.pages(), function (item) {
if (item.page() == page)
item.active(true);
else
item.active(false);
});
return repo.getRemote("stockId, barcode", getPredicate(), pagination.current()) //[CH]
.then(function (obj) {
indicator.isLoading(false);
items(obj.data);
});
}
};