What would be the best way to stripe a list with KnockoutJS? The class on the div below should be even or odd depending where it is in the list, and update when adding or removi
One easy way to do this is to add a computed observable that adds an index to each element, e.g.
self.logLines = ko.observable(logLinesInput);
self.enhancedLogLines = ko.computed(function() {
var res = [];
$.each(self.logLines(), function(index, ll) {
res.push(new LogLine(index, ll));
});
return res;
}, self);
In my case LogLine()
creates an object with an index field and the other fields that were in the original object.
Now you can easily add zebra stripes to your output:
- 热议问题