简体   繁体   English

Knockout从observableArray获取对象

[英]Knockout get object from observableArray

I have an observableArray in my Knockout app and I'm wondering how I can go about selecting only the targeted object within the array. 我的Knockout应用程序中有一个observableArray,我想知道如何只选择数组中的目标对象。

I have the following code which calculates the total of all 'value's within the array but I'd like to be able to just select, say, only the second 'value' within the array. 我有以下代码来计算数组中所有'值的总和,但我希望能够只选择数组中的第二个'值'。

self.csu_treatment_inputs = ko.observableArray([
                {
                    value: ko.observable(10),
                    image: ko.observable('')
                },
                {
                    value: ko.observable(120),
                    image: ko.observable('')
                },
                {
                    value: ko.observable(160),
                    image: ko.observable('')
                },

            ]);

self.totaltest = ko.computed(function () {
            var total = 0;
            ko.utils.arrayFilter(self.value_inputs(), function (item) {
                  total += parseFloat(ko.utils.unwrapObservable(item.value));
            });
            return total;
    });

To bind to the second value, use this code: 要绑定到第二个值,请使用以下代码:

<span data-bind='text: csu_treatment_inputs()[1].value'></span>

Exemple: http://jsfiddle.net/v6T5T/ 例如: http//jsfiddle.net/v6T5T/

If you want to access an element from javascript, use this: 如果要从javascript访问元素,请使用以下命令:

total+= parseFloat(self.test_values()[i].value());

Exemple: http://jsfiddle.net/v6T5T/2/ 例如: http//jsfiddle.net/v6T5T/2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM