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

Commit dfbe666

Browse files
author
vyarovuy
committed
- add "group filter" feature
1 parent 948d383 commit dfbe666

File tree

3 files changed

+73
-13
lines changed

3 files changed

+73
-13
lines changed

src/uiSelectChoicesDirective.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ uis.directive('uiSelectChoices',
2121

2222
// var repeat = RepeatParser.parse(attrs.repeat);
2323
var groupByExp = attrs.groupBy;
24+
var groupFilterExp = attrs.groupFilter;
2425

25-
$select.parseRepeatAttr(attrs.repeat, groupByExp); //Result ready at $select.parserResult
26+
$select.parseRepeatAttr(attrs.repeat, groupByExp, groupFilterExp); //Result ready at $select.parserResult
2627

2728
$select.disableChoiceExpression = attrs.uiDisableChoice;
2829
$select.onHighlightCallback = attrs.onHighlight;

src/uiSelectController.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ uis.controller('uiSelectCtrl',
5959
}
6060
}
6161

62+
function _groupsFilter(groups, groupNames) {
63+
var i, j, result = [];
64+
for(i = 0; i < groupNames.length ;i++){
65+
for(j = 0; j < groups.length ;j++){
66+
if(groups[j].name == [groupNames[i]]){
67+
result.push(groups[j]);
68+
}
69+
}
70+
}
71+
return result;
72+
}
73+
6274
// When the user clicks on ui-select, displays the dropdown list
6375
ctrl.activate = function(initSearchValue, avoidReset) {
6476
if (!ctrl.disabled && !ctrl.open) {
@@ -90,11 +102,11 @@ uis.controller('uiSelectCtrl',
90102
})[0];
91103
};
92104

93-
ctrl.parseRepeatAttr = function(repeatAttr, groupByExp) {
105+
ctrl.parseRepeatAttr = function(repeatAttr, groupByExp, groupFilterExp) {
94106
function updateGroups(items) {
107+
var groupFn = $scope.$eval(groupByExp);
95108
ctrl.groups = [];
96109
angular.forEach(items, function(item) {
97-
var groupFn = $scope.$eval(groupByExp);
98110
var groupName = angular.isFunction(groupFn) ? groupFn(item) : item[groupFn];
99111
var group = ctrl.findGroupByName(groupName);
100112
if(group) {
@@ -104,6 +116,14 @@ uis.controller('uiSelectCtrl',
104116
ctrl.groups.push({name: groupName, items: [item]});
105117
}
106118
});
119+
if(groupFilterExp){
120+
var groupFilterFn = $scope.$eval(groupFilterExp);
121+
if( angular.isFunction(groupFilterFn)){
122+
ctrl.groups = groupFilterFn(ctrl.groups);
123+
} else if(angular.isArray(groupFilterFn)){
124+
ctrl.groups = _groupsFilter(ctrl.groups, groupFilterFn);
125+
}
126+
}
107127
ctrl.items = [];
108128
ctrl.groups.forEach(function(group) {
109129
ctrl.items = ctrl.items.concat(group.items);

test/select.spec.js

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,22 @@ describe('ui-select tests', function() {
6060
return person.age % 2 ? 'even' : 'odd';
6161
};
6262

63+
64+
scope.filterInvertOrder = function(groups) {
65+
debugger;
66+
var results = groups.sort(function(groupA, groupB){
67+
return groupA.name.toLocaleLowerCase() < groupB.name.toLocaleLowerCase();
68+
});
69+
return results;
70+
};
71+
72+
scope.filterBarAndBaz = function(groups) {
73+
debugger;
74+
return groups.sort(function(groupA, groupB){
75+
return groupA.name.toLocaleLowerCase() < groupB.name.toLocaleLowerCase();
76+
});
77+
};
78+
6379
scope.people = [
6480
{ name: 'Adam', email: 'adam@email.com', group: 'Foo', age: 12 },
6581
{ name: 'Amalie', email: 'amalie@email.com', group: 'Foo', age: 12 },
@@ -683,25 +699,48 @@ describe('ui-select tests', function() {
683699
});
684700
});
685701

686-
describe('choices group by function', function() {
702+
describe('choices group filter function', function() {
687703
function createUiSelect() {
688-
return compileTemplate(
689-
'<ui-select ng-model="selection.selected"> \
690-
<ui-select-match placeholder="Pick one...">{{$select.selected.name}}</ui-select-match> \
691-
<ui-select-choices group-by="getGroupLabel" repeat="person in people | filter: $select.search"> \
692-
<div ng-bind-html="person.name | highlight: $select.search"></div> \
693-
</ui-select-choices> \
694-
</ui-select>'
704+
return compileTemplate('\
705+
<ui-select ng-model="selection.selected"> \
706+
<ui-select-match placeholder="Pick one...">{{$select.selected.name}}</ui-select-match> \
707+
<ui-select-choices group-by="\'group\'" group-filter="filterInvertOrder" repeat="person in people | filter: $select.search"> \
708+
<div ng-bind-html="person.name | highlight: $select.search"></div> \
709+
</ui-select-choices> \
710+
</ui-select>'
695711
);
696712
}
697-
it("should extract group value through function", function () {
713+
it("should sort groups using filter", function () {
698714
var el = createUiSelect();
715+
debugger;
699716
expect(el.find('.ui-select-choices-group .ui-select-choices-group-label').map(function() {
700717
return this.textContent;
701-
}).toArray()).toEqual(['odd', 'even']);
718+
}).toArray()).toEqual(["Foo", "Baz", "bar"]);
702719
});
703720
});
704721

722+
describe('choices group filter array', function() {
723+
function createUiSelect() {
724+
return compileTemplate('\
725+
<ui-select ng-model="selection.selected"> \
726+
<ui-select-match placeholder="Pick one...">{{$select.selected.name}}</ui-select-match> \
727+
<ui-select-choices group-by="\'group\'" group-filter="[\'Foo\']" \
728+
repeat="person in people | filter: $select.search"> \
729+
<div ng-bind-html="person.name | highlight: $select.search"></div> \
730+
</ui-select-choices> \
731+
</ui-select>'
732+
);
733+
}
734+
it("should sort groups using filter", function () {
735+
var el = createUiSelect();
736+
debugger;
737+
expect(el.find('.ui-select-choices-group .ui-select-choices-group-label').map(function() {
738+
return this.textContent;
739+
}).toArray()).toEqual(["Foo"]);
740+
});
741+
});
742+
743+
705744
it('should throw when no ui-select-choices found', function() {
706745
expect(function() {
707746
compileTemplate(

0 commit comments

Comments
 (0)