编辑这是我从视图中的下拉列表-
<div class="ToolBox" style="height: 30px; width: 100%">
<b> Sort By: </b>
<select id="ddlSortBy" style="margin-top: 0px; height: 24px; width: 160px !important"
data-bind="value: serverSelectedOptionID, options: serverOptions, optionsText: 'name', optionsValue: 'id'">
</select> <img data-bind="click: SortUpDownAllCerts" src="/Content/images/updownarrow.bmp" style="padding-bottom: 4px; cursor: pointer; vertical-align: middle;" />
</div>
在我看来,我正在显示 observableArray 的长度-
My Certificates (<span data-bind="text: allCertificates().length"></span>)
如果我使用以下代码对 observableArray 进行排序,则 allCertificates observableArray 的长度显示为 0(屏幕上确实显示 66 条记录,因此 observableArray 的长度应为 66)-
serverSelectedOptionID.subscribe(function () {
var sortCriteriaID = serverSelectedOptionID._latestValue;
allCertificates.sort(function (a, b) {
var fieldname = serverOptions[sortCriteriaID-1].OptionText;
allCertificates.valueHasMutated();
if (a[fieldname] == b[fieldname]) {
return a[fieldname] > b[fieldname] ? 1 : a[fieldname] < b[fieldname] ? -1 : 0;
}
return a[fieldname] > b[fieldname] ? 1 : -1;
});
});
如果我注释掉上面的代码,allCertificates observableArray 显示 66 作为长度,就像它应该的那样。
为了完整起见,这是整个视图模型代码-
define(['services/logger', 'durandal/system', 'durandal/plugins/router', 'services/CertificateDataService'],
function (logger, system, router, CertificateDataService) {
var allCertificates = ko.observableArray([]);
var myCertificates = ko.observableArray([]);
var serverSelectedOptionID = ko.observableArray();
var serverOptions = [
{ id: 1, name: 'Certificate', OptionText: 'lwCertID' },
{ id: 2, name: 'Client Name', OptionText: 'clientName' },
{ id: 3, name: 'Client Number', OptionText: 'clientNumber' },
{ id: 4, name: 'Request Date', OptionText: 'requestDate' },
{ id: 5, name: 'Collateral Analyst', OptionText: 'userName' }
];
var serverSelectedOptionID = ko.observable();
var activate = function () {
// go get local data, if we have it
return SelectAllCerts(), SelectMyCerts();
};
var vm = {
activate: activate,
allCertificates: allCertificates,
myCertificates: myCertificates,
title: 'Certificate Approvals',
SelectMyCerts: SelectMyCerts,
SelectAllCerts: SelectAllCerts,
theOptionId: ko.observable(1),
serverOptions: serverOptions,
serverSelectedOptionID: serverSelectedOptionID
};
serverSelectedOptionID.subscribe(function () {
var sortCriteriaID = serverSelectedOptionID._latestValue;
allCertificates.sort(function (a, b) {
var fieldname = serverOptions[sortCriteriaID-1].OptionText;
allCertificates.valueHasMutated();
if (a[fieldname] == b[fieldname]) {
return a[fieldname] > b[fieldname] ? 1 : a[fieldname] < b[fieldname] ? -1 : 0;
}
return a[fieldname] > b[fieldname] ? 1 : -1;
});
});
function getFieldNameByOptionID(OptionID) {
}
return vm;
function SortUpDownAllCerts() {
return allCertificates.sort()
}
function SortUpDownMyCerts() {
return allCertificates.sort()
}
function SelectAllCerts() {
return CertificateDataService.getallCertificates(allCertificates);
}
function SelectMyCerts() {
return CertificateDataService.getMyCertificates(myCertificates);
}
});
我怎样才能让它工作?为什么它评估为 0 而不是 66?