Skip to content

Commit 0102a6f

Browse files
committed
bug fix in using option text matching if value matching exist
Thanks [ksrini](http://stackoverflow.com/users/505373/ksrini) for the bug report. See http://stackoverflow.com/q/31738950/315935 for more details.
1 parent 42752be commit 0102a6f

File tree

1 file changed

+62
-44
lines changed

1 file changed

+62
-44
lines changed

js/grid.common.js

+62-44
Original file line numberDiff line numberDiff line change
@@ -419,14 +419,20 @@
419419
break;
420420
case "select":
421421
elem = document.createElement("select");
422-
var msl, ovm = [], cm, iCol;
423-
iCol = p.iColByName[options.name];
424-
cm = p.colModel[iCol];
422+
var msl, ovm = [], iCol = p.iColByName[options.name], cm = p.colModel[iCol], isSelected;
425423
if (options.multiple === true) {
426424
msl = true;
427425
elem.multiple = "multiple";
428426
$(elem).attr("aria-multiselectable", "true");
429-
} else { msl = false; }
427+
ovm = vl.split(",");
428+
ovm = $.map(ovm, function (n) { return $.trim(n); });
429+
} else {
430+
msl = false;
431+
ovm[0] = $.trim(vl);
432+
}
433+
if (options.size === undefined) {
434+
options.size = msl ? 3 : 1;
435+
}
430436
if (options.dataUrl !== undefined) {
431437
var rowid = null, postData = options.postData || ajaxso.postData;
432438
try {
@@ -441,35 +447,36 @@
441447
type: "GET",
442448
dataType: "html",
443449
data: $.isFunction(postData) ? postData.call($t, rowid, vl, String(options.name)) : postData,
444-
context: { elem: elem, options: options, vl: vl, cm: cm, iCol: iCol },
450+
context: { elem: elem, options: options, cm: cm, iCol: iCol, ovm: ovm },
445451
success: function (data, textStatus, jqXHR) {
446-
var ovm1 = [], elem1 = this.elem, vl2 = this.vl, cm1 = this.cm, iCol1 = this.iCol,
452+
var ovm1 = this.ovm, elem1 = this.elem, cm1 = this.cm, iCol1 = this.iCol,
447453
options1 = $.extend({}, this.options),
448-
msl1 = options1.multiple === true,
449454
a = $.isFunction(options1.buildSelect) ? options1.buildSelect.call($t, data, jqXHR, cm1, iCol1) : data;
450455
if (typeof a === "string") {
451456
a = $($.trim(a)).html();
452457
}
453458
if (a) {
459+
//$(elem1).empty(); // ???
454460
$(elem1).append(a);
455461
setAttributes(elem1, options1, postData ? ["postData"] : undefined);
456-
if (options1.size === undefined) { options1.size = msl1 ? 3 : 1; }
457-
if (msl1) {
458-
ovm1 = vl2.split(",");
459-
ovm1 = $.map(ovm1, function (n) { return $.trim(n); });
460-
} else {
461-
ovm1[0] = $.trim(vl2);
462-
}
463-
//$(elem).attr(options);
464462
setTimeout(function () {
463+
var isSelected1; // undefined
465464
$("option", elem1).each(function (i) {
466465
//if(i===0) { this.selected = ""; }
467466
// fix IE8/IE7 problem with selecting of the first item on multiple=true
468467
if (i === 0 && elem1.multiple) { this.selected = false; }
469-
if ($.inArray($.trim($(this).text()), ovm1) > -1 || $.inArray($.trim($(this).val()), ovm1) > -1) {
468+
if ($.inArray($.trim($(this).val()), ovm1) > -1) {
470469
this.selected = "selected";
470+
isSelected1 = true;
471471
}
472472
});
473+
if (!isSelected1) {
474+
$("option", elem1).each(function (i) {
475+
if ($.inArray($.trim($(this).text()), ovm1) > -1) {
476+
this.selected = "selected";
477+
}
478+
});
479+
}
473480
jgrid.fullBoolFeedback.call($t, options1.selectFilled, "jqGridSelectFilled", {
474481
elem: elem1,
475482
options: options1,
@@ -482,16 +489,8 @@
482489
}
483490
}, ajaxso || {}));
484491
} else if (options.value) {
485-
var i;
486-
if (options.size === undefined) {
487-
options.size = msl ? 3 : 1;
488-
}
489-
if (msl) {
490-
ovm = vl.split(",");
491-
ovm = $.map(ovm, function (n) { return $.trim(n); });
492-
}
493492
if (typeof options.value === "function") { options.value = options.value(); }
494-
var so, sv, ov, svv, svt,
493+
var i, so, sv, ov, optionInfos = [], optionInfo,
495494
sep = options.separator === undefined ? ":" : options.separator,
496495
delim = options.delimiter === undefined ? ";" : options.delimiter,
497496
mapFunc = function (n, ii) { if (ii > 0) { return n; } };
@@ -502,30 +501,49 @@
502501
if (sv.length > 2) {
503502
sv[1] = $.map(sv, mapFunc).join(sep);
504503
}
505-
ov = document.createElement("option");
506-
// consider to trim BEFORE filling the options
507-
ov.value = sv[0];
508-
ov.innerHTML = sv[1];
509-
elem.appendChild(ov);
510-
svv = $.trim(sv[0]);
511-
svt = $.trim(sv[1]);
512-
if (!msl && (svv === $.trim(vl) || svt === $.trim(vl))) {
513-
ov.selected = "selected";
514-
}
515-
if (msl && ($.inArray(svt, ovm) > -1 || $.inArray(svv, ovm) > -1)) {
516-
ov.selected = "selected";
517-
}
504+
optionInfos.push({
505+
value: sv[0],
506+
innerHtml: sv[1],
507+
selectValue: $.trim(sv[0]),
508+
selectText: $.trim(sv[1])
509+
});
518510
}
519511
} else if (typeof options.value === "object") {
520512
var oSv = options.value, key;
521513
for (key in oSv) {
522514
if (oSv.hasOwnProperty(key)) {
523-
ov = document.createElement("option");
524-
ov.value = key;
525-
ov.innerHTML = oSv[key];
526-
elem.appendChild(ov);
527-
if (!msl && ($.trim(key) === $.trim(vl) || $.trim(oSv[key]) === $.trim(vl))) { ov.selected = "selected"; }
528-
if (msl && ($.inArray($.trim(oSv[key]), ovm) > -1 || $.inArray($.trim(key), ovm) > -1)) { ov.selected = "selected"; }
515+
optionInfos.push({
516+
value: key,
517+
innerHtml: oSv[key],
518+
selectValue: $.trim(key),
519+
selectText: $.trim(oSv[key])
520+
});
521+
}
522+
}
523+
}
524+
//$(elem).empty();
525+
for (i = 0; i < optionInfos.length; i++) {
526+
optionInfo = optionInfos[i];
527+
ov = document.createElement("option");
528+
ov.value = optionInfo.value;
529+
ov.innerHTML = optionInfo.innerHtml;
530+
elem.appendChild(ov);
531+
if (!msl && optionInfo.selectValue === $.trim(vl)) {
532+
ov.selected = "selected";
533+
isSelected = true;
534+
}
535+
if (msl && $.inArray(optionInfo.selectValue, ovm) > -1) {
536+
ov.selected = "selected";
537+
isSelected = true;
538+
}
539+
}
540+
if (!isSelected) {
541+
for (i = 0; i < optionInfo.length; i++) {
542+
if (!msl && optionInfo.selectText === $.trim(vl)) {
543+
ov.selected = "selected";
544+
}
545+
if (msl && $.inArray(optionInfo.selectText, ovm) > -1) {
546+
ov.selected = "selected";
529547
}
530548
}
531549
}

0 commit comments

Comments
 (0)