|
14 | 14 | }) |
15 | 15 | .controller('gridController', ['$scope', '$element', '$filter', '$location', 'filtersFactory', function ($scope, $element, $filter, $location, filtersFactory) { |
16 | 16 | // values by default |
17 | | - $scope._gridOptions = deepFind($scope, $element.attr('grid-options')); |
18 | | - $scope._gridActions = deepFind($scope, $element.attr('grid-actions')); |
| 17 | + $scope._gridOptions = $scope.$eval($element.attr('grid-options')); |
| 18 | + $scope._gridActions = $scope.$eval($element.attr('grid-actions')); |
19 | 19 | $scope.serverPagination = $element.attr('server-pagination') === 'true'; |
20 | 20 | $scope.getDataDelay = $element.attr('get-delay') || 350; |
21 | 21 |
|
22 | 22 | if (!$scope._gridActions) { |
23 | | - $scope.$parent[$element.attr('grid-actions')] = {}; |
24 | | - $scope._gridActions = $scope.$parent[$element.attr('grid-actions')]; |
| 23 | + $scope.$parent.$eval($element.attr('grid-actions') + '= {}'); |
| 24 | + $scope._gridActions = $scope.$parent.$eval($element.attr('grid-actions')); |
25 | 25 | } |
26 | 26 |
|
27 | | - $scope.filtered = angular.copy($scope._gridOptions.data); |
| 27 | + $scope._gridOptions.grid = $scope; |
| 28 | + |
| 29 | + $scope.filtered = $scope._gridOptions.data.slice(); |
28 | 30 | $scope.paginationOptions = $scope._gridOptions.pagination ? angular.copy($scope._gridOptions.pagination) : {}; |
29 | 31 | $scope.defaultsPaginationOptions = { |
30 | 32 | itemsPerPage: $scope.paginationOptions.itemsPerPage || '10', |
|
105 | 107 | //custom filters |
106 | 108 | $scope.filters.forEach(function (filter) { |
107 | 109 | var urlName = filter.model, |
108 | | - value = $scope[urlName]; |
| 110 | + value = $scope.$eval(urlName); |
109 | 111 |
|
110 | 112 | if (filter.disableUrl) { |
111 | 113 | needApplyFilters = true; |
|
152 | 154 | //custom filters |
153 | 155 | $scope.filters.forEach(function (filter) { |
154 | 156 | var urlName = filter.model, |
155 | | - value = customParams[urlName], |
156 | | - scope = $scope; |
| 157 | + value = customParams[urlName]; |
157 | 158 |
|
158 | 159 | if (filter.disableUrl) { |
159 | 160 | return; |
160 | 161 | } |
161 | 162 |
|
162 | | - if (~filter.filterType.toLowerCase().indexOf('date') && value) { |
163 | | - scope[urlName] = new Date(value); |
| 163 | + //datepicker-specific |
| 164 | + if (~filter.filterType.toLowerCase().indexOf('date')) { |
| 165 | + $scope.$parent.__evaltmp = value ? new Date(value) : null; |
| 166 | + $scope.$parent.$eval(urlName + '=__evaltmp'); |
164 | 167 | return; |
165 | 168 | } |
166 | 169 |
|
| 170 | + |
167 | 171 | if (filter.filterType === 'select' && !value) { |
168 | | - value = ""; |
| 172 | + value = ''; |
169 | 173 | } |
170 | 174 |
|
171 | | - scope[urlName] = value; |
| 175 | + if (value) { |
| 176 | + $scope.__evaltmp = value; |
| 177 | + $scope.$eval(urlName + '=__evaltmp'); |
| 178 | + } |
172 | 179 | }); |
173 | 180 |
|
174 | 181 | if (!$scope.serverPagination) { |
175 | 182 | applyCustomFilters(); |
176 | 183 | } |
177 | 184 |
|
| 185 | + |
178 | 186 | //pagination options |
179 | 187 | $scope.paginationOptions.itemsPerPage = $scope.defaultsPaginationOptions.itemsPerPage; |
180 | 188 | $scope.paginationOptions.currentPage = $scope.defaultsPaginationOptions.currentPage; |
|
252 | 260 | $scope.filters.forEach(function (filter) { |
253 | 261 | var predicate = filter.filterBy, |
254 | 262 | urlName = filter.model, |
255 | | - value = $scope[urlName], |
| 263 | + value = $scope.$eval(urlName), |
256 | 264 | type = filter.filterType; |
257 | 265 | if ($scope.customFilters[urlName]) { |
258 | 266 | $scope.filtered = $scope.customFilters[urlName]($scope.filtered, value, predicate); |
|
265 | 273 | }); |
266 | 274 | } |
267 | 275 | }]) |
268 | | - .directive('gridData', ['$compile', '$animate', function ($compile, $animate) { |
| 276 | + .directive('gridData', ['$compile', '$animate', function ($compile) { |
269 | 277 | return { |
270 | 278 | restrict: 'EA', |
271 | | - transclude: true, |
272 | | - replace: true, |
| 279 | + scope: true, |
273 | 280 | controller: 'gridController', |
274 | | - link: function ($scope, $element, attrs, controller, $transclude) { |
| 281 | + link: function ($scope, $element, attrs) { |
275 | 282 | var sorting = [], |
276 | 283 | filters = [], |
277 | 284 | rows = [], |
278 | | - childScope = $scope.$new(), |
279 | 285 | directiveElement = $element.parent(), |
280 | 286 | gridId = attrs.id, |
281 | 287 | serverPagination = attrs.serverPagination === 'true'; |
282 | 288 |
|
283 | | - $transclude($scope, function (clone) { |
284 | | - $animate.enter(clone, $element); |
285 | | - }); |
286 | 289 |
|
287 | 290 | angular.forEach(angular.element(directiveElement[0].querySelectorAll('[sortable]')), function (sortable) { |
288 | 291 | var element = angular.element(sortable), |
|
292 | 295 | predicate + "' && sortOptions.direction === 'asc', 'sort-descent' : sortOptions.predicate === '" + |
293 | 296 | predicate + "' && sortOptions.direction === 'desc'}"); |
294 | 297 | element.attr('ng-click', "sort('" + predicate + "')"); |
295 | | - $compile(element)(childScope); |
| 298 | + $compile(element)($scope); |
296 | 299 | }); |
297 | 300 |
|
298 | 301 | angular.forEach(angular.element(document.querySelectorAll('[filter-by]')), function (filter) { |
|
309 | 312 |
|
310 | 313 | if (filterType !== 'select') { |
311 | 314 | } else { |
312 | | - $scope[urlName + 'Options'] = generateOptions(deepFind($scope, $element.attr('grid-options') + '.data'), predicate); |
| 315 | + $scope[urlName + 'Options'] = generateOptions($scope.$eval($element.attr('grid-options') + '.data'), predicate); |
313 | 316 | } |
314 | 317 |
|
315 | 318 | if (~filterType.indexOf('date') && !element.attr('ng-focus') |
316 | 319 | && !element.attr('ng-blur')) { |
317 | 320 | element.attr('ng-focus', "filter('{" + urlName + " : " + "this." + urlName + "}')"); |
318 | 321 | element.attr('ng-blur', "filter('{" + urlName + " : " + "this." + urlName + "}')"); |
319 | | - $compile(element)($scope); |
320 | 322 | } |
321 | 323 | if (!urlName) { |
322 | 324 | urlName = predicate; |
323 | 325 | element.attr('ng-model', predicate); |
324 | 326 | element.attr('ng-change', 'filter()'); |
325 | | - $compile(element)($scope); |
326 | 327 | } |
327 | 328 |
|
328 | 329 | filters.push({ |
|
332 | 333 | filterType: filterType, |
333 | 334 | disableUrl: disableUrl |
334 | 335 | }); |
| 336 | + |
| 337 | + $compile(element)($scope); |
335 | 338 | }); |
336 | 339 |
|
337 | 340 | angular.forEach(angular.element(directiveElement[0].querySelectorAll('[grid-item]')), function (row) { |
|
342 | 345 | } else { |
343 | 346 | element.attr('ng-repeat', "item in filtered | startFrom:(paginationOptions.currentPage-1)*paginationOptions.itemsPerPage | limitTo:paginationOptions.itemsPerPage track by $index"); |
344 | 347 | } |
345 | | - $compile(element)(childScope); |
| 348 | + $compile(element)($scope); |
346 | 349 | }); |
347 | 350 |
|
348 | 351 | $scope.sorting = sorting; |
|
360 | 363 |
|
361 | 364 | function textFilter(items, value, predicate) { |
362 | 365 | return items.filter(function (item) { |
363 | | - return value && item[predicate] ? ~(item[predicate] + '').toLowerCase().indexOf(value.toLowerCase()) : true; |
| 366 | + return value && item[predicate] ? ~(item[predicate] + '').toLowerCase().indexOf((value + '').toLowerCase()) : true; |
364 | 367 | }); |
365 | 368 | } |
366 | 369 |
|
|
406 | 409 | } |
407 | 410 | }); |
408 | 411 |
|
409 | | - function deepFind(obj, path) { |
410 | | - var paths = path.split('.'), |
411 | | - current = obj, |
412 | | - i; |
413 | | - |
414 | | - for (i = 0; i < paths.length; ++i) { |
415 | | - if (current[paths[i]] == undefined) { |
416 | | - return undefined; |
417 | | - } else { |
418 | | - current = current[paths[i]]; |
419 | | - } |
420 | | - } |
421 | | - return current; |
422 | | - } |
423 | | - |
424 | | - |
425 | 412 | function generateOptions(values, predicate) { |
426 | 413 | var array = []; |
427 | 414 | if (values) { |
|
0 commit comments