KnockoutJS:从 JavaScript 模板中访问数组中项目的索引 [英] KnockoutJS: Access index of item in array from within the JavaScript template
问题描述
我使用 KnockoutJS 从数组中填充列表:
<input type="text" data-bind="value: myText"/>函数视图模型(){self.list = ko.observableArray([新列表项(示例文本")]);};功能列表项(文本){this.myText = 文本;};
我可以像这样为我的输入的各个实例分配一个 id
<input data-bind="attr: { id: $index } ...
如何从我的 listItem 函数中访问该索引?我希望能够做类似的事情
function listItem (text) {this.myText = 文本;this.index = $index;};
为了将其用于进一步处理.
您可以创建一个自定义绑定,将您的属性设置为索引,它看起来像:
ko.bindingHandlers.setIndex = {初始化:函数(元素,valueAccessor,allBindings,数据,上下文){var prop = valueAccessor();数据[prop] = context.$index;}};
这假设您正在处理数组中的对象.你会像这样使用它:
<li data-bind="setIndex: 'myIndex', text: name"></li>
因此,这会将 $index
observable 复制到具有您指定的属性名称的对象上.示例:http://jsfiddle.net/rniemeyer/zGmcg/
在绑定之外执行此操作的另一种方法(这是我在 $index
之前使用的方法)是订阅对 observableArray 的更改并每次重新填充索引.>
observableArray 的扩展如下所示:
//跟踪 observableArray 中项目的索引ko.observableArray.fn.indexed = 函数(道具){道具 = 道具 ||'指数';//每当数组更改时,进行一个循环以更新每个数组的索引this.subscribe(function(newValue) {如果(新值){变量项目;for (var i = 0, j = newValue.length; i < j; i++) {item = newValue[i];如果 (!ko.isObservable(item[prop])) {item[prop] = ko.observable();}项目[道具](i);}}});//初始化索引this.valueHasMutated();返回这个;};
然后你会像这样使用它:
this.myItems = ko.observableArray().indexed('myIndexProp');
这是一个示例:http://jsfiddle.net/rniemeyer/bQD2C/
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 processing.
You could create a custom binding that sets your property to the index, it would look something like:
ko.bindingHandlers.setIndex = {
init: function(element, valueAccessor, allBindings, data, context) {
var prop = valueAccessor();
data[prop] = context.$index;
}
};
This assumes that you are dealing with objects in your array. You would use it like:
<ul data-bind="foreach: items">
<li data-bind="setIndex: 'myIndex', text: name"></li>
</ul>
So, this copies the $index
observable on to your object with the property name that you specify. Sample: http://jsfiddle.net/rniemeyer/zGmcg/
Another way that you can do this outside of bindings (this is how I used to do it before $index
) is to subscribe to changes to the observableArray and repopulate an index each time.
Here is what an extension to an observableArray might look like:
//track an index on items in an observableArray
ko.observableArray.fn.indexed = function(prop) {
prop = prop || 'index';
//whenever the array changes, make one loop to update the index on each
this.subscribe(function(newValue) {
if (newValue) {
var item;
for (var i = 0, j = newValue.length; i < j; i++) {
item = newValue[i];
if (!ko.isObservable(item[prop])) {
item[prop] = ko.observable();
}
item[prop](i);
}
}
});
//initialize the index
this.valueHasMutated();
return this;
};
You would then use it like:
this.myItems = ko.observableArray().indexed('myIndexProp');
Here is a sample: http://jsfiddle.net/rniemeyer/bQD2C/
这篇关于KnockoutJS:从 JavaScript 模板中访问数组中项目的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!