简体   繁体   English

可观察数组的淘汰赛可观察数组

[英]Knockout Observable Array of Observable Array

I have passed a model to my View Model from .Net, which is a Object, containing a few fields, and a List, and each of those has another List. 我已经将模型从.Net传递给我的视图模型,该模型是一个Object,包含一些字段和一个List,每个字段都有另一个List。

So, an object with a List of Lists. 因此,具有列表列表的对象。

On my View, I represent this as some data, with a list of Tabs (The first list), and then each tab has a grid of data. 在我的视图上,我将其表示为一些数据,并带有一个选项卡列表(第一个列表),然后每个选项卡都有一个数据网格。

In my Knockout View model, I just have: 在我的淘汰赛视图模型中,我只有:

self.Name = ko.observable("");
self.Width = ko.observable(0);
self.Height = ko.observable(0);

self.TemplateGroups = ko.observableArray();

So, my main object, with an observable array (the first list). 因此,我的主要对象带有一个可观察的数组(第一个列表)。 But, the list within that, isn't observable. 但是,其中的列表是不可观察的。

So when rendering my tabls, I do: 因此,在渲染表格时,我会执行以下操作:

<div class="tab-content" data-bind="foreach: TemplateGroups">

And that renders each tab. 并渲染每个标签。 And then within each tab, I do this: 然后在每个选项卡中,执行以下操作:

<tbody data-bind="foreach: $data.Components">
      <tr  style="cursor: pointer" data-bind="click: $parents[1].ClickedIt">

('Components' is the name of the list within the outer list) (“组件”是外部列表中列表的名称)

Problem is, on my ClickIt function, I am displaying the Idof the row I clicked, and it works. 问题是,在我的ClickIt函数上,我正在显示单击的行的Idof,它可以正常工作。 And then I am just trying to change the 'Description' field of this outer list, but ... it's not a function, so, no observed: 然后,我只是尝试更改此外部列表的“描述”字段,但是...这不是一个函数,因此,没有观察到:

self.ClickedIt = function () {
    alert(this.Id);
    this.Description("Craig");

}

The alert shows the Id, but error on Description("Craig"), as it's not a function. 警报显示ID,但Description(“ Craig”)错误,因为它不是函数。

How can I make the list, within my ObservableArray, observable? 如何在ObservableArray中使列表可观察?

(To be clearer, my model I pass in is an object, which contains a List called TemplateGroups... and then each item in that list, contains a List called 'Components'. It's the Id of one of those Components that I am displaying, but I need to make that list Observable. (更清楚地说,我传入的模型是一个对象,其中包含一个名为TemplateGroups ...的列表,然后该列表中的每个项目均包含一个名为“ Components”的列表。这是我所使用的其中一个Components的ID显示,但我需要使该列表可观察。

Edit: This seems to be what I am looking for ( http://jsfiddle.net/rniemeyer/bZhCk/ ), but ... I am not defining my array the same way. 编辑:这似乎是我在寻找( http://jsfiddle.net/rniemeyer/bZhCk/ ),但是...我不是以相同的方式定义数组。 Instead, they're being passed in from a .Net class (So, converted to JSON, I think). 相反,它们是从.Net类传入的(因此,我认为已转换为JSON)。 And that .Net class contains a List of another .Net class, which has a List). 该.Net类包含另一个.Net类的列表,该类具有一个List。

Note, My load method: 注意,我的加载方法:

self.loadData = function () {
        $.get("/api/PlateTemplate/Get", { id: self.TemplateId() }).done(function (data) {
            self.Name(data.Description);
            self.Width(data.Width);
            self.Height(data.Height);
            self.TemplateGroups(data.PlateTemplateGroups);
        });
    }

The content of self.TemplateGroups is data.PlateTemplateGroups , that is an array and its content are not observable properties (they are javascript objects). self.TemplateGroups的内容是data.PlateTemplateGroups ,它是一个数组,其内容不是可观察的属性(它们是javascript对象)。

If you want that this objects in this array become observables, you could use the Mapping Plugin : 如果希望此数组中的此对象变为可观察对象,则可以使用Mapping Plugin

self.loadData = function () {
    $.get("/api/PlateTemplate/Get", { id: self.TemplateId() }).done(function (data) {
        self.Name(data.Description);
        self.Width(data.Width);
        self.Height(data.Height);
        self.TemplateGroups(
          ko.mapping.fromJS(     //   <--- new
             data.PlateTemplateGrou‌​ps));
    });
}

This way, all properties become observables. 这样,所有属性都变为可观察的。

If you need to convert this data to a Json format you could use ko.mapping.toJS() : 如果需要将此数据转换为Json格式,可以使用ko.mapping.toJS()

ko.mapping.toJS(self.TemplateGroups)

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

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