|
36 | 36 | $scope.sortOptions = $scope._gridOptions.sort ? angular.copy($scope._gridOptions.sort) : {}; |
37 | 37 | $scope.customFilters = $scope._gridOptions.customFilters ? angular.copy($scope._gridOptions.customFilters) : {}; |
38 | 38 | $scope.urlSync = $scope._gridOptions.urlSync; |
| 39 | + $scope.allFilters = []; |
| 40 | + $scope.dropDownFilters = []; |
| 41 | + |
39 | 42 |
|
40 | 43 | $scope.$watchCollection('_gridOptions.data', function (newValue) { |
41 | 44 | if (newValue && newValue.length > -1) { |
42 | 45 | $scope.sortCache = {}; |
43 | 46 | $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); |
48 | 49 | }); |
49 | 50 |
|
50 | 51 | if ($scope.urlSync) { |
|
116 | 117 | } |
117 | 118 |
|
118 | 119 | //custom filters |
119 | | - $scope.filters.forEach(function (filter) { |
| 120 | + $scope.allFilters.forEach(function (filter) { |
120 | 121 | var urlName = filter.model, |
121 | 122 | value = filter.isInScope ? $scope.$eval(urlName) : $scope.$parent.$eval(urlName); |
122 | 123 |
|
|
160 | 161 | }); |
161 | 162 |
|
162 | 163 | //custom filters |
163 | | - $scope.filters.forEach(function (filter) { |
| 164 | + $scope.allFilters.forEach(function (filter) { |
164 | 165 | var urlName = filter.model, |
165 | 166 | value = customParams[urlName]; |
166 | 167 |
|
|
246 | 247 | //TO REMOVE ? |
247 | 248 | $scope._time = {}; |
248 | 249 |
|
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()) { |
251 | 254 | $scope.filtered = $scope.sortCache.data.slice(); |
252 | 255 | sorted = true; |
253 | | - } else { |
254 | | - $scope.filtered = $scope._gridOptions.data.slice(); |
255 | 256 | } |
256 | 257 |
|
257 | 258 | $scope._time.copy = Date.now() - time; |
258 | 259 | var time2 = Date.now(); |
259 | | - applyCustomFilters(); |
| 260 | + filterData($scope.textAndDateFilters); |
260 | 261 | $scope._time.filters = Date.now() - time2; |
261 | 262 | var time3 = Date.now(); |
262 | 263 |
|
263 | 264 | if ($scope.sortOptions.predicate && !sorted) { |
264 | 265 | $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(); |
270 | 267 | } |
271 | 268 | $scope._time.sort = Date.now() - time3; |
272 | 269 | $scope._time.all = Date.now() - time; |
273 | 270 | $scope.paginationOptions.totalItems = $scope.filtered.length; |
274 | 271 | } |
275 | 272 |
|
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) { |
278 | 285 | var predicate = filter.filterBy, |
279 | 286 | urlName = filter.model, |
280 | 287 | value = filter.isInScope ? $scope.$eval(urlName) : $scope.$parent.$eval(urlName), |
|
289 | 296 | } |
290 | 297 | }); |
291 | 298 | } |
| 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 | + } |
292 | 312 | }]) |
293 | 313 | .directive('gridItem', ['$compile', function ($compile) { |
294 | 314 | return { |
|
315 | 335 | scope: true, |
316 | 336 | controller: 'gridController', |
317 | 337 | link: function ($scope, $element, attrs) { |
318 | | - var filters = [], |
| 338 | + var allFilters = [], |
| 339 | + textAndDateFilters = [], |
| 340 | + dropDownFilters = [], |
319 | 341 | directiveElement = $element.parent(), |
320 | 342 | gridId = attrs.id, |
321 | 343 | serverPagination = attrs.serverPagination === 'true'; |
|
362 | 384 | //$compile(element)($scope); |
363 | 385 | } |
364 | 386 | //$compile(element)($scope); |
365 | | - filters.push({ |
| 387 | + |
| 388 | + allFilters.push({ |
366 | 389 | model: urlName, |
367 | 390 | isInScope: isInScope, |
368 | 391 | filterBy: predicate, |
369 | 392 | filterType: filterType, |
370 | 393 | disableUrl: disableUrl |
371 | 394 | }); |
| 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 | + } |
372 | 413 | }); |
373 | 414 |
|
374 | | - $scope.filters = filters; |
| 415 | + $scope.allFilters = allFilters; |
| 416 | + $scope.textAndDateFilters = textAndDateFilters; |
| 417 | + $scope.dropDownFilters = dropDownFilters; |
375 | 418 | } |
376 | 419 | } |
377 | 420 | }]) |
|
0 commit comments