1
0
mirror of synced 2024-11-22 13:06:08 +03:00

Update the selected property on objects

This fixes the results display so they prefer the `selected`
property on objects over checking if the `id` matches the value of
the underlying select.

We only ignore the `selected` property if it is null, at that point
we compare against the `id` property.

This closes https://github.com/select2/select2/issues/3163.
This commit is contained in:
Kevin Brown 2015-03-18 10:41:04 -04:00
parent a6999990b0
commit 9d359c9b38
6 changed files with 26 additions and 11 deletions

View File

@ -828,7 +828,8 @@ S2.define('select2/results',[
// id needs to be converted to a string when comparing // id needs to be converted to a string when comparing
var id = '' + item.id; var id = '' + item.id;
if ($.inArray(id, selectedIds) > -1) { if (item.selected ||
(item.selected == null && $.inArray(id, selectedIds) > -1)) {
$option.attr('aria-selected', 'true'); $option.attr('aria-selected', 'true');
} else { } else {
$option.attr('aria-selected', 'false'); $option.attr('aria-selected', 'false');
@ -2865,7 +2866,9 @@ S2.define('select2/data/select',[
SelectAdapter.prototype.select = function (data) { SelectAdapter.prototype.select = function (data) {
var self = this; var self = this;
// If data.element is a DOM nose, use it instead data.selected = true;
// If data.element is a DOM node, use it instead
if ($(data.element).is('option')) { if ($(data.element).is('option')) {
data.element.selected = true; data.element.selected = true;
@ -2907,6 +2910,8 @@ S2.define('select2/data/select',[
return; return;
} }
data.selected = false;
if ($(data.element).is('option')) { if ($(data.element).is('option')) {
data.element.selected = false; data.element.selected = false;

File diff suppressed because one or more lines are too long

9
dist/js/select2.js vendored
View File

@ -828,7 +828,8 @@ S2.define('select2/results',[
// id needs to be converted to a string when comparing // id needs to be converted to a string when comparing
var id = '' + item.id; var id = '' + item.id;
if ($.inArray(id, selectedIds) > -1) { if (item.selected ||
(item.selected == null && $.inArray(id, selectedIds) > -1)) {
$option.attr('aria-selected', 'true'); $option.attr('aria-selected', 'true');
} else { } else {
$option.attr('aria-selected', 'false'); $option.attr('aria-selected', 'false');
@ -2865,7 +2866,9 @@ S2.define('select2/data/select',[
SelectAdapter.prototype.select = function (data) { SelectAdapter.prototype.select = function (data) {
var self = this; var self = this;
// If data.element is a DOM nose, use it instead data.selected = true;
// If data.element is a DOM node, use it instead
if ($(data.element).is('option')) { if ($(data.element).is('option')) {
data.element.selected = true; data.element.selected = true;
@ -2907,6 +2910,8 @@ S2.define('select2/data/select',[
return; return;
} }
data.selected = false;
if ($(data.element).is('option')) { if ($(data.element).is('option')) {
data.element.selected = false; data.element.selected = false;

File diff suppressed because one or more lines are too long

View File

@ -30,7 +30,9 @@ define([
SelectAdapter.prototype.select = function (data) { SelectAdapter.prototype.select = function (data) {
var self = this; var self = this;
// If data.element is a DOM nose, use it instead data.selected = true;
// If data.element is a DOM node, use it instead
if ($(data.element).is('option')) { if ($(data.element).is('option')) {
data.element.selected = true; data.element.selected = true;
@ -72,6 +74,8 @@ define([
return; return;
} }
data.selected = false;
if ($(data.element).is('option')) { if ($(data.element).is('option')) {
data.element.selected = false; data.element.selected = false;

View File

@ -109,7 +109,8 @@ define([
// id needs to be converted to a string when comparing // id needs to be converted to a string when comparing
var id = '' + item.id; var id = '' + item.id;
if ($.inArray(id, selectedIds) > -1) { if (item.selected ||
(item.selected == null && $.inArray(id, selectedIds) > -1)) {
$option.attr('aria-selected', 'true'); $option.attr('aria-selected', 'true');
} else { } else {
$option.attr('aria-selected', 'false'); $option.attr('aria-selected', 'false');