Add striped styling to a list of items

前端 未结 6 982
执笔经年
执笔经年 2021-02-01 17:43

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

6条回答
  •  遇见更好的自我
    2021-02-01 18:05

    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:

                
    

提交回复
热议问题