Skip to content

Instantly share code, notes, and snippets.

@Kjaer
Last active August 29, 2015 14:09
Show Gist options
  • Save Kjaer/feda24156e23e4cc9276 to your computer and use it in GitHub Desktop.
Save Kjaer/feda24156e23e4cc9276 to your computer and use it in GitHub Desktop.
Angular.js Divide List with ng-repeat
var myApp = angular.module('myApp', []);
myApp.controller('MyCtrl', function ($scope) {
$scope.parts = [{"Id":7204, "Name":"Air Cleaner", "ProductCount":3452},
{"Id":7205, "Name":"Battery", "ProductCount":76},
{"Id":7206, "Name":"Choke", "ProductCount":342},
{"Id":7207, "Name":"Clutch Lever", "ProductCount":98},
{"Id":7208, "Name":"Crankcase Cover", "ProductCount":0},
{"Id":7209, "Name":"Crash Bar", "ProductCount":456},
{"Id":7210, "Name":"Cylinder", "ProductCount":184},
{"Id":7211, "Name":"Cylinder Head", "ProductCount":345},
{"Id":7212, "Name":"Engine Stop Switch (KILL)", "ProductCount":87},
{"Id":7213, "Name":"Foot Pegs", "ProductCount":98},
{"Id":7214, "Name":"Front Brake Lever", "ProductCount":324},
{"Id":7215, "Name":"Front Disc Brakes", "ProductCount":897},
{"Id":7216, "Name":"Front Master Cylinder", "ProductCount":45},
{"Id":7217, "Name":"Front Suspension", "ProductCount":76},
{"Id":7218, "Name":"Fuel Petcock Valve", "ProductCount":32},
{"Id":7219, "Name":"Gas Cap", "ProductCount":98},
{"Id":7220, "Name":"Gas Tank", "ProductCount":123},
{"Id":7222, "Name":"Gear Shift", "ProductCount":342},
{"Id":7223, "Name":"Headlight Switch", "ProductCount":5},
{"Id":7224, "Name":"Highway Peg", "ProductCount":876},
{"Id":7225, "Name":"Horn", "ProductCount":453},
{"Id":7226, "Name":"Horn Button", "ProductCount":234},
{"Id":7227, "Name":"Ignition Switch", "ProductCount":76},
{"Id":7228, "Name":"Left Side Mirror", "ProductCount":986},
{"Id":7229, "Name":"Muffler", "ProductCount":43},
{"Id":7230, "Name":"Oil Filter", "ProductCount":678},
{"Id":7231, "Name":"Oil Tank", "ProductCount":90},
{"Id":7232, "Name":"Passenger Back Rest", "ProductCount":24},
{"Id":7233, "Name":"Passenger Pegs", "ProductCount":123},
{"Id":7234, "Name":"Pillion or Passenger Seat", "ProductCount":9708},
{"Id":7235, "Name":"Primary Chain Cover", "ProductCount":435},
{"Id":7236, "Name":"Rear Disc Brakes", "ProductCount":345},
{"Id":7237, "Name":"Rear Foot Brake Pedal", "ProductCount":578},
{"Id":7238, "Name":"Rear Master Cylinder", "ProductCount":39},
{"Id":7239, "Name":"Right Side Mirror", "ProductCount":6},
{"Id":7240, "Name":"Rocker Box", "ProductCount":4087},
{"Id":7241, "Name":"Seat", "ProductCount":134},
{"Id":7242, "Name":"Shock Absorber", "ProductCount":0698},
{"Id":7243, "Name":"Side Stand", "ProductCount":436},
{"Id":7244, "Name":"Spark Plug", "ProductCount":325},
{"Id":7245, "Name":"Speedometer", "ProductCount":76},
{"Id":7246, "Name":"Starter Switch", "ProductCount":345},
{"Id":7247, "Name":"Tachometer", "ProductCount":234},
{"Id":7248, "Name":"Tail Light", "ProductCount":658},
{"Id":7249, "Name":"Throttle", "ProductCount":98},
{"Id":7250, "Name":"Turn Signal", "ProductCount":2},
{"Id":7251, "Name":"Turn Signal Switch", "ProductCount":67},
{"Id":7252, "Name":"Warning Lights/Neutral Light", "ProductCount":879},
{"Id":7253, "Name":"Windshield", "ProductCount":98}];
$scope.group = $scope.group % 15 != 0 ? Math.ceil($scope.parts.length / 15) : parseInt($scope.parts.length / 15);
$scope.getTimes = function(num){
return new Array(num);
}
});
myApp.filter("slice",function(){
return function(arr, start, end) {
return arr.slice(start, end);
};
});
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type='text/css'>
div{
width:200px;
float:left;
margin-bottom:10px;
}
</style>
<script type='text/javascript' src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.0/angular.js"></script>
</head>
<body ng-app="myApp">
<section ng-controller="MyCtrl">
{{araryObje.length}}
<div ng-repeat="i in getTimes(group) track by $index">
{{$index}}
<p ng-repeat="part in parts | slice:($index*15):($index*15)+15">
{{part.Id}} {{part.Name}}
</p>
</div>
</section>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment