SelectOption.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /* SelectOption对象 */
  2. //构造函数
  3. var SelectOptionHelper = function() {}
  4. //属性及函数
  5. SelectOptionHelper.prototype =
  6. {
  7. /* private function */
  8. //moveUp和moveDown方法中使用
  9. swapOptionProperties:function(option1, option2)
  10. {
  11. var tempStr = option1.value;
  12. option1.value = option2.value;
  13. option2.value = tempStr;
  14. tempStr = option1.text;
  15. option1.text = option2.text;
  16. option2.text = tempStr;
  17. tempStr = option1.selected;
  18. option1.selected = option2.selected;
  19. option2.selected = tempStr;
  20. },
  21. //move和moveAll方法中使用
  22. resetAutoWidth:function(obj)
  23. {
  24. try
  25. {
  26. var tempWidth = obj.style.getExpression("width");
  27. if(tempWidth != null)
  28. {
  29. obj.style.width = "auto";
  30. obj.style.setExpression("width", tempWidth);
  31. obj.style.width = null;
  32. }
  33. }
  34. catch(e)
  35. {
  36. }
  37. },
  38. /* public function */
  39. //添加一个项
  40. add:function(toObj, objText, objValue)
  41. {
  42. toObj.options[toObj.options.length] = new Option(objText, objValue);
  43. },
  44. //移动选中的项到目标中
  45. move:function(fromObj, toObj)
  46. {
  47. var fromObjOptions = fromObj.options;
  48. for( var i = 0;i < fromObjOptions.length;i++)
  49. {
  50. if(fromObjOptions[i].selected)
  51. {
  52. toObj.appendChild(fromObjOptions[i]);
  53. i--;
  54. }
  55. }
  56. this.resetAutoWidth(fromObj);
  57. this.resetAutoWidth(toObj);
  58. },
  59. //移动所有的项到目标中
  60. moveAll:function(fromObj, toObj)
  61. {
  62. var fromObjOptions = fromObj.options;
  63. if(fromObjOptions.length > 1000)
  64. {
  65. //if(!confirm("Are you sure to move options?")) return false;
  66. }
  67. for( var i = 0;i < fromObjOptions.length;i++)
  68. {
  69. fromObjOptions[0].selected = true;
  70. toObj.appendChild(fromObjOptions[i]);
  71. i--;
  72. }
  73. this.resetAutoWidth(fromObj);
  74. this.resetAutoWidth(toObj);
  75. },
  76. //移除相应列表的所选项目
  77. removeSelectOptions:function(obj)
  78. {
  79. if(obj.selectedIndex == -1)
  80. {
  81. alert("未选择任何选项!");
  82. }
  83. for( var i = obj.length - 1;i >= 0;i--)
  84. {
  85. if(obj.options[i].selected)
  86. {
  87. obj.remove(obj.selectedIndex);
  88. //obj.appendChild(obj.options[i]);//把当前选中的选项移到最后面
  89. //obj.options.length = obj.options.length - 1;//把移到最后面的选项移去
  90. }
  91. }
  92. },
  93. //移除相应列表中的所有项目
  94. removeAllOptions:function(obj)
  95. {
  96. obj.options.length = 0;
  97. },
  98. //添加左边列表中所选项目到右边列表中
  99. addSelectOptions:function(objSource, objDestination)
  100. {
  101. if(objSource.selectedIndex == -1)
  102. {
  103. alert("未选择任何选项!");
  104. }
  105. var sourceLen = objSource.options.length;
  106. for( var i = 0;i < sourceLen;i++)
  107. {
  108. if(objSource.options[i].selected)
  109. {
  110. var optionValue = objSource.options[i].value;
  111. if(!this.existOptionByValue(optionValue, objDestination))
  112. {
  113. var optOption = new Option(objSource.options[i].text, objSource.options[i].value);
  114. objDestination.options[objDestination.options.length] = optOption;
  115. }
  116. }
  117. }
  118. },
  119. //添加左边列表中的全部项目到右边列表
  120. addAllOptions:function(objSource, objDes)
  121. {
  122. if(objSource.options.length == 0)
  123. {
  124. alert("源列表中无选项可添加!");
  125. return;
  126. }
  127. //清空目标列表
  128. objDes.options.length = 0;
  129. //添加
  130. for( var i = 0;i < objSource.options.length;i++)
  131. {
  132. var optOption = new Option(objSource.options[i].text, objSource.options[i].value);
  133. objDes.options[objDes.options.length] = optOption;
  134. }
  135. },
  136. //判断相应的选项是否在列表中存在
  137. existOptionByValue:function(optionValue, obj)
  138. {
  139. for( var i = 0;i < obj.options.length;i++)
  140. {
  141. if(optionValue == obj.options[i].value)
  142. return true;
  143. }
  144. return false;
  145. },
  146. //选中相应列表中的全部项
  147. selectAll:function(selectObj)
  148. {
  149. var theObjOptions = selectObj.options;
  150. for( var i = 0;i < theObjOptions.length;i++)
  151. {
  152. theObjOptions[i].selected = true;
  153. }
  154. },
  155. //取消选中相应列表中的全部项
  156. unSelectAll:function(selectObj)
  157. {
  158. var theObjOptions = selectObj.options;
  159. for( var i = 0;i < theObjOptions.length;i++)
  160. {
  161. theObjOptions[i].selected = false;
  162. }
  163. },
  164. //将选中的项目向上移动若干格
  165. moveUp:function(selectObj, count)
  166. {
  167. var theObjOptions = selectObj.options;
  168. for( var c = 0;c < count;c++)
  169. {
  170. for( var i = 1;i < theObjOptions.length;i++)
  171. {
  172. if(theObjOptions[i].selected && !theObjOptions[i - 1].selected)
  173. {
  174. this.swapOptionProperties(theObjOptions[i], theObjOptions[i - 1]);
  175. }
  176. }
  177. }
  178. },
  179. //将选中的项目向下移动若干格
  180. moveDown:function(selectObj, count)
  181. {
  182. var theObjOptions = selectObj.options;
  183. for( var c = 0;c < count;c++)
  184. {
  185. for( var i = theObjOptions.length - 2;i > -1;i--)
  186. {
  187. if(theObjOptions[i].selected && !theObjOptions[i + 1].selected)
  188. {
  189. this.swapOptionProperties(theObjOptions[i], theObjOptions[i + 1]);
  190. }
  191. }
  192. }
  193. },
  194. //将选中的项目移至最前
  195. moveTop:function(selectObj)
  196. {
  197. var theObjOptions = selectObj.options;
  198. var oOption = null;
  199. for( var i = 0;i < theObjOptions.length;i++)
  200. {
  201. if(theObjOptions[i].selected && oOption)
  202. {
  203. selectObj.insertBefore(theObjOptions[i], oOption);
  204. }
  205. else if(!oOption && !theObjOptions[i].selected)
  206. {
  207. oOption = theObjOptions[i];
  208. }
  209. }
  210. },
  211. //将选中的项目移至最后
  212. moveBottom:function(selectObj)
  213. {
  214. var theObjOptions = selectObj.options;
  215. var oOption = null;
  216. for( var i = theObjOptions.length - 1;i > -1;i--)
  217. {
  218. if(theObjOptions[i].selected)
  219. {
  220. if(oOption)
  221. {
  222. oOption = selectObj.insertBefore(theObjOptions[i], oOption);
  223. }
  224. else
  225. {
  226. oOption = selectObj.appendChild(theObjOptions[i]);
  227. }
  228. }
  229. }
  230. }
  231. }
  232. var __SelectOption__ = new SelectOptionHelper();//默认生成一个对象