Skip to content

Commit 135d663

Browse files
author
Ben TORFS
committed
Support for async data sources, closes bentorfs#3
1 parent 0d150a2 commit 135d663

7 files changed

+110
-14
lines changed

dist/angular-bootstrap-multiselect-templates.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ angular.module("multiselect.html", []).run(["$templateCache", function($template
3434
" <li ng-show=\"showSearch\">\n" +
3535
" <div class=\"dropdown-header\">\n" +
3636
" <input type=\"text\" class=\"form-control input-sm\" style=\"width: 100%;\"\n" +
37-
" ng-model=\"searchFilter\" placeholder=\"Search...\"/>\n" +
37+
" ng-model=\"searchFilter\" placeholder=\"Search...\" ng-change=\"updateOptions()\"/>\n" +
3838
" </div>\n" +
3939
" </li>\n" +
4040
"\n" +

dist/angular-bootstrap-multiselect.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
showSelectAll: '=?',
2626
showUnselectAll: '=?',
2727
showSearch: '=?',
28+
searchFilter: '=?',
2829
disabled: '=?ngDisabled'
2930
},
3031
require: 'ngModel',
@@ -35,6 +36,10 @@
3536

3637
$scope.searchFilter = '';
3738

39+
if (typeof $scope.options !== 'function') {
40+
$scope.resolvedOptions = $scope.options;
41+
}
42+
3843
if (typeof $attrs.disabled != 'undefined') {
3944
$scope.disabled = true;
4045
}
@@ -58,9 +63,9 @@
5863
if ($scope.selectedOptions) {
5964
$scope.selectedOptions = [];
6065
}
61-
$scope.unselectedOptions = angular.copy($scope.options);
66+
$scope.unselectedOptions = angular.copy($scope.resolvedOptions);
6267
} else {
63-
$scope.selectedOptions = $scope.options.filter(function (el) {
68+
$scope.selectedOptions = $scope.resolvedOptions.filter(function (el) {
6469
var id = $scope.getId(el);
6570
for (var i = 0; i < $ngModelCtrl.$viewValue.length; i++) {
6671
var selectedId = $scope.getId($ngModelCtrl.$viewValue[i]);
@@ -70,7 +75,7 @@
7075
}
7176
return false;
7277
});
73-
$scope.unselectedOptions = $scope.options.filter(function (el) {
78+
$scope.unselectedOptions = $scope.resolvedOptions.filter(function (el) {
7479
return $scope.selectedOptions.indexOf(el) < 0;
7580
});
7681
}
@@ -121,13 +126,13 @@
121126
};
122127

123128
$scope.selectAll = function () {
124-
$scope.selectedOptions = $scope.options;
129+
$scope.selectedOptions = $scope.resolvedOptions;
125130
$scope.unselectedOptions = [];
126131
};
127132

128133
$scope.unselectAll = function () {
129134
$scope.selectedOptions = [];
130-
$scope.unselectedOptions = $scope.options;
135+
$scope.unselectedOptions = $scope.resolvedOptions;
131136
};
132137

133138
$scope.toggleItem = function (item) {
@@ -190,6 +195,15 @@
190195
return false;
191196
};
192197

198+
$scope.updateOptions = function () {
199+
if (typeof $scope.options === 'function') {
200+
$scope.options().then(function (resolvedOptions) {
201+
$scope.resolvedOptions = resolvedOptions;
202+
updateSelectionLists();
203+
});
204+
}
205+
};
206+
193207
// This search function is optimized to take into account the search limit.
194208
// Using angular limitTo filter is not efficient for big lists, because it still runs the search for
195209
// all elements, even if the limit is reached
@@ -252,7 +266,7 @@ angular.module("multiselect.html", []).run(["$templateCache", function($template
252266
" <li ng-show=\"showSearch\">\n" +
253267
" <div class=\"dropdown-header\">\n" +
254268
" <input type=\"text\" class=\"form-control input-sm\" style=\"width: 100%;\"\n" +
255-
" ng-model=\"searchFilter\" placeholder=\"Search...\"/>\n" +
269+
" ng-model=\"searchFilter\" placeholder=\"Search...\" ng-change=\"updateOptions()\"/>\n" +
256270
" </div>\n" +
257271
" </li>\n" +
258272
"\n" +

dist/angular-bootstrap-multiselect.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/multiselect.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<li ng-show="showSearch">
3131
<div class="dropdown-header">
3232
<input type="text" class="form-control input-sm" style="width: 100%;"
33-
ng-model="searchFilter" placeholder="Search..."/>
33+
ng-model="searchFilter" placeholder="Search..." ng-change="updateOptions()"/>
3434
</div>
3535
</li>
3636

src/multiselect.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
showSelectAll: '=?',
2626
showUnselectAll: '=?',
2727
showSearch: '=?',
28+
searchFilter: '=?',
2829
disabled: '=?ngDisabled'
2930
},
3031
require: 'ngModel',
@@ -35,6 +36,10 @@
3536

3637
$scope.searchFilter = '';
3738

39+
if (typeof $scope.options !== 'function') {
40+
$scope.resolvedOptions = $scope.options;
41+
}
42+
3843
if (typeof $attrs.disabled != 'undefined') {
3944
$scope.disabled = true;
4045
}
@@ -58,9 +63,9 @@
5863
if ($scope.selectedOptions) {
5964
$scope.selectedOptions = [];
6065
}
61-
$scope.unselectedOptions = angular.copy($scope.options);
66+
$scope.unselectedOptions = angular.copy($scope.resolvedOptions);
6267
} else {
63-
$scope.selectedOptions = $scope.options.filter(function (el) {
68+
$scope.selectedOptions = $scope.resolvedOptions.filter(function (el) {
6469
var id = $scope.getId(el);
6570
for (var i = 0; i < $ngModelCtrl.$viewValue.length; i++) {
6671
var selectedId = $scope.getId($ngModelCtrl.$viewValue[i]);
@@ -70,7 +75,7 @@
7075
}
7176
return false;
7277
});
73-
$scope.unselectedOptions = $scope.options.filter(function (el) {
78+
$scope.unselectedOptions = $scope.resolvedOptions.filter(function (el) {
7479
return $scope.selectedOptions.indexOf(el) < 0;
7580
});
7681
}
@@ -121,13 +126,13 @@
121126
};
122127

123128
$scope.selectAll = function () {
124-
$scope.selectedOptions = $scope.options;
129+
$scope.selectedOptions = $scope.resolvedOptions;
125130
$scope.unselectedOptions = [];
126131
};
127132

128133
$scope.unselectAll = function () {
129134
$scope.selectedOptions = [];
130-
$scope.unselectedOptions = $scope.options;
135+
$scope.unselectedOptions = $scope.resolvedOptions;
131136
};
132137

133138
$scope.toggleItem = function (item) {
@@ -190,6 +195,15 @@
190195
return false;
191196
};
192197

198+
$scope.updateOptions = function () {
199+
if (typeof $scope.options === 'function') {
200+
$scope.options().then(function (resolvedOptions) {
201+
$scope.resolvedOptions = resolvedOptions;
202+
updateSelectionLists();
203+
});
204+
}
205+
};
206+
193207
// This search function is optimized to take into account the search limit.
194208
// Using angular limitTo filter is not efficient for big lists, because it still runs the search for
195209
// all elements, even if the limit is reached

0 commit comments

Comments
 (0)