-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilterController.js
109 lines (92 loc) · 3.02 KB
/
filterController.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
angular.module("group-filter").controller("filterController", function($scope) {
$scope.selectedFilter = null;
$scope.addedFilters = [];
let grouped = null;
function startList() {
$scope.listCars = [
{ category: "Suv", brand: "Volkswagen", model: "T-Cross" },
{ category: "Suv", brand: "Volkswagen", model: "Tiguan" },
{ category: "Suv", brand: "Chevrolet", model: "Equinox" },
{ category: "Suv", brand: "Chevrolet", model: "Tracker" },
{ category: "Sedan", brand: "Fiat", model: "Cronos" },
{ category: "Sedan", brand: "Volkswagen", model: "Virtus" },
{ category: "Sedan", brand: "Ford", model: "Fusion" },
{ category: "Sedan", brand: "Ford", model: "Ka" },
{ category: "Hatch", brand: "Chevrolet", model: "Onix" },
{ category: "Hatch", brand: "Ford", model: "Ka" },
{ category: "Hatch", brand: "Ford", model: "Fiesta" }
];
}
startList();
$scope.filters = [
{ id: 1, name: "Categoria" },
{ id: 2, name: "Marca" },
{ id: 3, name: "Modelo" }
];
$scope.changeFilter = function() {
switch ($scope.selectedFilter.id) {
case 1:
grouped = groupBy($scope.listCars, car => car.category);
$scope.listItems = Array.from(grouped.keys());
break;
case 2:
grouped = groupBy($scope.listCars, car => car.brand);
$scope.listItems = Array.from(grouped.keys());
break;
case 3:
grouped = groupBy($scope.listCars, car => car.model);
$scope.listItems = Array.from(grouped.keys());
break;
}
};
function groupBy(list, keyGetter) {
const map = new Map();
list.forEach(item => {
const key = keyGetter(item);
const collection = map.get(key);
if (!collection) {
map.set(key, [item]);
} else {
collection.push(item);
}
});
return map;
}
$scope.addFilter = function() {
$scope.filters = $scope.filters.filter(function(value) {
return value.id !== $scope.selectedFilter.id;
});
$scope.addedFilters.push($scope.selectedFilter);
$scope.selectedFilter = null;
filteredSearch();
};
$scope.removeFilter = function(filter) {
$scope.addedFilters.splice($scope.addedFilters.indexOf(filter), 1);
$scope.selectedFilter = null;
delete filter.value;
$scope.filters.push(filter);
filteredSearch();
};
function filteredSearch() {
startList();
$scope.addedFilters.forEach(function(valueFilter) {
switch (valueFilter.id) {
case 1:
$scope.listCars = $scope.listCars.filter(function(valueList) {
return valueList.category === valueFilter.value;
});
break;
case 2:
$scope.listCars = $scope.listCars.filter(function(valueList) {
return valueList.brand === valueFilter.value;
});
break;
case 3:
$scope.listCars = $scope.listCars.filter(function(valueList) {
return valueList.model === valueFilter.value;
});
break;
}
});
}
});