Skip to content

Commit e528a95

Browse files
Merge pull request #9 from angular-data-grid/multiple
Added multiselect support. Some code improvements
2 parents d9d14c0 + a763805 commit e528a95

File tree

5 files changed

+70
-91
lines changed

5 files changed

+70
-91
lines changed

demo/bootstrap/index.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ <h4>Additional Demos</h4>
6767
class="form-control"
6868
uib-datepicker-popup="dd/MM/yyyy"
6969
placeholder="DD/MM/YYYY"
70-
min-date="dateFrom"
7170
max-date="dateTo"
7271
close-text="Close"
7372
ng-model="dateFrom"
@@ -117,7 +116,7 @@ <h4>Additional Demos</h4>
117116
</div>
118117
</div>
119118
<div ng-show="dateTo || dateFrom" class="buttons-right">
120-
<a href="" ng-click="dateTo = ''; dateFrom = ''; reloadGrid();">Clear Dates</a>
119+
<a href="" ng-click="dateTo = ''; dateFrom = ''; gridActions.refresh();">Clear Dates</a>
121120
</div>
122121
</div>
123122
</div>

demo/bootstrap/multiple.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ <h2>First Grid</h2>
3737
ng-change="gridActions1.filter()"
3838
ng-model="name"
3939
filter-by="name"
40+
grid-id="grid1"
4041
filter-type="text">
4142
</div>
4243
</div>
@@ -130,7 +131,8 @@ <h2>Second Grid</h2>
130131
<input type="text" class="form-control order-search-box"
131132
placeholder="Enter User Name"
132133
ng-change="gridActions2.filter()"
133-
ng-model="name"
134+
ng-model="name2"
135+
grid-id="grid2"
134136
filter-by="name"
135137
filter-type="text">
136138
</div>
@@ -224,7 +226,7 @@ <h2>Second Grid</h2>
224226
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-aria.min.js"></script>
225227
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-messages.min.js"></script>
226228
<script src="../../dist/pagination.min.js"></script>
227-
<script src="../../dist/dataGrid.min.js"></script>
229+
<script src="../../dist/dataGrid.js"></script>
228230
<script src="js/multipleApp.js"></script>
229231

230232
</html>

dist/dataGrid.js

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,19 @@
1414
})
1515
.controller('gridController', ['$scope', '$element', '$filter', '$location', 'filtersFactory', function ($scope, $element, $filter, $location, filtersFactory) {
1616
// 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'));
1919
$scope.serverPagination = $element.attr('server-pagination') === 'true';
2020
$scope.getDataDelay = $element.attr('get-delay') || 350;
2121

2222
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'));
2525
}
2626

27-
$scope.filtered = angular.copy($scope._gridOptions.data);
27+
$scope._gridOptions.grid = $scope;
28+
29+
$scope.filtered = $scope._gridOptions.data.slice();
2830
$scope.paginationOptions = $scope._gridOptions.pagination ? angular.copy($scope._gridOptions.pagination) : {};
2931
$scope.defaultsPaginationOptions = {
3032
itemsPerPage: $scope.paginationOptions.itemsPerPage || '10',
@@ -105,7 +107,7 @@
105107
//custom filters
106108
$scope.filters.forEach(function (filter) {
107109
var urlName = filter.model,
108-
value = $scope[urlName];
110+
value = $scope.$eval(urlName);
109111

110112
if (filter.disableUrl) {
111113
needApplyFilters = true;
@@ -152,29 +154,35 @@
152154
//custom filters
153155
$scope.filters.forEach(function (filter) {
154156
var urlName = filter.model,
155-
value = customParams[urlName],
156-
scope = $scope;
157+
value = customParams[urlName];
157158

158159
if (filter.disableUrl) {
159160
return;
160161
}
161162

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');
164167
return;
165168
}
166169

170+
167171
if (filter.filterType === 'select' && !value) {
168-
value = "";
172+
value = '';
169173
}
170174

171-
scope[urlName] = value;
175+
if (value) {
176+
$scope.__evaltmp = value;
177+
$scope.$eval(urlName + '=__evaltmp');
178+
}
172179
});
173180

174181
if (!$scope.serverPagination) {
175182
applyCustomFilters();
176183
}
177184

185+
178186
//pagination options
179187
$scope.paginationOptions.itemsPerPage = $scope.defaultsPaginationOptions.itemsPerPage;
180188
$scope.paginationOptions.currentPage = $scope.defaultsPaginationOptions.currentPage;
@@ -252,7 +260,7 @@
252260
$scope.filters.forEach(function (filter) {
253261
var predicate = filter.filterBy,
254262
urlName = filter.model,
255-
value = $scope[urlName],
263+
value = $scope.$eval(urlName),
256264
type = filter.filterType;
257265
if ($scope.customFilters[urlName]) {
258266
$scope.filtered = $scope.customFilters[urlName]($scope.filtered, value, predicate);
@@ -265,24 +273,19 @@
265273
});
266274
}
267275
}])
268-
.directive('gridData', ['$compile', '$animate', function ($compile, $animate) {
276+
.directive('gridData', ['$compile', '$animate', function ($compile) {
269277
return {
270278
restrict: 'EA',
271-
transclude: true,
272-
replace: true,
279+
scope: true,
273280
controller: 'gridController',
274-
link: function ($scope, $element, attrs, controller, $transclude) {
281+
link: function ($scope, $element, attrs) {
275282
var sorting = [],
276283
filters = [],
277284
rows = [],
278-
childScope = $scope.$new(),
279285
directiveElement = $element.parent(),
280286
gridId = attrs.id,
281287
serverPagination = attrs.serverPagination === 'true';
282288

283-
$transclude($scope, function (clone) {
284-
$animate.enter(clone, $element);
285-
});
286289

287290
angular.forEach(angular.element(directiveElement[0].querySelectorAll('[sortable]')), function (sortable) {
288291
var element = angular.element(sortable),
@@ -292,7 +295,7 @@
292295
predicate + "' && sortOptions.direction === 'asc', 'sort-descent' : sortOptions.predicate === '" +
293296
predicate + "' && sortOptions.direction === 'desc'}");
294297
element.attr('ng-click', "sort('" + predicate + "')");
295-
$compile(element)(childScope);
298+
$compile(element)($scope);
296299
});
297300

298301
angular.forEach(angular.element(document.querySelectorAll('[filter-by]')), function (filter) {
@@ -309,20 +312,18 @@
309312

310313
if (filterType !== 'select') {
311314
} 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);
313316
}
314317

315318
if (~filterType.indexOf('date') && !element.attr('ng-focus')
316319
&& !element.attr('ng-blur')) {
317320
element.attr('ng-focus', "filter('{" + urlName + " : " + "this." + urlName + "}')");
318321
element.attr('ng-blur', "filter('{" + urlName + " : " + "this." + urlName + "}')");
319-
$compile(element)($scope);
320322
}
321323
if (!urlName) {
322324
urlName = predicate;
323325
element.attr('ng-model', predicate);
324326
element.attr('ng-change', 'filter()');
325-
$compile(element)($scope);
326327
}
327328

328329
filters.push({
@@ -332,6 +333,8 @@
332333
filterType: filterType,
333334
disableUrl: disableUrl
334335
});
336+
337+
$compile(element)($scope);
335338
});
336339

337340
angular.forEach(angular.element(directiveElement[0].querySelectorAll('[grid-item]')), function (row) {
@@ -342,7 +345,7 @@
342345
} else {
343346
element.attr('ng-repeat', "item in filtered | startFrom:(paginationOptions.currentPage-1)*paginationOptions.itemsPerPage | limitTo:paginationOptions.itemsPerPage track by $index");
344347
}
345-
$compile(element)(childScope);
348+
$compile(element)($scope);
346349
});
347350

348351
$scope.sorting = sorting;
@@ -360,7 +363,7 @@
360363

361364
function textFilter(items, value, predicate) {
362365
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;
364367
});
365368
}
366369

@@ -406,22 +409,6 @@
406409
}
407410
});
408411

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-
425412
function generateOptions(values, predicate) {
426413
var array = [];
427414
if (values) {

0 commit comments

Comments
 (0)