ko.observablearray

KnockoutJS: Access index of item in array from within the JavaScript template

我的梦境 提交于 2019-11-30 16:12:30
I populate a list from an array using KnockoutJS: <div data-bind:"foreach: list"> <input type="text" data-bind="value: myText" /> </div> function ViewModel() { self.list = ko.observableArray([ new listItem("sample text") ]); }; function listItem (text) { this.myText = text; }; I can assign an id to the individual instances of my input like so <input data-bind="attr: { id: $index } ... How do I access this index from within my listItem function? I want to be able to do something like function listItem (text) { this.myText = text; this.index = $index; }; in order to use this for further

KnockoutJS: Access index of item in array from within the JavaScript template

吃可爱长大的小学妹 提交于 2019-11-30 00:22:13
问题 I populate a list from an array using KnockoutJS: <div data-bind:"foreach: list"> <input type="text" data-bind="value: myText" /> </div> function ViewModel() { self.list = ko.observableArray([ new listItem("sample text") ]); }; function listItem (text) { this.myText = text; }; I can assign an id to the individual instances of my input like so <input data-bind="attr: { id: $index } ... How do I access this index from within my listItem function? I want to be able to do something like function

KnockoutJS - Observable Array of Observable objects

不问归期 提交于 2019-11-28 08:04:52
I would like to display an editable list of items, each item of which is editable (kind of like an editable grid, in a way). I am using KnockoutJS. I cannot use just a simple Observable Array because, as the documentation states "An observableArray tracks which objects are in the array, not the state of those objects" So, I have created an observableArray of observable objects (using utils.arrayMap), and bound them to the view. However, the problem is that if I edit the data on screen, any data-entry changes that the user makes on screen do not appear to take effect. See http://jsfiddle.net

KnockoutJS - Observable Array of Observable objects

笑着哭i 提交于 2019-11-27 02:04:09
问题 I would like to display an editable list of items, each item of which is editable (kind of like an editable grid, in a way). I am using KnockoutJS. I cannot use just a simple Observable Array because, as the documentation states "An observableArray tracks which objects are in the array, not the state of those objects" So, I have created an observableArray of observable objects (using utils.arrayMap), and bound them to the view. However, the problem is that if I edit the data on screen, any