From 860f4f8d0a7a29820693cac8258933e93ced0fe8 Mon Sep 17 00:00:00 2001 From: "kevin.luo" Date: Mon, 19 Jan 2015 17:17:38 +0800 Subject: [PATCH] Fix that allow binding key value to select component --- ui/select.reel/select.js | 72 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 69 insertions(+), 3 deletions(-) diff --git a/ui/select.reel/select.js b/ui/select.reel/select.js index 30a8df7..ef65442 100644 --- a/ui/select.reel/select.js +++ b/ui/select.reel/select.js @@ -80,14 +80,80 @@ exports.Select = AbstractSelect.specialize(/** @lends Select */{ } // Select the current value. - selectedIndex = organizedContent.indexOf(this.value); + + if (this.value) { + selectedIndex = this.getValueIndex(this.value, organizedContent); + }else{ + selectedIndex = 0; + } + + //selectedIndex = organizedContent.indexOf(this.value); if (selectedIndex == -1) { selectedIndex = 0; } - if (this.element.selectedIndex !== selectedIndex) { - this.element.selectedIndex = selectedIndex; + this.element.selectedIndex = selectedIndex; + } + }, + + getValueIndex: { + value: function (value, options) { + + var selectedIndex = -1; + var firstItem; + var itemType; + + //If options length > 0, get the first item object type + if (options && options.length > 0) { + firstItem = options[0]; + itemType = typeof firstItem; + } else { + return selectedIndex; } + + //Judge if we directly match the item or we match the value + if (typeof value == itemType) { + for (var i = 0, n = options.length; i < n; i++) { + var item = options[i]; + + //If the select has valuePropertyName, use item[this.valuePropertyName] to do compare with value[this.valuePropertyName] + //Otherwise, compare the item and value directly + if (this.valuePropertyName) { + if (item[this.valuePropertyName] == value[this.valuePropertyName]) { + selectedIndex = i; + break; + } + } else { + if (item == value) { + selectedIndex = i; + break; + } + } + } + } else { + + + for (i = 0, n = options.length; i < n; i++) { + item = options[i]; + + //If the select has valuePropertyName, use item[this.valuePropertyName] to compare with value + //Because value should be the key value. + if (this.valuePropertyName) { + if (item[this.valuePropertyName] == value) { + selectedIndex = i; + break; + } + } else { + if (item == value) { + selectedIndex = i; + break; + } + } + + } + } + + return selectedIndex; } },