Knockout.js向数组添加新项

时间:2012-07-31 10:08:11

标签: javascript knockout.js

我无法以编程方式将元素添加到数据表中包含的可观察数组中。

表格的每一行都有一个我希望影响的不同数组。

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>    

数据和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));​

我遇到的问题是,在单击“添加”按钮并输入值后,数组中的值为空字符串(我在创建新数组元素时添加的值)。

我认为问题在于我在获取值之前将新项添加到数组中,但我认为它将绑定到新的inpute,因此当我执行Save函数时,新值将被反映出来。

以上的JsFiddle位于http://jsfiddle.net/amcgoldrick/3h9GZ/

由于

安迪

1 个答案:

答案 0 :(得分:3)

您添加到ids observableArray的值无法观察到,因此绑定只是一种方式。如果要更新observableArray内的值,则需要在observableArray可观察的内部创建元素。

所以而不是:

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

你会:

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

有关工作示例,请参阅http://jsfiddle.net/5wKjk/3/。 (我没有添加代码来映射您现有的ID,因为我只是想证明所涉及的原则并且没有时间)。