简体   繁体   English

从淘汰赛可观察阵列中移除物品

[英]Remove items from Knockout observable array

I have the below structure for knockout model. 我具有以下结构的剔除模型。 It contains an observable array which in turn contains an object. 它包含一个可观察的数组,该数组又包含一个对象。

   function ViewModel() {
    var self = this;

    self.newItem = ko.observable({        
        manufacturer: ko.observable(),
        itemnumber: ko.observable(),
        itemDescription: ko.observable()    

    });
    self.AllItems = ko.observableArray();      

    self.addItem = function() { 
     self.newItem().manufacturer("test");    
     self.newItem().itemDescription("data");

    self.AllItems.push(self.newItem); 

    };
    self.removeItem = function(data) { 
        self.AllItems.remove(data);
    };
}

First issue:Through this script I am entering a new itemnumber in the textbox and then clicking on add item to have the new item with the itemnumber from the textbox added to the observable array but when I change the item number and hit add it changes all the itemnumber inside the array. 第一个问题:通过此脚本,我在文本框中输入新的项目编号,然后单击添加项目以将具有文本框中的项目编号的新项目添加到可观察数组中,但是当我更改项目编号并单击添加时,它将全部更改数组内的itemnumber。 How can i have unique data inside the array. 我怎样才能在数组内有唯一的数据。

Second issue: I need to remove the specific items from the array but it's not deleting it. 第二个问题:我需要从数组中删除特定项目,但并没有删除它。 Can someone please tell me how I can delete items from the observable array based on say the itemnumber property. 有人可以告诉我如何根据itemnumber属性从可观察数组中删除项目。

<input type="text" data-bind="value: newItem().itemnumber"/>
<div>
    Items: <button data-bind="click: addItem">Add Item</button>
</div>
<div>
    <table>
        <tbody data-bind="template: { name: 'itemTemplate', foreach: AllItems }"></tbody>
    </table>
</div>
<script type="text/html" id="itemTemplate">
    <tr>
        <td>
            <input data-bind="value: itemnumber" />
            <a href="#" data-bind="click: $parent.removeItem">Remove Item</a>
        </td>
    </tr>
</script>

I have created this fiddle for quick view of the issue. 我创建这个小提琴是为了快速查看问题。 Just started learning knockout so any help is appreciated. 刚开始学习淘汰赛,所以我们将不胜感激。

http://jsfiddle.net/N3JaW/138/ http://jsfiddle.net/N3JaW/138/

Try the following for adding new item, which will solve your first issue:- 尝试以下操作添加新项,这将解决您的第一个问题:-

HTML code HTML代码

<input type="text" id="textBox" data-bind="value : textBoxVal"/>
<div>
    Items: <button data-bind="click: addItem">Add Item</button>
</div>
<div>
<table>
    <tbody data-bind="template: { name: 'itemTemplate', foreach: AllItems }"></tbody>
</table>
</div>
<script type="text/html" id="itemTemplate">
<tr>
    <td>
        <input data-bind="value: itemnumber" />
        <a href="#" data-bind="click: $parent.removeItem">Remove Item</a>
    </td>
</tr>
</script>

JS code:- JS代码:-

function ViewModel() {
var self = this;

self.newItem = ko.observable({        
    manufacturer: "",
    itemnumber: "",
    itemDescription: ""   

});

 self.textBoxVal = ko.observable();
 self.AllItems = ko.observableArray();      

self.addItem = function() { 
 self.newItem().manufacturer= "test"; 
 self.newItem().itemDescription= "data";
 self.newItem().itemnumber = self.textBoxVal();

self.AllItems.push(self.newItem); 

};
self.removeItem = function(data) { 
    self.AllItems.remove(data);
};
}
$(document).ready(function() {ko.applyBindings(new ViewModel()); });

Your first issue was because, each time you are trying to add a new item, you were changing the value of itemNumber , which is an observable. 您的第一个问题是因为,每次尝试添加新项目时,您都在更改itemNumber的值,这是可以观察到的。

Observable value will be changed every where it is binded, when it's value is changed. 当可观察值的值更改时,它在绑定的每个位置都会更改。

Instead you need to create new object and do push into the observableArray. 相反,您需要创建新对象并将其推入observableArray。

Refer doc to know more about observableArray. 请参阅doc,以了解有关observableArray的更多信息。

For your second problem change removeItem as given below:- 对于第二个问题,更改removeItem ,如下所示:

 self.removeItem = function(data) {
var dtIndex = self.AllItems.indexOf(data); //Get the index of the object you want to remove.
    self.AllItems.splice(dtIndex, 1); //Then do splice
};

You can refer the above doc, to know how to use splice . 您可以参考上面的文档,以了解如何使用splice

EDIT based on the suggestion in the comment :- 根据评论中的建议进行编辑:-

For working code of edited answer click here . 有关已编辑答案的工作代码, 请单击此处

Hope this will solve your problem. 希望这能解决您的问题。

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

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