-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
53 lines (42 loc) · 1.59 KB
/
app.js
File metadata and controls
53 lines (42 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'use strict';
require('angular/angular');
var app = angular.module('app',[]);
app.factory('dataService', function(){
var service = {};
service.data = require('./data');
return service;
});
app.controller('MainController', ['$scope', 'filterFilter','dataService' ,function($scope, filterFilter, dataService){
$scope.data = dataService.data;
$scope.max = 6; //max number of results per page.
$scope.index = 1; //current page number
$scope.filteredItems = $scope.data; //data after it has been filtered, but will start unfiltered
$scope.pagesCount = Math.ceil($scope.filteredItems.length/$scope.max); //number of pages
$scope.len = $scope.filteredItems.length; //length of the filtered data array.
//watches for changes in search input, and re-filters the array accordingly,
//while also reseting page number and total number of pages.
$scope.$watch('search', function(){
$scope.index = 1
$scope.filteredItems = filterFilter($scope.data, $scope.search);
if($scope.search.length==0) {
filteredItems = $scope.data;
}
$scope.len = $scope.filteredItems.length;
$scope.pagesCount = Math.ceil($scope.filteredItems.length/$scope.max)
});
//moves to next page of results.
$scope.nextPage = function(){
$scope.index = $scope.index + 1;
}
//moves back to previous page of results.
$scope.prevPage = function(){
$scope.index = $scope.index - 1;
}
}]);
//custom filter to help with pagination
app.filter('startAtIndex', function(){
return function(input, index){
index = +index;
return input.slice(index);
}
});