Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix(ngOptions): don't duplicate groups with falsy values #14784

Merged
merged 1 commit into from
Jun 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/ng/directive/ngOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ var ngOptionsDirective = ['$compile', '$document', '$parse', function($compile,

for (var i = options.items.length - 1; i >= 0; i--) {
var option = options.items[i];
if (option.group) {
if (isDefined(option.group)) {
jqLiteRemove(option.element.parentNode);
} else {
jqLiteRemove(option.element);
Expand Down Expand Up @@ -661,7 +661,8 @@ var ngOptionsDirective = ['$compile', '$document', '$parse', function($compile,
listFragment.appendChild(groupElement);

// Update the label on the group element
groupElement.label = option.group;
// "null" is special cased because of Safari
groupElement.label = option.group === null ? 'null' : option.group;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed for some specific browser? In Chrome that I tried, null seems to be converted to "null" anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes for Safari :|

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Who else? Safari is officially the new IE 😛


// Store it for use later
groupElementMap[option.group] = groupElement;
Expand Down
81 changes: 81 additions & 0 deletions test/ng/directive/ngOptionsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1789,6 +1789,87 @@ describe('ngOptions', function() {
});


it('should group if the group has a falsy value (except undefined)', function() {
createSelect({
'ng-model': 'selected',
'ng-options': 'item.name group by item.group for item in values'
});

scope.$apply(function() {
scope.values = [{name: 'A'},
{name: 'B', group: ''},
{name: 'C', group: null},
{name: 'D', group: false},
{name: 'E', group: 0}];
scope.selected = scope.values[0];
});

var optgroups = element.find('optgroup');
var options = element.find('option');

expect(optgroups.length).toEqual(4);
expect(options.length).toEqual(5);

expect(optgroups[0].label).toBe('');
expect(optgroups[1].label).toBe('null');
expect(optgroups[2].label).toBe('false');
expect(optgroups[3].label).toBe('0');

expect(options[0].textContent).toBe('A');
expect(options[0].parentNode).toBe(element[0]);

expect(options[1].textContent).toBe('B');
expect(options[1].parentNode).toBe(optgroups[0]);

expect(options[2].textContent).toBe('C');
expect(options[2].parentNode).toBe(optgroups[1]);

expect(options[3].textContent).toBe('D');
expect(options[3].parentNode).toBe(optgroups[2]);

expect(options[4].textContent).toBe('E');
expect(options[4].parentNode).toBe(optgroups[3]);
});


it('should not duplicate a group with a falsy value when the options are updated', function() {

scope.$apply(function() {
scope.values = [{value: 'A', group: ''},
{value: 'B', group: 'First'}];
scope.selected = scope.values[0];
});

createSelect({
'ng-model': 'selected',
'ng-options': 'item.value group by item.group for item in values'
});

scope.$apply(function() {
scope.values.push({value: 'C', group: false});
});

var optgroups = element.find('optgroup');
var options = element.find('option');

expect(optgroups.length).toEqual(3);
expect(options.length).toEqual(3);

expect(optgroups[0].label).toBe('');
expect(optgroups[1].label).toBe('First');
expect(optgroups[2].label).toBe('false');

expect(options[0].textContent).toBe('A');
expect(options[0].parentNode).toBe(optgroups[0]);

expect(options[1].textContent).toBe('B');
expect(options[1].parentNode).toBe(optgroups[1]);

expect(options[2].textContent).toBe('C');
expect(options[2].parentNode).toBe(optgroups[2]);
});


it('should bind to scope value and track/identify objects', function() {
createSelect({
'ng-model': 'selected',
Expand Down