Skip to content

Commit 5958611

Browse files
committed
#57 Fixed the issue with filtering after sort applied
1 parent a4962d0 commit 5958611

File tree

3 files changed

+129
-43
lines changed

3 files changed

+129
-43
lines changed

Diff for: dist/dataGrid.js

+64-21
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,16 @@
3636
$scope.sortOptions = $scope._gridOptions.sort ? angular.copy($scope._gridOptions.sort) : {};
3737
$scope.customFilters = $scope._gridOptions.customFilters ? angular.copy($scope._gridOptions.customFilters) : {};
3838
$scope.urlSync = $scope._gridOptions.urlSync;
39+
$scope.allFilters = [];
40+
$scope.dropDownFilters = [];
41+
3942

4043
$scope.$watchCollection('_gridOptions.data', function (newValue) {
4144
if (newValue && newValue.length > -1) {
4245
$scope.sortCache = {};
4346
$scope.filtered = $scope._gridOptions.data.slice();
44-
$scope.filters.forEach(function (filter) {
45-
if (filter.filterType === 'select') {
46-
$scope[filter.model + 'Options'] = generateOptions($scope.filtered, filter.filterBy);
47-
}
47+
$scope.dropDownFilters.forEach(function (filter) {
48+
$scope[filter.model + 'Options'] = generateOptions($scope.filtered, filter.filterBy);
4849
});
4950

5051
if ($scope.urlSync) {
@@ -116,7 +117,7 @@
116117
}
117118

118119
//custom filters
119-
$scope.filters.forEach(function (filter) {
120+
$scope.allFilters.forEach(function (filter) {
120121
var urlName = filter.model,
121122
value = filter.isInScope ? $scope.$eval(urlName) : $scope.$parent.$eval(urlName);
122123

@@ -160,7 +161,7 @@
160161
});
161162

162163
//custom filters
163-
$scope.filters.forEach(function (filter) {
164+
$scope.allFilters.forEach(function (filter) {
164165
var urlName = filter.model,
165166
value = customParams[urlName];
166167

@@ -246,35 +247,41 @@
246247
//TO REMOVE ?
247248
$scope._time = {};
248249

249-
if ($scope.sortOptions.predicate && $scope.sortCache && $scope.sortCache.predicate === $scope.sortOptions.predicate
250-
&& $scope.sortCache.direction === $scope.sortOptions.direction) {
250+
applyDropDownFilters();
251+
252+
/* if grid already sorted we need to use sort cache instead of the whole data array */
253+
if (isGridSorted()) {
251254
$scope.filtered = $scope.sortCache.data.slice();
252255
sorted = true;
253-
} else {
254-
$scope.filtered = $scope._gridOptions.data.slice();
255256
}
256257

257258
$scope._time.copy = Date.now() - time;
258259
var time2 = Date.now();
259-
applyCustomFilters();
260+
filterData($scope.textAndDateFilters);
260261
$scope._time.filters = Date.now() - time2;
261262
var time3 = Date.now();
262263

263264
if ($scope.sortOptions.predicate && !sorted) {
264265
$scope.filtered = $filter('orderBy')($scope.filtered, $scope.sortOptions.predicate, $scope.sortOptions.direction === 'desc');
265-
$scope.sortCache = {
266-
data: $scope.filtered.slice(),
267-
predicate: $scope.sortOptions.predicate,
268-
direction: $scope.sortOptions.direction
269-
}
266+
generateSortCache();
270267
}
271268
$scope._time.sort = Date.now() - time3;
272269
$scope._time.all = Date.now() - time;
273270
$scope.paginationOptions.totalItems = $scope.filtered.length;
274271
}
275272

276-
function applyCustomFilters() {
277-
$scope.filters.forEach(function (filter) {
273+
function applyDropDownFilters() {
274+
$scope.filtered = $scope._gridOptions.data.slice();
275+
filterData($scope.dropDownFilters);
276+
/* needed to check if grid already sorted, restore sort and update sortCache */
277+
if (isGridSorted()) {
278+
$scope.filtered = $filter('orderBy')($scope.filtered, $scope.sortOptions.predicate, $scope.sortOptions.direction === 'desc');
279+
generateSortCache();
280+
}
281+
}
282+
283+
function filterData(filters) {
284+
filters.forEach(function (filter) {
278285
var predicate = filter.filterBy,
279286
urlName = filter.model,
280287
value = filter.isInScope ? $scope.$eval(urlName) : $scope.$parent.$eval(urlName),
@@ -289,6 +296,19 @@
289296
}
290297
});
291298
}
299+
300+
function isGridSorted() {
301+
return $scope.sortOptions.predicate && $scope.sortCache && $scope.sortCache.predicate === $scope.sortOptions.predicate
302+
&& $scope.sortCache.direction === $scope.sortOptions.direction;
303+
}
304+
305+
function generateSortCache() {
306+
$scope.sortCache = {
307+
data: $scope.filtered.slice(),
308+
predicate: $scope.sortOptions.predicate,
309+
direction: $scope.sortOptions.direction
310+
}
311+
}
292312
}])
293313
.directive('gridItem', ['$compile', function ($compile) {
294314
return {
@@ -315,7 +335,9 @@
315335
scope: true,
316336
controller: 'gridController',
317337
link: function ($scope, $element, attrs) {
318-
var filters = [],
338+
var allFilters = [],
339+
textAndDateFilters = [],
340+
dropDownFilters = [],
319341
directiveElement = $element.parent(),
320342
gridId = attrs.id,
321343
serverPagination = attrs.serverPagination === 'true';
@@ -362,16 +384,37 @@
362384
//$compile(element)($scope);
363385
}
364386
//$compile(element)($scope);
365-
filters.push({
387+
388+
allFilters.push({
366389
model: urlName,
367390
isInScope: isInScope,
368391
filterBy: predicate,
369392
filterType: filterType,
370393
disableUrl: disableUrl
371394
});
395+
396+
if (filterType === 'select') {
397+
dropDownFilters.push({
398+
model: urlName,
399+
isInScope: isInScope,
400+
filterBy: predicate,
401+
filterType: filterType,
402+
disableUrl: disableUrl
403+
});
404+
} else {
405+
textAndDateFilters.push({
406+
model: urlName,
407+
isInScope: isInScope,
408+
filterBy: predicate,
409+
filterType: filterType,
410+
disableUrl: disableUrl
411+
});
412+
}
372413
});
373414

374-
$scope.filters = filters;
415+
$scope.allFilters = allFilters;
416+
$scope.textAndDateFilters = textAndDateFilters;
417+
$scope.dropDownFilters = dropDownFilters;
375418
}
376419
}
377420
}])

0 commit comments

Comments
 (0)