How to get an observableArray's length?

前端 未结 3 1135
后悔当初
后悔当初 2020-12-17 07:42

I am attempting to create an array of contract line items (CLINs) that will be displayed as individual div elements below a header of general contract informati

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-17 08:03

    For anyone who wants to get the length of an observableArray without the _destroyed items, you can use this function:

    ko.observableArray.fn.totalVisible = function() {
        var items = this(), count = 0;
    
        if (items == null || typeof items.length === "undefined") {
            return 0;
        }
    
        for (var i = 0, l = items.length; i < l; i++) {
            if (items[i]._destroy !== true) {
                count++;
            }
        }
    
        return count;
    };
    

    Then to use it:

    myObservableArray.totalVisible();
    

提交回复
热议问题