AngularJS: Can I use a filter to chunk an array in ng-repeat?

前端 未结 3 1182
失恋的感觉
失恋的感觉 2021-01-21 01:14

Edit to add a clear question: I have a flat array of some length and I want to put it into a tr/td type view? This might also be in a bootstrap grid or somethin

3条回答
  •  走了就别回头了
    2021-01-21 01:44

    maybe this could work to someone, you never know

    the following code will assign an extra value (store_chunk) to an array of stores. so I can use with ng-repeat to show 3 different columns in my HTML

            var x               = 0;
            var y               = 1;
            var how_many_chunks = 3;
            var limit = $scope.main.stores.length / how_many_chunks ;
            angular.forEach($scope.main.stores, function(e, key) {
                if (x <= limit) {
                    $scope.main.stores[key].store_chunk = y;
                }
                else{
                    y    += 1;
                    limit = y * limit;
                    $scope.main.stores[key].store_chunk = y;
                }
                x += 1;
            });
    

    here the HTML

    • {{store.store_name}}
    • {{store.store_name}}
    • {{store.store_name}}

    this works like a charm! and don't vote down just because you didn't like it!.. thumbs up instead!

提交回复
热议问题