我如何在Knockout中完成这样的事情?鉴于来源是这样的[“一”,“两”,“三”]
<div>
<p>
<div>
<div>One</div>
<div>Two</div>
<div>Three</div>
</div>
<div>
<div>Four</div>
<div>Five</div>
<div>Six</div>
</div>
</p>
</div>
答案 0 :(得分:0)
<div data-bind="foreach:someArrayContainingTheValues">
<div data-bind="text:$data"></div>
</div>
试试上面的
答案 1 :(得分:0)
我创建了一个JSFiddle来展示如何实现这一目标:http://jsfiddle.net/bxfXd/3509/
<强>标记:强>
<div class="outer" data-bind="foreach:itemsGrouped">
<div class="inner">
<!-- ko foreach: $data -->
<div class="main" data-bind="text:$data"></div>
<!-- /ko -->
<div>
</div>
<强> JS:强>
Array.prototype.chunk = function (chunkSize) {
var array = this;
return [].concat.apply([],
array.map(function (elem, i) {
return i % chunkSize ? [] : [array.slice(i, i + chunkSize)];
}));
}
var SimpleListModel = function(items) {
var self = this;
self.items = ko.observableArray(items);
self.itemsGrouped = ko.computed(function () {
return self.items().chunk(3);
});
};
ko.applyBindings(new SimpleListModel(
["One", "Two", "Three", "Four", "Five", "Six"]));
作为参考,块方法取自here,由@ninjagecko
发布