lodash (_.sortBy _.chunk), , , :
https://jsfiddle.net/1LLf7gz6/
, :
$scope.itemssorted = _.sortBy($scope.items, 'price');
:
$scope.itemschunk = _.chunk(_.sortBy($scope.items, 'price'), 4);
jsfiddle (new): https://jsfiddle.net/py5hs1yj/
jsfiddle : https://jsfiddle.net/Ldcpx35w/
:
._ sortBy() → https://lodash.com/docs#sortBy
._chunk() → https://lodash.com/docs#chunk
jsfiddle:
Script:
var app = angular.module('anExample', []);
app.controller('theController', ['$scope',function($scope){
$scope.test = 'this is a test';
$scope.items = [
{ model:"A", price: 100, quantity: 30},
{ model:"B", price: 90, quantity: 20 },
{ model:"C", price: 80, quantity: 200 },
{ model:"D", price: 70, quantity: 20 },
{ model:"E", price: 60, quantity: 100 },
{ model:"F", price: 50, quantity: 70 },
{ model:"G", price: 40, quantity: 230 },
{ model:"H", price: 30, quantity: 50 }
];
//$scope.itemssorted = _.sortBy($scope.items, 'price');
//example: reverse array
//$scope.itemssorted = _.sortBy($scope.items, 'price').reverse();
$scope.itemschunk = _.chunk(_.sortBy($scope.items, 'price'), 4);
console.log("items: ", $scope.items);
console.log("items chunked: ", $scope.itemschunk);
//console.log("items sorted: ", $scope.itemssorted);
}]);
HTML:
<div ng-app="anExample" ng-controller="theController">
Hello, {{test}}!
<ul ng-repeat="group in itemschunk">
<li ng-repeat="items in group"> Model: {{items.model}}, price: {{items.price}}, quantity: {{items.quantity}}</li>
</ul>
</div>
:
Hello, this is a test!
Model: H, price: 30, quantity: 50
Model: G, price: 40, quantity: 230
Model: F, price: 50, quantity: 70
Model: E, price: 60, quantity: 100
Model: D, price: 70, quantity: 20
Model: C, price: 80, quantity: 200
Model: B, price: 90, quantity: 20
Model: A, price: 100, quantity: 30