简体   繁体   English

Knockout.js如何为可观察数组创建唯一字段值

[英]Knockout.js How to create a unique field value for an observable array

I have an observableArray and I would like to loop through to achieve a table of fields and in which the user should be able to dynamically add the rows. 我有一个observableArray ,我想循环遍历以获取一个字段表,并且用户应该可以在其中动态添加行。 Here is an image of what I would like to accomplish: 这是我想要完成的图像:

在此处输入图片说明

Note in the above, whenever I fill the form it updates/mutates to the other fields although I have my field name made unique. 请注意,尽管我将字段名称设为唯一,但无论何时填写表格,它都会更新/更改为其他字段。

<div class="spacer-xxs">
<records data-bind="foreach: $data.multi_records">
    <record>
        <h5 data-bind="text: $data.title"></h5>
        <table class="table table-condensed" data-bind="if: $data.fields().length > 0">
            <thead>
            <tr data-bind="foreach: $data.fields_schema">
                <th data-bind="text: $data.title"></th>
            </tr>
            </thead>
            <tbody data-bind="foreach: $data.fields">
            <tr data-bind="foreach: $parent.fields_schema">
                <td>
                    <input class="form-control input-sm"
                           data-bind="attr: {'name': $data.name, 'id': $index()}, value: $data.value">
                </td>
                <!-- ko if: $index() == $parent.length-1 -->
                <td>
                    <a href="#" class="error" data-bind="click: $root.removeRow.bind($data, $parent)"><i
                            class="fa fa-times-circle"></i> </a>
                </td>
                <!-- /ko -->
            </tr>
            </tbody>
        </table>
        <div class="row">
            <div class="col-xs-12">
                <button class="btn btn-xs btn-inverse" data-bind="click: $root.addTableRow.bind($data, $parent)">
                    <i class="fa fa-plus"></i> Add item
                </button>
            </div>
        </div>
    </record>
</records>

Here is how the fields are generated for the observableArray : 这是为observableArray生成字段的方式:

var field = function (field) {
            this.name = ko.observable(field.name).extend({required: field.required === true});
            this.title = ko.observable(field.title);
            this.type = ko.observable(field.type);
            this.help_text = ko.observable(field.help_text);
            this.value = ko.observable(field.value).extend({required: field.required === true});

            //Add validation group
            self.errors.push(ko.validation.group(this));
  };

section.fields = [{...}, ] //Contains fields array from the database.

this.fields = ko.observableArray(ko.utils.arrayMap(section.fields, function (field_item) {
      return new field(field_item);
}));

Additional information . 附加信息

Here is a schema data from where I'd like to create my multi records form from. 这是我要从中创建多记录表单的模式数据。 So a given page will have several form sections with single fields and multi-record fields such as the products list below. 因此,给定的页面将具有几个带有单个字段和多个记录字段的表单部分,例如下面的产品列表。 For multi records schema, I should be able to add more rows to it. 对于多记录架构,我应该能够向其中添加更多行。

[{"title":"Untitled section","fields":[],"multi_records":[]},{"title":"Section 1","fields":[{"title":"Field 1","type":"text","required":true,"value":"1000","help_text":"Default is 1000","name":"field_1"},{"title":"Field 2","type":"text","required":true,"name":"field_2"}],"multi_records":[{"title":"Products","fields":[{"title":"Name","type":"text","name":"name"},{"title":"Qty","type":"number","name":"qty"},{"title":"Price","type":"number","name":"price"},{"title":"Total","type":"formula","value":"MULT(price,qty)","name":"total"}]}]}]

Here's the section of interest: 这是感兴趣的部分:

<tbody data-bind="foreach: $data.fields">
<tr data-bind="foreach: $parent.fields_schema">
    <td>
        <input class="form-control input-sm"
               data-bind="attr: {'name': $data.name, 'id': $index()}, value: $data.value">
    </td>

$data.fields is the outer loop, so there is one row for each entry in it. $data.fields是外部循环,因此其中的每个条目都有一行。 $parent.fields_schema provides the $data for the columns in each row. $parent.fields_schema为每一行中的列提供$data You bind every element of a column to the same thing, fields_schema[$index].value . 您将列的每个元素绑定到相同的fields_schema[$index].value

You are going to need to provide an alias for the outer loop so that you can refer to the row's data and not just the schema column specs. 您将需要为外循环提供一个别名 ,以便您可以引用行的数据,而不仅仅是模式列规范。 That will look something like: 看起来像:

<tbody data-bind="foreach: {data: fields, as:row}">
<tr data-bind="foreach: $parent.fields_schema">
    <td>
        <input class="form-control input-sm"
               data-bind="attr: {'name': name}, value: row[name()]">
    </td>

(I removed references to $data , as they are unnecessary.) row refers to the field object for the current row, and it is indexed (I think) by name from the schema. (我删除了对$data引用,因为它们是不必要的。) row引用当前行的field对象,并通过模式中的name对其进行索引(我认为)。

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

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