尽管在排序时有数据,但Observablearray长度评估为0

时间:2013-07-19 20:01:16

标签: javascript knockout.js knockout-2.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作为它应该的长度。

为了完整性,这里是整个viewmodel代码 -

 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?

1 个答案:

答案 0 :(得分: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的值