0

编辑这是我从视图中的下拉列表-

                 <div class="ToolBox" style="height: 30px; width: 100%">
                <b>&nbsp;&nbsp;Sort By:&nbsp;</b>
                <select id="ddlSortBy" style="margin-top: 0px; height: 24px; width: 160px !important"
                    data-bind="value: serverSelectedOptionID, options: serverOptions, optionsText: 'name', optionsValue: 'id'">
                </select>&nbsp;&nbsp;<img data-bind="click: SortUpDownAllCerts" src="/Content/images/updownarrow.bmp" style="padding-bottom: 4px; cursor: pointer; vertical-align: middle;" />
            </div>

在我看来,我正在显示 observableArray 的长度-

 My Certificates&nbsp;(<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?

4

1 回答 1

0

你会为此恨自己——

http://jsfiddle.net/VzER2/15/

您的代码中唯一的问题是

<h2 data-bind="text: title()"></h2>Length: <span data-bind="text: allCertificates().length"></span>

你有 allCertificates.length 而不是 allCertificates().length。当您在视图中并使用数据绑定时,它会为您解开您的 observable。但是,如果您需要获取该可观察对象的属性,则需要再次调用它。

如果还有其他不起作用的东西,请告诉我,我没有尝试更改 serverSelectedID 的值

于 2013-07-22T23:07:57.873 回答