How to get Knockout to group foreach

前端 未结 1 704
粉色の甜心
粉色の甜心 2020-12-16 03:34

I can get my records to repeat using foreach, but because I\'m using a grid system for CSS, I want to group these records four at a time (div class=\"column\") for each (div

相关标签:
1条回答
  • 2020-12-16 03:42

    So I'm not entirely sure what you are after but you could you group manually like this.

    http://jsfiddle.net/madcapnmckay/hFPgT/1/

    <div data-bind="foreach: grouped" >
        <div data-bind="foreach: $data" class="row">
            <div class="column" data-bind="text: text"></div>
        </div>
    </div>    
    
    this.grouped = ko.computed(function () {
            var rows = [], current = [];
            rows.push(current);
            for (var i = 0; i < this.items.length; i += 1) {
                current.push(this.items[i]);
                if (((i + 1) % 4) === 0) {
                    current = [];
                    rows.push(current);
                }
            }
            return rows;
    }, this);
    

    Hope this helps.

    0 讨论(0)
提交回复
热议问题