Skip to content

Commit 1b3411e

Browse files
author
Christian Musa
committed
Add attribute to set a custom text when no options selected
1 parent 72029b8 commit 1b3411e

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

dist/angular-bootstrap-multiselect.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,19 @@
2626
showUnselectAll: '=?',
2727
showSearch: '=?',
2828
searchFilter: '=?',
29-
disabled: '=?ngDisabled'
29+
disabled: '=?ngDisabled',
30+
defaultText: '@'
3031
},
3132
require: 'ngModel',
3233
templateUrl: 'multiselect.html',
3334
link: function ($scope, $element, $attrs, $ngModelCtrl) {
3435
$scope.selectionLimit = $scope.selectionLimit || 0;
3536
$scope.searchLimit = $scope.searchLimit || 25;
37+
$scope.defaultText = $scope.defaultText || 'Select';
3638

3739
$scope.searchFilter = '';
3840

41+
$scope.resolvedOptions = [];
3942
if (typeof $scope.options !== 'function') {
4043
$scope.resolvedOptions = $scope.options;
4144
}
@@ -116,12 +119,12 @@
116119
var totalSelected;
117120
totalSelected = angular.isDefined($scope.selectedOptions) ? $scope.selectedOptions.length : 0;
118121
if (totalSelected === 0) {
119-
return 'Select';
122+
return $scope.defaultText;
120123
} else {
121124
return totalSelected + ' ' + 'selected';
122125
}
123126
} else {
124-
return 'Select';
127+
return $scope.defaultText;
125128
}
126129
};
127130

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.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@
2626
showUnselectAll: '=?',
2727
showSearch: '=?',
2828
searchFilter: '=?',
29-
disabled: '=?ngDisabled'
29+
disabled: '=?ngDisabled',
30+
defaultText: '@'
3031
},
3132
require: 'ngModel',
3233
templateUrl: 'multiselect.html',
3334
link: function ($scope, $element, $attrs, $ngModelCtrl) {
3435
$scope.selectionLimit = $scope.selectionLimit || 0;
3536
$scope.searchLimit = $scope.searchLimit || 25;
37+
$scope.defaultText = $scope.defaultText || 'Select';
3638

3739
$scope.searchFilter = '';
3840

@@ -117,12 +119,12 @@
117119
var totalSelected;
118120
totalSelected = angular.isDefined($scope.selectedOptions) ? $scope.selectedOptions.length : 0;
119121
if (totalSelected === 0) {
120-
return 'Select';
122+
return $scope.defaultText;
121123
} else {
122124
return totalSelected + ' ' + 'selected';
123125
}
124126
} else {
125-
return 'Select';
127+
return $scope.defaultText;
126128
}
127129
};
128130

test/unit/multiselect-stringmodels-test.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,22 @@ describe("The multiselect directive, when using string models,", function () {
4141
expect(element.isolateScope().unselectedOptions.length).toBe(3);
4242
});
4343

44-
it('shows a label on the button when no items have been chosen', function () {
44+
it('shows a default label on the button when no items have been chosen', function () {
4545
$scope.options = ['el1', 'el2', 'el3'];
4646
var element = $compile("<multiselect ng-model='selection' options='options'></multiselect>")($scope);
4747
$scope.$digest();
4848

4949
expect(element.isolateScope().getButtonText()).toBe('Select');
5050
});
5151

52+
it('shows a custom label on the button when no items have been chosen', function () {
53+
$scope.options = ['el1', 'el2', 'el3'];
54+
var element = $compile("<multiselect ng-model='selection' options='options' default-text='dummy'></multiselect>")($scope);
55+
$scope.$digest();
56+
57+
expect(element.isolateScope().getButtonText()).toBe('dummy');
58+
});
59+
5260
it('shows the name of the element when one item is chosen', function () {
5361
$scope.options = ['el1', 'el2', 'el3'];
5462
$scope.selection = ['el1'];

0 commit comments

Comments
 (0)