ligerComboBox.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. /**
  2. * jQuery ligerUI 1.1.9
  3. *
  4. * http://ligerui.com
  5. *
  6. * Author daomi 2012 [ gd_star@163.com ]
  7. *
  8. */
  9. (function ($)
  10. {
  11. $.fn.ligerComboBox = function (options)
  12. {
  13. return $.ligerui.run.call(this, "ligerComboBox", arguments);
  14. };
  15. $.fn.ligerGetComboBoxManager = function ()
  16. {
  17. return $.ligerui.run.call(this, "ligerGetComboBoxManager", arguments);
  18. };
  19. $.ligerDefaults.ComboBox = {
  20. resize: true, //是否调整大小
  21. isMultiSelect: false, //是否多选
  22. isShowCheckBox: false, //是否选择复选框
  23. columns: false, //表格状态
  24. selectBoxWidth: false, //宽度
  25. selectBoxHeight: false, //高度
  26. onBeforeSelect: false, //选择前事件
  27. onSelected: null, //选择值事件
  28. initValue: null,
  29. initText: null,
  30. valueField: 'id',
  31. textField: 'text',
  32. valueFieldID: null,
  33. slide: true, //是否以动画的形式显示
  34. split: ";",
  35. data: null,
  36. tree: null, //下拉框以树的形式显示,tree的参数跟LigerTree的参数一致
  37. treeLeafOnly: true, //是否只选择叶子
  38. grid: null, //表格
  39. onStartResize: null,
  40. onEndResize: null,
  41. hideOnLoseFocus: true,
  42. url: null, //数据源URL(需返回JSON)
  43. onSuccess: null,
  44. onError: null,
  45. onBeforeOpen: null, //打开下拉框前事件,可以通过return false来阻止继续操作,利用这个参数可以用来调用其他函数,比如打开一个新窗口来选择值
  46. render: null, //文本框显示html函数
  47. absolute: true //选择框是否在附加到body,并绝对定位
  48. };
  49. //扩展方法
  50. $.ligerMethos.ComboBox = $.ligerMethos.ComboBox || {};
  51. $.ligerui.controls.ComboBox = function (element, options)
  52. {
  53. $.ligerui.controls.ComboBox.base.constructor.call(this, element, options);
  54. };
  55. $.ligerui.controls.ComboBox.ligerExtend($.ligerui.controls.Input, {
  56. __getType: function ()
  57. {
  58. return 'ComboBox';
  59. },
  60. _extendMethods: function ()
  61. {
  62. return $.ligerMethos.ComboBox;
  63. },
  64. _init: function ()
  65. {
  66. $.ligerui.controls.ComboBox.base._init.call(this);
  67. var p = this.options;
  68. if (p.columns)
  69. {
  70. p.isShowCheckBox = true;
  71. }
  72. if (p.isMultiSelect)
  73. {
  74. p.isShowCheckBox = true;
  75. }
  76. },
  77. _render: function ()
  78. {
  79. var g = this, p = this.options;
  80. g.data = p.data;
  81. g.inputText = null;
  82. g.select = null;
  83. g.textFieldID = "";
  84. g.valueFieldID = "";
  85. g.valueField = null; //隐藏域(保存值)
  86. //文本框初始化
  87. if (this.element.tagName.toLowerCase() == "input")
  88. {
  89. this.element.readOnly = true;
  90. g.inputText = $(this.element);
  91. g.textFieldID = this.element.id;
  92. }
  93. else if (this.element.tagName.toLowerCase() == "select")
  94. {
  95. $(this.element).hide();
  96. g.select = $(this.element);
  97. p.isMultiSelect = false;
  98. p.isShowCheckBox = false;
  99. g.textFieldID = this.element.id + "_txt";
  100. g.inputText = $('<input type="text" readonly="true"/>');
  101. g.inputText.attr("id", g.textFieldID).insertAfter($(this.element));
  102. } else
  103. {
  104. //不支持其他类型
  105. return;
  106. }
  107. if (g.inputText[0].name == undefined) g.inputText[0].name = g.textFieldID;
  108. //隐藏域初始化
  109. g.valueField = null;
  110. if (p.valueFieldID)
  111. {
  112. g.valueField = $("#" + p.valueFieldID + ":input");
  113. if (g.valueField.length == 0) g.valueField = $('<input type="hidden"/>');
  114. g.valueField[0].id = g.valueField[0].name = p.valueFieldID;
  115. }
  116. else
  117. {
  118. g.valueField = $('<input type="hidden"/>');
  119. g.valueField[0].id = g.valueField[0].name = g.textFieldID + "_val";
  120. }
  121. if (g.valueField[0].name == undefined) g.valueField[0].name = g.valueField[0].id;
  122. //开关
  123. g.link = $('<div class="l-trigger"><div class="l-trigger-icon"></div></div>');
  124. //下拉框
  125. g.selectBox = $('<div class="l-box-select"><div class="l-box-select-inner"><table cellpadding="0" cellspacing="0" border="0" class="l-box-select-table"></table></div></div>');
  126. g.selectBox.table = $("table:first", g.selectBox);
  127. //外层
  128. g.wrapper = g.inputText.wrap('<div class="l-text l-text-combobox"></div>').parent();
  129. g.wrapper.append('<div class="l-text-l"></div><div class="l-text-r"></div>');
  130. g.wrapper.append(g.link);
  131. //添加个包裹,
  132. g.textwrapper = g.wrapper.wrap('<div class="l-text-wrapper"></div>').parent();
  133. if (p.absolute)
  134. g.selectBox.appendTo('body').addClass("l-box-select-absolute");
  135. else
  136. g.textwrapper.append(g.selectBox);
  137. g.textwrapper.append(g.valueField);
  138. g.inputText.addClass("l-text-field");
  139. if (p.isShowCheckBox && !g.select)
  140. {
  141. $("table", g.selectBox).addClass("l-table-checkbox");
  142. } else
  143. {
  144. p.isShowCheckBox = false;
  145. $("table", g.selectBox).addClass("l-table-nocheckbox");
  146. }
  147. //开关 事件
  148. g.link.hover(function ()
  149. {
  150. if (p.disabled) return;
  151. this.className = "l-trigger-hover";
  152. }, function ()
  153. {
  154. if (p.disabled) return;
  155. this.className = "l-trigger";
  156. }).mousedown(function ()
  157. {
  158. if (p.disabled) return;
  159. this.className = "l-trigger-pressed";
  160. }).mouseup(function ()
  161. {
  162. if (p.disabled) return;
  163. this.className = "l-trigger-hover";
  164. }).click(function ()
  165. {
  166. if (p.disabled) return;
  167. if (g.trigger('beforeOpen') == false) return false;
  168. g._toggleSelectBox(g.selectBox.is(":visible"));
  169. });
  170. g.inputText.click(function ()
  171. {
  172. if (p.disabled) return;
  173. if (g.trigger('beforeOpen') == false) return false;
  174. g._toggleSelectBox(g.selectBox.is(":visible"));
  175. }).blur(function ()
  176. {
  177. if (p.disabled) return;
  178. g.wrapper.removeClass("l-text-focus");
  179. }).focus(function ()
  180. {
  181. if (p.disabled) return;
  182. g.wrapper.addClass("l-text-focus");
  183. });
  184. g.wrapper.hover(function ()
  185. {
  186. if (p.disabled) return;
  187. g.wrapper.addClass("l-text-over");
  188. }, function ()
  189. {
  190. if (p.disabled) return;
  191. g.wrapper.removeClass("l-text-over");
  192. });
  193. g.resizing = false;
  194. g.selectBox.hover(null, function (e)
  195. {
  196. if (p.hideOnLoseFocus && g.selectBox.is(":visible") && !g.boxToggling && !g.resizing)
  197. {
  198. g._toggleSelectBox(true);
  199. }
  200. });
  201. var itemsleng = $("tr", g.selectBox.table).length;
  202. if (!p.selectBoxHeight && itemsleng < 8) p.selectBoxHeight = itemsleng * 30;
  203. if (p.selectBoxHeight)
  204. {
  205. g.selectBox.height(p.selectBoxHeight);
  206. }
  207. //下拉框内容初始化
  208. g.bulidContent();
  209. g.set(p);
  210. //下拉框宽度、高度初始化
  211. if (p.selectBoxWidth)
  212. {
  213. g.selectBox.width(p.selectBoxWidth);
  214. }
  215. else
  216. {
  217. g.selectBox.css('width', g.wrapper.css('width'));
  218. }
  219. },
  220. destroy: function ()
  221. {
  222. if (this.wrapper) this.wrapper.remove();
  223. if (this.selectBox) this.selectBox.remove();
  224. this.options = null;
  225. $.ligerui.remove(this);
  226. },
  227. _setDisabled: function (value)
  228. {
  229. //禁用样式
  230. if (value)
  231. {
  232. this.wrapper.addClass('l-text-disabled');
  233. } else
  234. {
  235. this.wrapper.removeClass('l-text-disabled');
  236. }
  237. },
  238. _setLable: function (label)
  239. {
  240. var g = this, p = this.options;
  241. if (label)
  242. {
  243. if (g.labelwrapper)
  244. {
  245. g.labelwrapper.find(".l-text-label:first").html(label + ':&nbsp');
  246. }
  247. else
  248. {
  249. g.labelwrapper = g.textwrapper.wrap('<div class="l-labeltext"></div>').parent();
  250. g.labelwrapper.prepend('<div class="l-text-label" style="float:left;display:inline;">' + label + ':&nbsp</div>');
  251. g.textwrapper.css('float', 'left');
  252. }
  253. if (!p.labelWidth)
  254. {
  255. p.labelWidth = $('.l-text-label', g.labelwrapper).outerWidth();
  256. }
  257. else
  258. {
  259. $('.l-text-label', g.labelwrapper).outerWidth(p.labelWidth);
  260. }
  261. $('.l-text-label', g.labelwrapper).width(p.labelWidth);
  262. $('.l-text-label', g.labelwrapper).height(g.wrapper.height());
  263. g.labelwrapper.append('<br style="clear:both;" />');
  264. if (p.labelAlign)
  265. {
  266. $('.l-text-label', g.labelwrapper).css('text-align', p.labelAlign);
  267. }
  268. g.textwrapper.css({ display: 'inline' });
  269. g.labelwrapper.width(g.wrapper.outerWidth() + p.labelWidth + 2);
  270. }
  271. },
  272. _setWidth: function (value)
  273. {
  274. var g = this;
  275. if (value > 20)
  276. {
  277. g.wrapper.css({ width: value });
  278. g.inputText.css({ width: value - 20 });
  279. g.textwrapper.css({ width: value });
  280. }
  281. },
  282. _setHeight: function (value)
  283. {
  284. var g = this;
  285. if (value > 10)
  286. {
  287. g.wrapper.height(value);
  288. g.inputText.height(value - 2);
  289. g.link.height(value - 4);
  290. g.textwrapper.css({ width: value });
  291. }
  292. },
  293. _setResize: function (resize)
  294. {
  295. //调整大小支持
  296. if (resize && $.fn.ligerResizable)
  297. {
  298. var g = this;
  299. g.selectBox.ligerResizable({ handles: 'se,s,e', onStartResize: function ()
  300. {
  301. g.resizing = true;
  302. g.trigger('startResize');
  303. }
  304. , onEndResize: function ()
  305. {
  306. g.resizing = false;
  307. if (g.trigger('endResize') == false)
  308. return false;
  309. }
  310. });
  311. g.selectBox.append("<div class='l-btn-nw-drop'></div>");
  312. }
  313. },
  314. //查找Text,适用多选和单选
  315. findTextByValue: function (value)
  316. {
  317. var g = this, p = this.options;
  318. if (value == undefined) return "";
  319. var texts = "";
  320. var contain = function (checkvalue)
  321. {
  322. var targetdata = value.toString().split(p.split);
  323. for (var i = 0; i < targetdata.length; i++)
  324. {
  325. if (targetdata[i] == checkvalue) return true;
  326. }
  327. return false;
  328. };
  329. $(g.data).each(function (i, item)
  330. {
  331. var val = item[p.valueField];
  332. var txt = item[p.textField];
  333. if (contain(val))
  334. {
  335. texts += txt + p.split;
  336. }
  337. });
  338. if (texts.length > 0) texts = texts.substr(0, texts.length - 1);
  339. return texts;
  340. },
  341. //查找Value,适用多选和单选
  342. findValueByText: function (text)
  343. {
  344. var g = this, p = this.options;
  345. if (!text && text == "") return "";
  346. var contain = function (checkvalue)
  347. {
  348. var targetdata = text.toString().split(p.split);
  349. for (var i = 0; i < targetdata.length; i++)
  350. {
  351. if (targetdata[i] == checkvalue) return true;
  352. }
  353. return false;
  354. };
  355. var values = "";
  356. $(g.data).each(function (i, item)
  357. {
  358. var val = item[p.valueField];
  359. var txt = item[p.textField];
  360. if (contain(txt))
  361. {
  362. values += val + p.split;
  363. }
  364. });
  365. if (values.length > 0) values = values.substr(0, values.length - 1);
  366. return values;
  367. },
  368. removeItem: function ()
  369. {
  370. },
  371. insertItem: function ()
  372. {
  373. },
  374. addItem: function ()
  375. {
  376. },
  377. _setValue: function (value)
  378. {
  379. var g = this, p = this.options;
  380. var text = g.findTextByValue(value);
  381. if (p.tree)
  382. {
  383. g.selectValueByTree(value);
  384. }
  385. else if (!p.isMultiSelect)
  386. {
  387. g._changeValue(value, text);
  388. $("tr[value=" + value + "] td", g.selectBox).addClass("l-selected");
  389. $("tr[value!=" + value + "] td", g.selectBox).removeClass("l-selected");
  390. }
  391. else
  392. {
  393. g._changeValue(value, text);
  394. var targetdata = value.toString().split(p.split);
  395. $("table.l-table-checkbox :checkbox", g.selectBox).each(function () { this.checked = false; });
  396. for (var i = 0; i < targetdata.length; i++)
  397. {
  398. $("table.l-table-checkbox tr[value=" + targetdata[i] + "] :checkbox", g.selectBox).each(function () { this.checked = true; });
  399. }
  400. }
  401. },
  402. selectValue: function (value)
  403. {
  404. this._setValue(value);
  405. },
  406. bulidContent: function ()
  407. {
  408. var g = this, p = this.options;
  409. this.clearContent();
  410. if (g.select)
  411. {
  412. g.setSelect();
  413. }
  414. else if (g.data)
  415. {
  416. g.setData(g.data);
  417. }
  418. else if (p.tree)
  419. {
  420. g.setTree(p.tree);
  421. }
  422. else if (p.grid)
  423. {
  424. g.setGrid(p.grid);
  425. }
  426. else if (p.url)
  427. {
  428. $.ajax({
  429. type: 'post',
  430. url: p.url,
  431. cache: false,
  432. dataType: 'json',
  433. success: function (data)
  434. {
  435. g.data = data;
  436. g.setData(g.data);
  437. g.trigger('success', [g.data]);
  438. },
  439. error: function (XMLHttpRequest, textStatus)
  440. {
  441. g.trigger('error', [XMLHttpRequest, textStatus]);
  442. }
  443. });
  444. }
  445. },
  446. clearContent: function ()
  447. {
  448. var g = this, p = this.options;
  449. $("table", g.selectBox).html("");
  450. //g.inputText.val("");
  451. //g.valueField.val("");
  452. },
  453. setSelect: function ()
  454. {
  455. var g = this, p = this.options;
  456. this.clearContent();
  457. $('option', g.select).each(function (i)
  458. {
  459. var val = $(this).val();
  460. var txt = $(this).html();
  461. var tr = $("<tr><td index='" + i + "' value='" + val + "'>" + txt + "</td>");
  462. $("table.l-table-nocheckbox", g.selectBox).append(tr);
  463. $("td", tr).hover(function ()
  464. {
  465. $(this).addClass("l-over");
  466. }, function ()
  467. {
  468. $(this).removeClass("l-over");
  469. });
  470. });
  471. $('td:eq(' + g.select[0].selectedIndex + ')', g.selectBox).each(function ()
  472. {
  473. if ($(this).hasClass("l-selected"))
  474. {
  475. g.selectBox.hide();
  476. return;
  477. }
  478. $(".l-selected", g.selectBox).removeClass("l-selected");
  479. $(this).addClass("l-selected");
  480. if (g.select[0].selectedIndex != $(this).attr('index') && g.select[0].onchange)
  481. {
  482. g.select[0].selectedIndex = $(this).attr('index'); g.select[0].onchange();
  483. }
  484. var newIndex = parseInt($(this).attr('index'));
  485. g.select[0].selectedIndex = newIndex;
  486. g.select.trigger("change");
  487. g.selectBox.hide();
  488. var value = $(this).attr("value");
  489. var text = $(this).html();
  490. if (p.render)
  491. {
  492. g.inputText.val(p.render(value, text));
  493. }
  494. else
  495. {
  496. g.inputText.val(text);
  497. }
  498. });
  499. g._addClickEven();
  500. },
  501. setData: function (data)
  502. {
  503. var g = this, p = this.options;
  504. this.clearContent();
  505. if (!data || !data.length) return;
  506. if (g.data != data) g.data = data;
  507. if (p.columns)
  508. {
  509. g.selectBox.table.headrow = $("<tr class='l-table-headerow'><td width='18px'></td></tr>");
  510. g.selectBox.table.append(g.selectBox.table.headrow);
  511. g.selectBox.table.addClass("l-box-select-grid");
  512. for (var j = 0; j < p.columns.length; j++)
  513. {
  514. var headrow = $("<td columnindex='" + j + "' columnname='" + p.columns[j].name + "'>" + p.columns[j].header + "</td>");
  515. if (p.columns[j].width)
  516. {
  517. headrow.width(p.columns[j].width);
  518. }
  519. g.selectBox.table.headrow.append(headrow);
  520. }
  521. }
  522. for (var i = 0; i < data.length; i++)
  523. {
  524. var val = data[i][p.valueField];
  525. var txt = data[i][p.textField];
  526. if (!p.columns)
  527. {
  528. $("table.l-table-checkbox", g.selectBox).append("<tr value='" + val + "'><td style='width:18px;' index='" + i + "' value='" + val + "' text='" + txt + "' ><input type='checkbox' /></td><td index='" + i + "' value='" + val + "' align='left'>" + txt + "</td>");
  529. $("table.l-table-nocheckbox", g.selectBox).append("<tr value='" + val + "'><td index='" + i + "' value='" + val + "' align='left'>" + txt + "</td>");
  530. } else
  531. {
  532. var tr = $("<tr value='" + val + "'><td style='width:18px;' index='" + i + "' value='" + val + "' text='" + txt + "' ><input type='checkbox' /></td></tr>");
  533. $("td", g.selectBox.table.headrow).each(function ()
  534. {
  535. var columnname = $(this).attr("columnname");
  536. if (columnname)
  537. {
  538. var td = $("<td>" + data[i][columnname] + "</td>");
  539. tr.append(td);
  540. }
  541. });
  542. g.selectBox.table.append(tr);
  543. }
  544. }
  545. //自定义复选框支持
  546. if (p.isShowCheckBox && $.fn.ligerCheckBox)
  547. {
  548. $("table input:checkbox", g.selectBox).ligerCheckBox();
  549. }
  550. $(".l-table-checkbox input:checkbox", g.selectBox).change(function ()
  551. {
  552. if (this.checked && g.hasBind('beforeSelect'))
  553. {
  554. var parentTD = null;
  555. if ($(this).parent().get(0).tagName.toLowerCase() == "div")
  556. {
  557. parentTD = $(this).parent().parent();
  558. } else
  559. {
  560. parentTD = $(this).parent();
  561. }
  562. if (parentTD != null && g.trigger('beforeSelect', [parentTD.attr("value"), parentTD.attr("text")]) == false)
  563. {
  564. g.selectBox.slideToggle("fast");
  565. return false;
  566. }
  567. }
  568. if (!p.isMultiSelect)
  569. {
  570. if (this.checked)
  571. {
  572. $("input:checked", g.selectBox).not(this).each(function ()
  573. {
  574. this.checked = false;
  575. $(".l-checkbox-checked", $(this).parent()).removeClass("l-checkbox-checked");
  576. });
  577. g.selectBox.slideToggle("fast");
  578. }
  579. }
  580. g._checkboxUpdateValue();
  581. });
  582. $("table.l-table-nocheckbox td", g.selectBox).hover(function ()
  583. {
  584. $(this).addClass("l-over");
  585. }, function ()
  586. {
  587. $(this).removeClass("l-over");
  588. });
  589. g._addClickEven();
  590. //选择项初始化
  591. g._dataInit();
  592. },
  593. //树
  594. setTree: function (tree)
  595. {
  596. var g = this, p = this.options;
  597. this.clearContent();
  598. g.selectBox.table.remove();
  599. if (tree.checkbox != false)
  600. {
  601. tree.onCheck = function ()
  602. {
  603. var nodes = g.treeManager.getChecked();
  604. var value = [];
  605. var text = [];
  606. $(nodes).each(function (i, node)
  607. {
  608. if (p.treeLeafOnly && node.data.children) return;
  609. value.push(node.data[p.valueField]);
  610. text.push(node.data[p.textField]);
  611. });
  612. g._changeValue(value.join(p.split), text.join(p.split));
  613. };
  614. }
  615. else
  616. {
  617. tree.onSelect = function (node)
  618. {
  619. if (p.treeLeafOnly && node.data.children) return;
  620. var value = node.data[p.valueField];
  621. var text = node.data[p.textField];
  622. g._changeValue(value, text);
  623. };
  624. tree.onCancelSelect = function (node)
  625. {
  626. g._changeValue("", "");
  627. };
  628. }
  629. tree.onAfterAppend = function (domnode, nodedata)
  630. {
  631. if (!g.treeManager) return;
  632. var value = null;
  633. if (p.initValue) value = p.initValue;
  634. else if (g.valueField.val() != "") value = g.valueField.val();
  635. g.selectValueByTree(value);
  636. };
  637. g.tree = $("<ul></ul>");
  638. $("div:first", g.selectBox).append(g.tree);
  639. g.tree.ligerTree(tree);
  640. g.treeManager = g.tree.ligerGetTreeManager();
  641. },
  642. selectValueByTree: function (value)
  643. {
  644. var g = this, p = this.options;
  645. if (value != null)
  646. {
  647. var text = "";
  648. var valuelist = value.toString().split(p.split);
  649. $(valuelist).each(function (i, item)
  650. {
  651. g.treeManager.selectNode(item.toString());
  652. text += g.treeManager.getTextByID(item);
  653. if (i < valuelist.length - 1) text += p.split;
  654. });
  655. g._changeValue(value, text);
  656. }
  657. },
  658. //表格
  659. setGrid: function (grid)
  660. {
  661. var g = this, p = this.options;
  662. this.clearContent();
  663. g.selectBox.table.remove();
  664. g.grid = $("div:first", g.selectBox);
  665. grid.columnWidth = grid.columnWidth || 120;
  666. grid.width = "100%";
  667. grid.height = "100%";
  668. grid.heightDiff = -2;
  669. grid.InWindow = false;
  670. g.gridManager = g.grid.ligerGrid(grid);
  671. p.hideOnLoseFocus = false;
  672. if (grid.checkbox != false)
  673. {
  674. var onCheckRow = function ()
  675. {
  676. var rowsdata = g.gridManager.getCheckedRows();
  677. var value = [];
  678. var text = [];
  679. $(rowsdata).each(function (i, rowdata)
  680. {
  681. value.push(rowdata[p.valueField]);
  682. text.push(rowdata[p.textField]);
  683. });
  684. g._changeValue(value.join(p.split), text.join(p.split));
  685. };
  686. g.gridManager.bind('CheckAllRow', onCheckRow);
  687. g.gridManager.bind('CheckRow', onCheckRow);
  688. }
  689. else
  690. {
  691. g.gridManager.bind('SelectRow', function (rowdata, rowobj, index)
  692. {
  693. var value = rowdata[p.valueField];
  694. var text = rowdata[p.textField];
  695. g._changeValue(value, text);
  696. });
  697. g.gridManager.bind('UnSelectRow', function (rowdata, rowobj, index)
  698. {
  699. g._changeValue("", "");
  700. });
  701. }
  702. g.bind('show', function ()
  703. {
  704. if (g.gridManager)
  705. {
  706. g.gridManager._updateFrozenWidth();
  707. }
  708. });
  709. g.bind('endResize', function ()
  710. {
  711. if (g.gridManager)
  712. {
  713. g.gridManager._updateFrozenWidth();
  714. g.gridManager.setHeight(g.selectBox.height() - 2);
  715. }
  716. });
  717. },
  718. _getValue: function ()
  719. {
  720. return $(this.valueField).val();
  721. },
  722. getValue: function ()
  723. {
  724. //获取值
  725. return this._getValue();
  726. },
  727. updateStyle: function ()
  728. {
  729. var g = this, p = this.options;
  730. g._dataInit();
  731. },
  732. _dataInit: function ()
  733. {
  734. var g = this, p = this.options;
  735. var value = null;
  736. if (p.initValue != null && p.initText != null)
  737. {
  738. g._changeValue(p.initValue, p.initText);
  739. }
  740. //根据值来初始化
  741. if (p.initValue != null)
  742. {
  743. value = p.initValue;
  744. if (p.tree)
  745. {
  746. if(value)
  747. g.selectValueByTree(value);
  748. }
  749. else
  750. {
  751. var text = g.findTextByValue(value);
  752. g._changeValue(value, text);
  753. }
  754. }
  755. //根据文本来初始化
  756. else if (p.initText != null)
  757. {
  758. value = g.findValueByText(p.initText);
  759. g._changeValue(value, p.initText);
  760. }
  761. else if (g.valueField.val() != "")
  762. {
  763. value = g.valueField.val();
  764. if (p.tree)
  765. {
  766. if(value)
  767. g.selectValueByTree(value);
  768. }
  769. else
  770. {
  771. var text = g.findTextByValue(value);
  772. g._changeValue(value, text);
  773. }
  774. }
  775. if (!p.isShowCheckBox && value != null)
  776. {
  777. $("table tr", g.selectBox).find("td:first").each(function ()
  778. {
  779. if (value == $(this).attr("value"))
  780. {
  781. $(this).addClass("l-selected");
  782. }
  783. });
  784. }
  785. if (p.isShowCheckBox && value != null)
  786. {
  787. $(":checkbox", g.selectBox).each(function ()
  788. {
  789. var parentTD = null;
  790. var checkbox = $(this);
  791. if (checkbox.parent().get(0).tagName.toLowerCase() == "div")
  792. {
  793. parentTD = checkbox.parent().parent();
  794. } else
  795. {
  796. parentTD = checkbox.parent();
  797. }
  798. if (parentTD == null) return;
  799. var valuearr = value.toString().split(p.split);
  800. $(valuearr).each(function (i, item)
  801. {
  802. if (item == parentTD.attr("value"))
  803. {
  804. $(".l-checkbox", parentTD).addClass("l-checkbox-checked");
  805. checkbox[0].checked = true;
  806. }
  807. });
  808. });
  809. }
  810. },
  811. //设置值到 文本框和隐藏域
  812. _changeValue: function (newValue, newText)
  813. {
  814. var g = this, p = this.options;
  815. g.valueField.val(newValue);
  816. if (p.render)
  817. {
  818. g.inputText.val(p.render(newValue, newText));
  819. }
  820. else
  821. {
  822. g.inputText.val(newText);
  823. }
  824. g.selectedValue = newValue;
  825. g.selectedText = newText;
  826. g.inputText.trigger("change").focus();
  827. g.trigger('selected', [newValue, newText]);
  828. },
  829. //更新选中的值(复选框)
  830. _checkboxUpdateValue: function ()
  831. {
  832. var g = this, p = this.options;
  833. var valueStr = "";
  834. var textStr = "";
  835. $("input:checked", g.selectBox).each(function ()
  836. {
  837. var parentTD = null;
  838. if ($(this).parent().get(0).tagName.toLowerCase() == "div")
  839. {
  840. parentTD = $(this).parent().parent();
  841. } else
  842. {
  843. parentTD = $(this).parent();
  844. }
  845. if (!parentTD) return;
  846. valueStr += parentTD.attr("value") + p.split;
  847. textStr += parentTD.attr("text") + p.split;
  848. });
  849. if (valueStr.length > 0) valueStr = valueStr.substr(0, valueStr.length - 1);
  850. if (textStr.length > 0) textStr = textStr.substr(0, textStr.length - 1);
  851. g._changeValue(valueStr, textStr);
  852. },
  853. _addClickEven: function ()
  854. {
  855. var g = this, p = this.options;
  856. //选项点击
  857. $(".l-table-nocheckbox td", g.selectBox).click(function ()
  858. {
  859. var value = $(this).attr("value");
  860. var index = parseInt($(this).attr('index'));
  861. var text = $(this).html();
  862. if (g.hasBind('beforeSelect') && g.trigger('beforeSelect', [value, text]) == false)
  863. {
  864. if (p.slide) g.selectBox.slideToggle("fast");
  865. else g.selectBox.hide();
  866. return false;
  867. }
  868. if ($(this).hasClass("l-selected"))
  869. {
  870. if (p.slide) g.selectBox.slideToggle("fast");
  871. else g.selectBox.hide();
  872. return;
  873. }
  874. $(".l-selected", g.selectBox).removeClass("l-selected");
  875. $(this).addClass("l-selected");
  876. if (g.select)
  877. {
  878. if (g.select[0].selectedIndex != index)
  879. {
  880. g.select[0].selectedIndex = index;
  881. g.select.trigger("change");
  882. }
  883. }
  884. if (p.slide)
  885. {
  886. g.boxToggling = true;
  887. g.selectBox.hide("fast", function ()
  888. {
  889. g.boxToggling = false;
  890. })
  891. } else g.selectBox.hide();
  892. g._changeValue(value, text);
  893. });
  894. },
  895. updateSelectBoxPosition: function ()
  896. {
  897. var g = this, p = this.options;
  898. if (p.absolute)
  899. {
  900. g.selectBox.css({ left: g.wrapper.offset().left, top: g.wrapper.offset().top + 1 + g.wrapper.outerHeight() });
  901. }
  902. else
  903. {
  904. var topheight = g.wrapper.offset().top - $(window).scrollTop();
  905. var selfheight = g.selectBox.height() + textHeight + 4;
  906. if (topheight + selfheight > $(window).height() && topheight > selfheight)
  907. {
  908. g.selectBox.css("marginTop", -1 * (g.selectBox.height() + textHeight + 5));
  909. }
  910. }
  911. },
  912. _toggleSelectBox: function (isHide)
  913. {
  914. var g = this, p = this.options;
  915. var textHeight = g.wrapper.height();
  916. g.boxToggling = true;
  917. if (isHide)
  918. {
  919. if (p.slide)
  920. {
  921. g.selectBox.slideToggle('fast', function ()
  922. {
  923. g.boxToggling = false;
  924. });
  925. }
  926. else
  927. {
  928. g.selectBox.hide();
  929. g.boxToggling = false;
  930. }
  931. }
  932. else
  933. {
  934. g.updateSelectBoxPosition();
  935. if (p.slide)
  936. {
  937. g.selectBox.slideToggle('fast', function ()
  938. {
  939. g.boxToggling = false;
  940. if (!p.isShowCheckBox && $('td.l-selected', g.selectBox).length > 0)
  941. {
  942. var offSet = ($('td.l-selected', g.selectBox).offset().top - g.selectBox.offset().top);
  943. $(".l-box-select-inner", g.selectBox).animate({ scrollTop: offSet });
  944. }
  945. });
  946. }
  947. else
  948. {
  949. g.selectBox.show();
  950. g.boxToggling = false;
  951. if (!g.tree && !g.grid && !p.isShowCheckBox && $('td.l-selected', g.selectBox).length > 0)
  952. {
  953. var offSet = ($('td.l-selected', g.selectBox).offset().top - g.selectBox.offset().top);
  954. $(".l-box-select-inner", g.selectBox).animate({ scrollTop: offSet });
  955. }
  956. }
  957. }
  958. g.isShowed = g.selectBox.is(":visible");
  959. g.trigger('toggle', [isHide]);
  960. g.trigger(isHide ? 'hide' : 'show');
  961. }
  962. });
  963. $.ligerui.controls.ComboBox.prototype.setValue = $.ligerui.controls.ComboBox.prototype.selectValue;
  964. //设置文本框和隐藏控件的值
  965. $.ligerui.controls.ComboBox.prototype.setInputValue = $.ligerui.controls.ComboBox.prototype._changeValue;
  966. })(jQuery);