To handle a situation like this, I would probably push the logic into your view model, so that your view can remain simple. So the idea would be to use a dependentObservable to represent your "rows". Then, your view can just foreach through the rows and then foreach through the cells in your row.
Here is a sample that makes that number of columns an observable, so that it can be dynamically updated. http://jsfiddle.net/rniemeyer/9TN9W/
var viewModel = {
items: ko.observableArray(),
columnLength: ko.observable(5)
};
//sample data
for (var i = 0; i < 100; i++) {
viewModel.items.push({ name: 'test' + i });
}
//return an array of rows. Each row is an array of items
viewModel.rows = ko.dependentObservable(function() {
var result = [],
colLength = parseInt(this.columnLength(), 10),
row;
//loop through items and push each item to a row array that gets pushed to the final result
for (var i = 0, j = this.items().length; i < j; i++) {
if (i % colLength === 0) {
if (row) {
result.push(row);
}
row = [];
}
row.push(this.items()[i]);
}
//push the final row
if (row) {
result.push(row);
}
return result;
}, viewModel);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…