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

fix(select): ensure option list is repainted on IE9 #6240

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
26 changes: 25 additions & 1 deletion src/ng/directive/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,9 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
if (selectCtrl.hasOption(viewValue)) {
if (unknownOption.parent()) unknownOption.remove();
selectElement.val(viewValue);
if (viewValue === '') emptyOption.prop('selected', true); // to make IE9 happy
if (msie && msie <= 9) {
forceRepaint(selectElement);
}
} else {
if (isUndefined(viewValue) && emptyOption) {
selectElement.val('');
Expand Down Expand Up @@ -301,6 +303,18 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
});
}

function forceRepaint(selectElement) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could the other code which performs this task be factored out into a function and just called here if the options don't look right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmm, when else do we have options that don't look right? I was hoping this can be solving #5132 with force repaint on iOS as well, but that's failing in more mysterious way.

Copy link
Contributor

Choose a reason for hiding this comment

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

I mean, this looks similar to where the list of options gets reconstructed in $render(), so it may be possible to make use of the existing code.

But then again, it might not :p

var option = document.createElement('option');
viewValue = selectElement.val();
selectElement[0].add(option, null);
selectElement[0].remove(selectElement[0].options.length - 1);
for(var i = 0, children = selectElement.children(), ii = children.length; i < ii; i++) {
if (children[i]) {
children.eq(i).prop('selected', children[i].value === viewValue)
}
}
};

function setupAsOptions(scope, selectElement, ctrl) {
var match;

Expand Down Expand Up @@ -396,6 +410,9 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
}
}
ctrl.$setViewValue(value);
if (msie && msie <= 9 && !multiple) {
forceRepaint(selectElement);
}
});
});

Expand Down Expand Up @@ -575,6 +592,13 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
while(optionGroupsCache.length > groupIndex) {
optionGroupsCache.pop()[0].element.remove();
}
if (msie && msie <= 9 && !multiple) {
// the add an option then remove technique doesn't work too well for active
// elements and optgroups
if (optionGroup.length == 1 && selectElement[0] !== document.activeElement) {
forceRepaint(selectElement);
}
}
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions test/ng/directive/selectSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,32 @@ describe('select', function() {
expect(element.find('option').length).toEqual(1); // we add back the special empty option
});

it('should select correct option when list is shrunk ', function() {
createSingleSelect();

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

expect(element.find('option').length).toBe(4);
expect(element).toEqualSelect([''], '0', '1', '2');
element.find('option').eq(1).prop('selected', true);
browserTrigger(element.find('option').eq(1));
scope.$digest();
expect(element).toEqualSelect(['0'], '1', '2');
expect(scope.selected).toEqual({name: 'A'})

var A = element.find('option').eq(0);
expect(A.prop('value')).toBe('0');
expect(A.prop('selected')).toBeTruthy();
expect(A[0].selected).toBeTruthy();

var B = element.find('option').eq(1);
expect(B.prop('value')).toBe('1');
expect(B.prop('selected')).toBeFalsy();
expect(B[0].selected).toBeFalsy();
});

it('should shrink and then grow list', function() {
createSingleSelect();
Expand Down