简体   繁体   English

Knockout.js向数组添加新项目

[英]Knockout.js adding new items to array

I am having trouble being able to programatically add elements to an observable array contained within a table of data. 我无法以编程方式将元素添加到数据表中包含的可观察数组中。

Each row of the table has a different array that I wish to affect. 表格的每一行都有一个我希望影响的数组。

The HTML looks like this: HTML看起来像这样:

<div class='liveExample'>     
<h2>Cases</h2>
<div id='contactsList'>
    <table class='contactsEditor'>
        <tr>
            <th>Case Number</th>
            <th>Stage</th>
            <th>Users</th>
            <th>Ids</th>
        </tr>
        <tbody data-bind="foreach: appearances ">
            <tr>
                <td>
                    <input data-bind='value: caseNumber' />
                </td>
                <td><input data-bind='value: caseStage' /></td>
                <td> 
                    <ul  data-bind="foreach: subjects">
                        <li style="list-style-type:none;"><label data-bind="text: Name"/></li>    
                    </ul>   
                </td>
                <td>
                    <table>
                        <tbody data-bind="foreach: ids">
                            <tr>
                                <td><input data-bind='value: $data' /></td>
                                <td><a href='#' data-bind='click: $root.removeId'>Delete</a></td>
                            </tr>
                        </tbody>
                    </table>
                    <a href='#' data-bind='click: $root.addId'>Add number</a>
                </td>
            </tr>
        </tbody>
    </table>
</div>     
<p>
    <button data-bind='click: save, enable:appearances().length > 0'>Save to JSON</button>
</p>     
<textarea data-bind='value: lastSavedJson' rows='5' cols='60' disabled='disabled'> </textarea>    

The data and JavaScript looks like this: 数据和JavaScript如下所示:

enter code here
var initialData = [{
"Number": "12000009",
"Stage": "Inital",
"Subjects": [{
    "Name": "Andrew McAdam"},
{
    "Name": "Patrick Brown"}],
"Ids": [1]},
{
"Number": "12000010",
"Stage": "Inital",
"Subjects": [{
    "Name": "John Smith"}],
"Ids": [2, 3]},
{
"Number": "12000011",
"Stage": "Inital",
"Subjects": [{
    "Name": "James Bonner"}],
"Ids": [4]},
{
"Number": "12000012",
"Stage": "Processed",
"Subjects": [{
    "Name": "Henrik Dalgleish"}],
"Ids": [5]}]

var AppearancesModel = function(appearances) {
var self = this;
self.appearances = ko.observableArray(ko.utils.arrayMap(appearances, function(appearance) {
    return {
        caseNumber: appearance.Number,
        caseStage: appearance.Stage,
        subjects: ko.observableArray(appearance.Subjects),
        ids: ko.observableArray(appearance.Ids)
    };
}));

self.addId = function(appearance) {
    appearance.ids.push("");
};

self.removeId = function(id) {
    $.each(self.appearances(), function() {
        this.ids.remove(id)
    })
};

self.save = function() {
    self.lastSavedJson(JSON.stringify(ko.toJS(self.appearances), null, 2));
};

self.lastSavedJson = ko.observable("")
}

ko.applyBindings(new AppearancesModel(initialData));​

The problem I have is that after the clicking the Add button and entering a value the value in the array is empty string (the value I added when creating the new array element). 我的问题是单击添加按钮并输入值后,数组中的值是空字符串(创建新数组元素时添加的值)。

I think the issue is that I am adding the new item to the array before I have a value, but I thought that it would be bound to the new inpute so when I executed the Save function the new value would be reflected. 我认为问题在于我要在有值之前将新项添加到数组中,但是我认为它将绑定到新输入,因此当我执行Save函数时,新值将得到反映。

The JsFiddle of the above is at http://jsfiddle.net/amcgoldrick/3h9GZ/ 上面的JsFiddle位于http://jsfiddle.net/amcgoldrick/3h9GZ/

Thanks 谢谢

Andy 安迪

The values that you add to your ids observableArray are not observable therefore the binding is only one way. 您添加到ids observableArray不可观察,因此绑定只是一种方法。 If you want to update the value inside the observableArray then you need to make the elements inside the observableArray observable. 如果要更新observableArray内部的observableArray则需要使observableArray内部的元素observableArray观察。

So instead of: 所以代替:

self.addId = function(appearance) {
    appearance.ids.push("");
};

You would have: 你将会拥有:

self.addId = function(appearance) {
    appearance.ids.push({id: ko.observable("")});
};

See http://jsfiddle.net/5wKjk/3/ for a working example. 有关工作示例,请参见http://jsfiddle.net/5wKjk/3/ (I've not added code to map your existing ids as I just wanted to demonstrate the principles involved and didn't have time). (我只是想演示所涉及的原理并且没有时间,所以我没有添加代码来映射您现有的ID)。

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

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