Skip to content
This repository was archived by the owner on Dec 16, 2019. It is now read-only.

Commit 4c43a28

Browse files
committed
Specify field to search on.
By default, search is done on all items, by specifying the searchField in the settings object one can specify on which field of the objects the filtering should be done.
1 parent 27b62a7 commit 4c43a28

File tree

3 files changed

+83
-13
lines changed

3 files changed

+83
-13
lines changed

pages/javascripts/pages/home/ExampleCtrl.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
'use strict';
22

33
angular.module('exampleApp').controller('ExampleCtrl', ['$scope', function($scope) {
4-
$scope.testmodel = {};
4+
$scope.testmodel = [];
55
$scope.testdata = [
6-
{ id: 1, label: "David" },
7-
{ id: 2, label: "Jhon" },
8-
{ id: 3, label: "Danny" }];
6+
{ id: 1, label: "David", age: 23 },
7+
{ id: 2, label: "Jhon", age: 24 },
8+
{ id: 3, label: "Danny", age: 26 }];
99
$scope.testsettings = {
10-
closeOnDeselect: true,
11-
selectionLimit: 1
10+
searchField: 'age',
11+
enableSearch: true
1212
};
1313

14+
$scope.testevents = {
15+
'onMaxSelectionReached': function () { // This event is not firing on selection of max limit
16+
alert("you can not select more than 2 contacts");
17+
}
18+
}
19+
1420
$scope.example1model = [];
1521
$scope.example1data = [
1622
{id: 1, label: "David"},
@@ -61,7 +67,8 @@ angular.module('exampleApp').controller('ExampleCtrl', ['$scope', function($scop
6167
{id: 1, label: "David"},
6268
{id: 2, label: "Jhon"},
6369
{id: 3, label: "Danny"}];
64-
$scope.example7settings = {externalIdProp: ''};
70+
$scope.example7settings = { externalIdProp: '' };
71+
$scope.customFilter = 'a';
6572

6673
$scope.example8model = [];
6774
$scope.example8data = [
@@ -210,5 +217,13 @@ angular.module('exampleApp').controller('ExampleCtrl', ['$scope', function($scop
210217
template: '<b>{{option.name}}</b>'
211218
};
212219

213-
$scope.customFilter = 'a';
220+
$scope.example20model = [];
221+
$scope.example20data = [
222+
{ id: 1, label: "David", age: 23 },
223+
{ id: 2, label: "Jhon", age: 24 },
224+
{ id: 3, label: "Danny", age: 26 }];
225+
$scope.example20settings = {
226+
searchField: 'age',
227+
enableSearch: true
228+
};
214229
}]);

pages/javascripts/pages/home/home.html

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ <h2 style="margin-bottom: 0">Download</h2>
2323
</div>
2424
</div>
2525
<uib-accordion close-others="true">
26-
<!--<uib-accordion-group heading="test Settings Example">
26+
<!--<uib-accordion-group heading="test Settings Example" is-open="true" style="height: 500px">
2727
<div class="row">
2828
<div class="col-xs-12">
2929
test
@@ -34,7 +34,7 @@ <h2 style="margin-bottom: 0">Download</h2>
3434
<h3>Demo</h3>
3535
<div class="well">
3636
<div>
37-
<div ng-dropdown-multiselect options="testdata" selected-model="testmodel" extra-settings="testsettings"></div>
37+
<div ng-dropdown-multiselect options="testdata" selected-model="testmodel" extra-settings="testsettings" events="testevents"></div>
3838
</div>
3939
</div>
4040
</div>
@@ -234,6 +234,48 @@ <h3>Code</h3>
234234
</div>
235235
</div>
236236
</uib-accordion-group>
237+
<uib-accordion-group heading="Specifying Search field">
238+
<div class="row">
239+
<div class="col-xs-12">
240+
By default, search is done on all items, by specifying the searchField in the settings object one can specify on which field of the objects the filtering should be done.
241+
</div>
242+
</div>
243+
<div class="row">
244+
<div class="col-xs-12 col-sm-6">
245+
<h3>Demo</h3>
246+
<div class="well">
247+
<div>
248+
<div ng-dropdown-multiselect="" options="example20data" selected-model="example20model" extra-settings="example20settings"></div>
249+
</div>
250+
</div>
251+
</div>
252+
<div class="col-xs-12 col-sm-6">
253+
<h3>The model:</h3>
254+
<pre>{{example20model|json}}</pre>
255+
</div>
256+
</div>
257+
<div class="row">
258+
<div class="col-md-12">
259+
<h3>Code</h3>
260+
<div hljs language="javascript">
261+
// HTML
262+
<div ng-dropdown-multiselect="" options="example20data" selected-model="example20model" extra-settings="example20settings"></div>
263+
264+
// JavaScript
265+
$scope.example20model = [];
266+
$scope.example20data = [
267+
{ id: 1, label: "David", age: 23 },
268+
{ id: 2, label: "Jhon", age: 24 },
269+
{ id: 3, label: "Danny", age: 26 }
270+
];
271+
$scope.example20settings = {
272+
searchField: 'age',
273+
enableSearch: true
274+
};
275+
</div>
276+
</div>
277+
</div>
278+
</uib-accordion-group>
237279
<uib-accordion-group heading="Selection Limit">
238280
<div class="row">
239281
<div class="col-xs-12">
@@ -867,6 +909,12 @@ <h2>Settings</h2>
867909
<td>false</td>
868910
<td>Indicated if to show the search input or not.</td>
869911
</tr>
912+
<tr>
913+
<td>searchField</td>
914+
<td>String</td>
915+
<td>"$"</td>
916+
<td>Indicates on which field the search should be done</td>
917+
</tr>
870918
<tr>
871919
<td>selectionLimit</td>
872920
<td>Number</td>

src/angularjs-dropdown-multiselect.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co
3838
template += '<li ng-show="settings.enableSearch" class="divider"></li>';
3939

4040
if (groups) {
41-
template += '<li ng-repeat-start="option in orderedItems | filter: searchFilter" ng-show="getPropertyForObject(option, settings.groupBy) !== getPropertyForObject(orderedItems[$index - 1], settings.groupBy)" role="presentation" class="dropdown-header">{{ getGroupTitle(getPropertyForObject(option, settings.groupBy)) }}</li>';
41+
template += '<li ng-repeat-start="option in orderedItems | filter:getFilter(searchFilter)" ng-show="getPropertyForObject(option, settings.groupBy) !== getPropertyForObject(orderedItems[$index - 1], settings.groupBy)" role="presentation" class="dropdown-header">{{ getGroupTitle(getPropertyForObject(option, settings.groupBy)) }}</li>';
4242
template += '<li ng-class="{\'active\': isChecked(getPropertyForObject(option,settings.idProp)) && settings.styleActive}" ng-repeat-end role="presentation">';
4343
} else {
44-
template += '<li ng-class="{\'active\': isChecked(getPropertyForObject(option,settings.idProp)) && settings.styleActive}" role="presentation" ng-repeat="option in options | filter: searchFilter">';
44+
template += '<li ng-class="{\'active\': isChecked(getPropertyForObject(option,settings.idProp)) && settings.styleActive}" role="presentation" ng-repeat="option in options | filter:getFilter(searchFilter)">';
4545
}
4646

4747
template += '<a role="menuitem" class="option" tabindex="-1" ng-click="setSelectedItem(getPropertyForObject(option,settings.idProp))">';
@@ -132,7 +132,8 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co
132132
smartButtonTextConverter: angular.noop,
133133
styleActive: false,
134134
keyboardControls: false,
135-
template: '{{getPropertyForObject(option, settings.displayProp)}}'
135+
template: '{{getPropertyForObject(option, settings.displayProp)}}',
136+
searchField: '$'
136137
};
137138

138139
$scope.texts = {
@@ -429,6 +430,12 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co
429430
}
430431
}
431432
}
433+
434+
$scope.getFilter = function(searchFilter) {
435+
var filter = {};
436+
filter[$scope.settings.searchField] = searchFilter;
437+
return filter;
438+
}
432439
}
433440
};
434441
}]);

0 commit comments

Comments
 (0)