UU跑腿标准版

ligerComboBox.js 49KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  1. /**
  2. * jQuery ligerUI 1.2.4
  3. *
  4. * http://ligerui.com
  5. *
  6. * Author daomi 2014 [ 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: null, //表格状态
  24. selectBoxWidth: null, //宽度
  25. selectBoxHeight: 120, //高度
  26. onBeforeSelect: false, //选择前事件
  27. onSelected: null, //选择值事件
  28. initValue: null,
  29. initText: null,
  30. valueField: 'id',
  31. textField: 'text',
  32. valueFieldID: null,
  33. slide: false, //是否以动画的形式显示
  34. split: ";",
  35. data: null,
  36. tree: null, //下拉框以树的形式显示,tree的参数跟LigerTree的参数一致
  37. treeLeafOnly: true, //是否只选择叶子
  38. condition: null, //列表条件搜索 参数同 ligerForm
  39. grid: null, //表格 参数同 ligerGrid
  40. onStartResize: null,
  41. onEndResize: null,
  42. hideOnLoseFocus: true,
  43. hideGridOnLoseFocus : false,
  44. url: null, //数据源URL(需返回JSON)
  45. emptyText: null, //空行
  46. addRowButton: '新增', //新增按钮
  47. addRowButtonClick: null, //新增事件
  48. triggerIcon : null, //
  49. onSuccess: null,
  50. onError: null,
  51. onBeforeOpen: null, //打开下拉框前事件,可以通过return false来阻止继续操作,利用这个参数可以用来调用其他函数,比如打开一个新窗口来选择值
  52. onButtonClick: null, //右侧图标按钮事件,可以通过return false来阻止继续操作,利用这个参数可以用来调用其他函数,比如打开一个新窗口来选择值
  53. render: null, //文本框显示html函数
  54. absolute: true, //选择框是否在附加到body,并绝对定位
  55. cancelable: true, //可取消选择
  56. css: null, //附加css
  57. parms: null, //ajax提交表单
  58. renderItem : null, //选项自定义函数
  59. autocomplete: false, //自动完成
  60. highLight : false, //自动完成是否匹配字符高亮显示
  61. readonly: false, //是否只读
  62. ajaxType: 'post',
  63. alwayShowInTop: false, //下拉框是否一直显示在上方
  64. valueFieldCssClass : null
  65. };
  66. $.ligerDefaults.ComboBoxString = {
  67. Search : "搜索"
  68. };
  69. //扩展方法
  70. $.ligerMethos.ComboBox = $.ligerMethos.ComboBox || {};
  71. $.ligerui.controls.ComboBox = function (element, options)
  72. {
  73. $.ligerui.controls.ComboBox.base.constructor.call(this, element, options);
  74. };
  75. $.ligerui.controls.ComboBox.ligerExtend($.ligerui.controls.Input, {
  76. __getType: function ()
  77. {
  78. return 'ComboBox';
  79. },
  80. _extendMethods: function ()
  81. {
  82. return $.ligerMethos.ComboBox;
  83. },
  84. _init: function ()
  85. {
  86. $.ligerui.controls.ComboBox.base._init.call(this);
  87. var p = this.options;
  88. if (p.columns)
  89. {
  90. p.isShowCheckBox = true;
  91. }
  92. if (p.isMultiSelect)
  93. {
  94. p.isShowCheckBox = true;
  95. }
  96. },
  97. _render: function ()
  98. {
  99. var g = this, p = this.options;
  100. g.data = p.data;
  101. g.inputText = null;
  102. g.select = null;
  103. g.textFieldID = "";
  104. g.valueFieldID = "";
  105. g.valueField = null; //隐藏域(保存值)
  106. //文本框初始化
  107. if (this.element.tagName.toLowerCase() == "input")
  108. {
  109. this.element.readOnly = true;
  110. g.inputText = $(this.element);
  111. g.textFieldID = this.element.id;
  112. }
  113. else if (this.element.tagName.toLowerCase() == "select")
  114. {
  115. $(this.element).hide();
  116. g.select = $(this.element);
  117. p.isMultiSelect = false;
  118. p.isShowCheckBox = false;
  119. p.cancelable = false;
  120. g.textFieldID = this.element.id + "_txt";
  121. g.inputText = $('<input type="text" readonly="true"/>');
  122. g.inputText.attr("id", g.textFieldID).insertAfter($(this.element));
  123. }
  124. if (g.inputText[0].name == undefined) g.inputText[0].name = g.textFieldID;
  125. //隐藏域初始化
  126. g.valueField = null;
  127. if (p.valueFieldID)
  128. {
  129. g.valueField = $("#" + p.valueFieldID + ":input,[name=" + p.valueFieldID + "]:input").filter("input:hidden");
  130. if (g.valueField.length == 0) g.valueField = $('<input type="hidden"/>');
  131. g.valueField[0].id = g.valueField[0].name = p.valueFieldID;
  132. }
  133. else
  134. {
  135. g.valueField = $('<input type="hidden"/>');
  136. g.valueField[0].id = g.valueField[0].name = g.textFieldID + "_val";
  137. }
  138. if (p.valueFieldCssClass)
  139. {
  140. g.valueField.addClass(p.valueFieldCssClass);
  141. }
  142. if (g.valueField[0].name == undefined) g.valueField[0].name = g.valueField[0].id;
  143. //update by superzoc 增加初始值
  144. if (p.initValue != null) g.valueField[0].value = p.initValue;
  145. g.valueField.attr("data-ligerid", g.id);
  146. //开关
  147. g.link = $('<div class="l-trigger"><div class="l-trigger-icon"></div></div>');
  148. if (p.triggerIcon) g.link.find("div:first").addClass(p.triggerIcon);
  149. //下拉框
  150. g.selectBox = $('<div class="l-box-select" style="display:none"><div class="l-box-select-inner"><table cellpadding="0" cellspacing="0" border="0" class="l-box-select-table"></table></div></div>');
  151. g.selectBox.table = $("table:first", g.selectBox);
  152. g.selectBoxInner = g.selectBox.find(".l-box-select-inner:first");
  153. //外层
  154. g.wrapper = g.inputText.wrap('<div class="l-text l-text-combobox"></div>').parent();
  155. g.wrapper.append('<div class="l-text-l"></div><div class="l-text-r"></div>');
  156. g.wrapper.append(g.link);
  157. //添加个包裹,
  158. g.textwrapper = g.wrapper.wrap('<div class="l-text-wrapper"></div>').parent();
  159. if (p.absolute)
  160. g.selectBox.appendTo('body').addClass("l-box-select-absolute");
  161. else
  162. g.textwrapper.append(g.selectBox);
  163. g.textwrapper.append(g.valueField);
  164. g.inputText.addClass("l-text-field");
  165. if (p.isShowCheckBox && !g.select)
  166. {
  167. $("table", g.selectBox).addClass("l-table-checkbox");
  168. } else
  169. {
  170. p.isShowCheckBox = false;
  171. $("table", g.selectBox).addClass("l-table-nocheckbox");
  172. }
  173. //开关 事件
  174. g.link.hover(function ()
  175. {
  176. if (p.disabled || p.readonly) return;
  177. this.className = "l-trigger-hover";
  178. }, function ()
  179. {
  180. if (p.disabled || p.readonly) return;
  181. this.className = "l-trigger";
  182. }).mousedown(function ()
  183. {
  184. if (p.disabled || p.readonly) return;
  185. this.className = "l-trigger-pressed";
  186. }).mouseup(function ()
  187. {
  188. if (p.disabled || p.readonly) return;
  189. this.className = "l-trigger-hover";
  190. }).click(function ()
  191. {
  192. if (p.disabled || p.readonly) return;
  193. if (g.trigger('buttonClick') == false) return false;
  194. if (g.trigger('beforeOpen') == false) return false;
  195. g._toggleSelectBox(g.selectBox.is(":visible"));
  196. });
  197. g.inputText.click(function ()
  198. {
  199. if (p.disabled || p.readonly) return;
  200. if (g.trigger('beforeOpen') == false) return false;
  201. g._toggleSelectBox(g.selectBox.is(":visible"));
  202. }).blur(function ()
  203. {
  204. if (p.disabled) return;
  205. g.wrapper.removeClass("l-text-focus");
  206. }).focus(function ()
  207. {
  208. if (p.disabled || p.readonly) return;
  209. g.wrapper.addClass("l-text-focus");
  210. });
  211. g.wrapper.hover(function ()
  212. {
  213. if (p.disabled || p.readonly) return;
  214. g.wrapper.addClass("l-text-over");
  215. }, function ()
  216. {
  217. if (p.disabled || p.readonly) return;
  218. g.wrapper.removeClass("l-text-over");
  219. });
  220. g.resizing = false;
  221. g.selectBox.hover(null, function (e)
  222. {
  223. if (p.hideOnLoseFocus && g.selectBox.is(":visible") && !g.boxToggling && !g.resizing)
  224. {
  225. g._toggleSelectBox(true);
  226. }
  227. });
  228. //下拉框内容初始化
  229. g.bulidContent();
  230. g.set(p);
  231. //下拉框宽度、高度初始化
  232. if (p.selectBoxWidth)
  233. {
  234. g.selectBox.width(p.selectBoxWidth);
  235. }
  236. else
  237. {
  238. g.selectBox.css('width', g.wrapper.css('width'));
  239. }
  240. if (p.grid) {
  241. g.bind('show', function () {
  242. if (!g.grid) {
  243. g.setGrid(p.grid);
  244. g.set('SelectBoxHeight', p.selectBoxHeight);
  245. }
  246. });
  247. }
  248. g.updateSelectBoxPosition();
  249. $(document).bind("click.combobox", function (e)
  250. {
  251. var tag = (e.target || e.srcElement).tagName.toLowerCase();
  252. if (tag == "html" || tag == "body")
  253. {
  254. g._toggleSelectBox(true);
  255. }
  256. });
  257. },
  258. destroy: function ()
  259. {
  260. if (this.wrapper) this.wrapper.remove();
  261. if (this.selectBox) this.selectBox.remove();
  262. this.options = null;
  263. $.ligerui.remove(this);
  264. },
  265. clear : function()
  266. {
  267. this._changeValue("", "");
  268. $("a.l-checkbox-checked", this.selectBox).removeClass("l-checkbox-checked");
  269. $("td.l-selected", this.selectBox).removeClass("l-selected");
  270. $(":checkbox", this.selectBox).each(function () { this.checked = false; });
  271. this.trigger('clear');
  272. },
  273. _setSelectBoxHeight: function (height)
  274. {
  275. if (!height) return;
  276. var g = this, p = this.options;
  277. if (p.grid) {
  278. g.grid && g.grid.set('height', g.getGridHeight(height));
  279. } else
  280. {
  281. var itemsleng = $("tr", g.selectBox.table).length;
  282. if (!p.selectBoxHeight && itemsleng < 8) p.selectBoxHeight = itemsleng * 30;
  283. if (p.selectBoxHeight)
  284. {
  285. if (itemsleng < 8)
  286. {
  287. g.selectBoxInner.height('auto');
  288. } else
  289. {
  290. g.selectBoxInner.height(p.selectBoxHeight);
  291. }
  292. }
  293. }
  294. },
  295. _setCss : function(css)
  296. {
  297. if (css) {
  298. this.wrapper.addClass(css);
  299. }
  300. },
  301. //取消选择
  302. _setCancelable: function (value)
  303. {
  304. var g = this, p = this.options;
  305. if (!value && g.unselect) {
  306. g.unselect.remove();
  307. g.unselect = null;
  308. }
  309. if (!value && !g.unselect) return;
  310. g.unselect = $('<div class="l-trigger l-trigger-cancel"><div class="l-trigger-icon"></div></div>').hide();
  311. g.wrapper.hover(function () {
  312. g.unselect.show();
  313. }, function () {
  314. g.unselect.hide();
  315. })
  316. if (!p.disabled && !p.readonly && p.cancelable) {
  317. g.wrapper.append(g.unselect);
  318. }
  319. g.unselect.hover(function () {
  320. this.className = "l-trigger-hover l-trigger-cancel";
  321. }, function () {
  322. this.className = "l-trigger l-trigger-cancel";
  323. }).click(function () {
  324. g.clear();
  325. });
  326. },
  327. _setDisabled: function (value)
  328. {
  329. //禁用样式
  330. if (value)
  331. {
  332. this.wrapper.addClass('l-text-disabled');
  333. } else
  334. {
  335. this.wrapper.removeClass('l-text-disabled');
  336. }
  337. },
  338. _setReadonly: function (readonly)
  339. {
  340. if (readonly)
  341. {
  342. this.wrapper.addClass("l-text-readonly");
  343. } else
  344. {
  345. this.wrapper.removeClass("l-text-readonly");
  346. }
  347. },
  348. _setLable: function (label)
  349. {
  350. var g = this, p = this.options;
  351. if (label)
  352. {
  353. if (g.labelwrapper)
  354. {
  355. g.labelwrapper.find(".l-text-label:first").html(label + ':&nbsp');
  356. }
  357. else
  358. {
  359. g.labelwrapper = g.textwrapper.wrap('<div class="l-labeltext"></div>').parent();
  360. g.labelwrapper.prepend('<div class="l-text-label" style="float:left;display:inline;">' + label + ':&nbsp</div>');
  361. g.textwrapper.css('float', 'left');
  362. }
  363. if (!p.labelWidth)
  364. {
  365. p.labelWidth = $('.l-text-label', g.labelwrapper).outerWidth();
  366. }
  367. else
  368. {
  369. $('.l-text-label', g.labelwrapper).outerWidth(p.labelWidth);
  370. }
  371. $('.l-text-label', g.labelwrapper).width(p.labelWidth);
  372. $('.l-text-label', g.labelwrapper).height(g.wrapper.height());
  373. g.labelwrapper.append('<br style="clear:both;" />');
  374. if (p.labelAlign)
  375. {
  376. $('.l-text-label', g.labelwrapper).css('text-align', p.labelAlign);
  377. }
  378. g.textwrapper.css({ display: 'inline' });
  379. g.labelwrapper.width(g.wrapper.outerWidth() + p.labelWidth + 2);
  380. }
  381. },
  382. _setWidth: function (value)
  383. {
  384. var g = this, p = this.options;
  385. if (value > 20)
  386. {
  387. g.wrapper.css({ width: value });
  388. g.inputText.css({ width: value - 20 });
  389. if (!p.selectBoxWidth) {
  390. g.selectBox.css({ width: value });
  391. }
  392. }
  393. },
  394. _setHeight: function (value)
  395. {
  396. var g = this;
  397. if (value > 10)
  398. {
  399. g.wrapper.height(value);
  400. g.inputText.height(value - 2);
  401. }
  402. },
  403. _setResize: function (resize)
  404. {
  405. var g = this, p = this.options;
  406. if (p.columns) {
  407. return;
  408. }
  409. //调整大小支持
  410. if (resize && $.fn.ligerResizable)
  411. {
  412. var handles = p.selectBoxHeight ? 'e' : 'se,s,e';
  413. g.selectBox.ligerResizable({
  414. handles: handles, onStartResize: function ()
  415. {
  416. g.resizing = true;
  417. g.trigger('startResize');
  418. }, onEndResize: function ()
  419. {
  420. g.resizing = false;
  421. if (g.trigger('endResize') == false)
  422. return false;
  423. }, onStopResize: function (current, e) {
  424. if (g.grid) {
  425. if (current.newWidth) {
  426. g.selectBox.width(current.newWidth);
  427. }
  428. if (current.newHeight) {
  429. g.set({ selectBoxHeight: current.newHeight });
  430. }
  431. g.grid.refreshSize();
  432. g.trigger('endResize');
  433. return false;
  434. }
  435. return true;
  436. }
  437. });
  438. g.selectBox.append("<div class='l-btn-nw-drop'></div>");
  439. }
  440. },
  441. //查找Text,适用多选和单选
  442. findTextByValue: function (value)
  443. {
  444. var g = this, p = this.options;
  445. if (value == null) return "";
  446. var texts = "";
  447. var contain = function (checkvalue)
  448. {
  449. var targetdata = value.toString().split(p.split);
  450. for (var i = 0; i < targetdata.length; i++)
  451. {
  452. if (targetdata[i] == checkvalue) return true;
  453. }
  454. return false;
  455. };
  456. $(g.data).each(function (i, item)
  457. {
  458. var val = item[p.valueField];
  459. var txt = item[p.textField];
  460. if (contain(val))
  461. {
  462. texts += txt + p.split;
  463. }
  464. });
  465. if (texts.length > 0) texts = texts.substr(0, texts.length - 1);
  466. return texts;
  467. },
  468. //查找Value,适用多选和单选
  469. findValueByText: function (text)
  470. {
  471. var g = this, p = this.options;
  472. if (!text && text == "") return "";
  473. var contain = function (checkvalue)
  474. {
  475. var targetdata = text.toString().split(p.split);
  476. for (var i = 0; i < targetdata.length; i++)
  477. {
  478. if (targetdata[i] == checkvalue) return true;
  479. }
  480. return false;
  481. };
  482. var values = "";
  483. $(g.data).each(function (i, item)
  484. {
  485. var val = item[p.valueField];
  486. var txt = item[p.textField];
  487. if (contain(txt))
  488. {
  489. values += val + p.split;
  490. }
  491. });
  492. if (values.length > 0) values = values.substr(0, values.length - 1);
  493. return values;
  494. },
  495. insertItem: function (data,index)
  496. {
  497. var g = this, p = this.options;
  498. g.data = g.data || [];
  499. g.data.splice(index, 0, data);
  500. g.setData(g.data);
  501. },
  502. addItem: function (data)
  503. {
  504. var g = this, p = this.options;
  505. g.insertItem(data, (g.data || []).length);
  506. },
  507. _setValue: function (value,text)
  508. {
  509. var g = this, p = this.options;
  510. text = g.findTextByValue(value);
  511. if (p.tree)
  512. {
  513. g.selectValueByTree(value);
  514. }
  515. else if (!p.isMultiSelect)
  516. {
  517. g._changeValue(value, text);
  518. $("tr[value='" + value + "'] td", g.selectBox).addClass("l-selected");
  519. $("tr[value!='" + value + "'] td", g.selectBox).removeClass("l-selected");
  520. }
  521. else
  522. {
  523. g._changeValue(value, text);
  524. if (value != null) {
  525. var targetdata = value.toString().split(p.split);
  526. $("table.l-table-checkbox :checkbox", g.selectBox).each(function () { this.checked = false; });
  527. for (var i = 0; i < targetdata.length; i++) {
  528. $("table.l-table-checkbox tr[value=" + targetdata[i] + "] :checkbox", g.selectBox).each(function () { this.checked = true; });
  529. }
  530. }
  531. }
  532. },
  533. selectValue: function (value)
  534. {
  535. this._setValue(value);
  536. },
  537. bulidContent: function ()
  538. {
  539. var g = this, p = this.options;
  540. this.clearContent();
  541. if (g.select)
  542. {
  543. g.setSelect();
  544. }
  545. else if (p.tree)
  546. {
  547. g.setTree(p.tree);
  548. }
  549. },
  550. reload: function ()
  551. {
  552. var g = this, p = this.options;
  553. if (p.url)
  554. {
  555. g.set('url', p.url);
  556. }
  557. else if (g.grid)
  558. {
  559. g.grid.reload();
  560. }
  561. },
  562. _setUrl: function (url) {
  563. if (!url) return;
  564. var g = this, p = this.options;
  565. var parms = $.isFunction(p.parms) ? p.parms() : p.parms;
  566. $.ajax({
  567. type: p.ajaxType,
  568. url: url,
  569. data: parms,
  570. cache: false,
  571. dataType: 'json',
  572. success: function (data) {
  573. g.setData(data);
  574. g.trigger('success', [data]);
  575. },
  576. error: function (XMLHttpRequest, textStatus) {
  577. g.trigger('error', [XMLHttpRequest, textStatus]);
  578. }
  579. });
  580. },
  581. setUrl: function (url) {
  582. return this._setUrl(url);
  583. },
  584. setParm: function (name, value) {
  585. if (!name) return;
  586. var g = this;
  587. var parms = g.get('parms');
  588. if (!parms) parms = {};
  589. parms[name] = value;
  590. g.set('parms', parms);
  591. },
  592. clearContent: function ()
  593. {
  594. var g = this, p = this.options;
  595. $("table", g.selectBox).html("");
  596. //g.inputText.val("");
  597. //g.valueField.val("");
  598. },
  599. setSelect: function ()
  600. {
  601. var g = this, p = this.options;
  602. this.clearContent();
  603. $('option', g.select).each(function (i)
  604. {
  605. var val = $(this).val();
  606. var txt = $(this).html();
  607. var tr = $("<tr><td index='" + i + "' value='" + val + "' text='" + txt + "'>" + txt + "</td>");
  608. $("table.l-table-nocheckbox", g.selectBox).append(tr);
  609. $("td", tr).hover(function ()
  610. {
  611. $(this).addClass("l-over");
  612. }, function ()
  613. {
  614. $(this).removeClass("l-over");
  615. });
  616. });
  617. $('td:eq(' + g.select[0].selectedIndex + ')', g.selectBox).each(function ()
  618. {
  619. if ($(this).hasClass("l-selected"))
  620. {
  621. g.selectBox.hide();
  622. return;
  623. }
  624. $(".l-selected", g.selectBox).removeClass("l-selected");
  625. $(this).addClass("l-selected");
  626. if (g.select[0].selectedIndex != $(this).attr('index') && g.select[0].onchange)
  627. {
  628. g.select[0].selectedIndex = $(this).attr('index'); g.select[0].onchange();
  629. }
  630. var newIndex = parseInt($(this).attr('index'));
  631. g.select[0].selectedIndex = newIndex;
  632. g.select.trigger("change");
  633. g.selectBox.hide();
  634. var value = $(this).attr("value");
  635. var text = $(this).html();
  636. if (p.render)
  637. {
  638. g.inputText.val(p.render(value, text));
  639. }
  640. else
  641. {
  642. g.inputText.val(text);
  643. }
  644. });
  645. g._addClickEven();
  646. },
  647. _setData : function(data)
  648. {
  649. this.setData(data);
  650. },
  651. setData: function (data)
  652. {
  653. var g = this, p = this.options;
  654. if (g.select) return;
  655. if (!data || !data.length) data = [];
  656. if (g.data != data) g.data = data;
  657. this.clearContent();
  658. if (p.columns)
  659. {
  660. g.selectBox.table.headrow = $("<tr class='l-table-headerow'><td width='18px'></td></tr>");
  661. g.selectBox.table.append(g.selectBox.table.headrow);
  662. g.selectBox.table.addClass("l-box-select-grid");
  663. for (var j = 0; j < p.columns.length; j++)
  664. {
  665. var headrow = $("<td columnindex='" + j + "' columnname='" + p.columns[j].name + "'>" + p.columns[j].header + "</td>");
  666. if (p.columns[j].width)
  667. {
  668. headrow.width(p.columns[j].width);
  669. }
  670. g.selectBox.table.headrow.append(headrow);
  671. }
  672. }
  673. var out = [];
  674. if (p.emptyText && !g.emptyRow && (data.length == 0 || data[0][p.textField] != p.emptyText))
  675. {
  676. g.emptyRow = {};
  677. g.emptyRow[p.textField] = p.emptyText;
  678. g.emptyRow[p.valueField] = null;
  679. data.splice(0, 0, g.emptyRow);
  680. }
  681. for (var i = 0; i < data.length; i++)
  682. {
  683. var val = data[i][p.valueField];
  684. var txt = data[i][p.textField];
  685. if (!p.columns)
  686. {
  687. out.push("<tr value='" + val + "'>");
  688. if(p.isShowCheckBox){
  689. out.push("<td style='width:18px;' index='" + i + "' value='" + val + "' text='" + txt + "' ><input type='checkbox' /></td>");
  690. }
  691. var itemHtml = txt;
  692. if (p.renderItem) {
  693. itemHtml = p.renderItem.call(g, {
  694. data: data[i],
  695. value: val,
  696. text: txt,
  697. key: g.inputText.val()
  698. });
  699. } else if (p.autocomplete && p.highLight)
  700. {
  701. itemHtml = g._highLight(txt, g.inputText.val());
  702. }
  703. out.push("<td index='" + i + "' value='" + val + "' text='" + txt + "' align='left'>" + itemHtml + "</td></tr>");
  704. } else
  705. {
  706. out.push("<tr value='" + val + "'><td style='width:18px;' index='" + i + "' value='" + val + "' text='" + txt + "' ><input type='checkbox' /></td>");
  707. for (var j = 0; j < p.columns.length; j++) {
  708. var columnname = p.columns[j].name;
  709. out.push("<td>" + data[i][columnname] + "</td>");
  710. }
  711. out.push('</tr>');
  712. }
  713. }
  714. if (!p.columns) {
  715. if (p.isShowCheckBox) {
  716. $("table.l-table-checkbox", g.selectBox).append(out.join(''));
  717. }else{
  718. $("table.l-table-nocheckbox", g.selectBox).append(out.join(''));
  719. }
  720. } else {
  721. g.selectBox.table.append(out.join(''));
  722. }
  723. if (p.addRowButton && p.addRowButtonClick && !g.addRowButton)
  724. {
  725. g.addRowButton = $('<div class="l-box-select-add"><a href="javascript:void(0)" class="link"><div class="icon"></div></a></div>');
  726. g.addRowButton.find(".link").append(p.addRowButton).click(p.addRowButtonClick);
  727. g.selectBoxInner.after(g.addRowButton);
  728. }
  729. g.set('selectBoxHeight', p.selectBoxHeight);
  730. //自定义复选框支持
  731. if (p.isShowCheckBox && $.fn.ligerCheckBox)
  732. {
  733. $("table input:checkbox", g.selectBox).ligerCheckBox();
  734. }
  735. $(".l-table-checkbox input:checkbox", g.selectBox).change(function ()
  736. {
  737. if (this.checked && g.hasBind('beforeSelect'))
  738. {
  739. var parentTD = null;
  740. if ($(this).parent().get(0).tagName.toLowerCase() == "div")
  741. {
  742. parentTD = $(this).parent().parent();
  743. } else
  744. {
  745. parentTD = $(this).parent();
  746. }
  747. if (parentTD != null && g.trigger('beforeSelect', [parentTD.attr("value"), parentTD.attr("text")]) == false)
  748. {
  749. g.selectBox.slideToggle("fast");
  750. return false;
  751. }
  752. }
  753. if (!p.isMultiSelect)
  754. {
  755. if (this.checked)
  756. {
  757. $("input:checked", g.selectBox).not(this).each(function ()
  758. {
  759. this.checked = false;
  760. $(".l-checkbox-checked", $(this).parent()).removeClass("l-checkbox-checked");
  761. });
  762. g.selectBox.slideToggle("fast");
  763. }
  764. }
  765. g._checkboxUpdateValue();
  766. });
  767. $("table.l-table-nocheckbox td", g.selectBox).hover(function ()
  768. {
  769. $(this).addClass("l-over");
  770. }, function ()
  771. {
  772. $(this).removeClass("l-over");
  773. });
  774. g._addClickEven();
  775. //选择项初始化
  776. if (!p.autocomplete) {
  777. g.updateStyle();
  778. }
  779. },
  780. //树
  781. setTree: function (tree)
  782. {
  783. var g = this, p = this.options;
  784. this.clearContent();
  785. g.selectBox.table.remove();
  786. if (tree.checkbox != false)
  787. {
  788. tree.onCheck = function ()
  789. {
  790. var nodes = g.treeManager.getChecked();
  791. var value = [];
  792. var text = [];
  793. $(nodes).each(function (i, node)
  794. {
  795. if (p.treeLeafOnly && node.data.children) return;
  796. value.push(node.data[p.valueField]);
  797. text.push(node.data[p.textField]);
  798. });
  799. g._changeValue(value.join(p.split), text.join(p.split));
  800. };
  801. }
  802. else
  803. {
  804. tree.onSelect = function (node)
  805. {
  806. if (g.trigger('BeforeSelect'[node]) == false) return;
  807. if (p.treeLeafOnly && node.data.children) return;
  808. var value = node.data[p.valueField];
  809. var text = node.data[p.textField];
  810. g._changeValue(value, text);
  811. };
  812. tree.onCancelSelect = function (node)
  813. {
  814. g._changeValue("", "");
  815. };
  816. }
  817. tree.onAfterAppend = function (domnode, nodedata)
  818. {
  819. if (!g.treeManager) return;
  820. var value = null;
  821. if (p.initValue) value = p.initValue;
  822. else if (g.valueField.val() != "") value = g.valueField.val();
  823. g.selectValueByTree(value);
  824. };
  825. g.tree = $("<ul></ul>");
  826. $("div:first", g.selectBox).append(g.tree);
  827. g.tree.ligerTree(tree);
  828. g.treeManager = g.tree.ligerGetTreeManager();
  829. },
  830. selectValueByTree: function (value)
  831. {
  832. var g = this, p = this.options;
  833. if (value != null)
  834. {
  835. var text = "";
  836. var valuelist = value.toString().split(p.split);
  837. $(valuelist).each(function (i, item)
  838. {
  839. g.treeManager.selectNode(item.toString());
  840. text += g.treeManager.getTextByID(item);
  841. if (i < valuelist.length - 1) text += p.split;
  842. });
  843. g._changeValue(value, text);
  844. }
  845. },
  846. //表格
  847. setGrid: function (grid)
  848. {
  849. var g = this, p = this.options;
  850. if (g.grid) return;
  851. p.hideOnLoseFocus = p.hideGridOnLoseFocus ? true : false;
  852. this.clearContent();
  853. g.selectBox.addClass("l-box-select-lookup");
  854. g.selectBox.table.remove();
  855. var panel = $("div:first", g.selectBox);
  856. var conditionPanel = $("<div></div>").appendTo(panel);
  857. var gridPanel = $("<div></div>").appendTo(panel);
  858. g.conditionPanel = conditionPanel;
  859. //搜索框
  860. if (p.condition) {
  861. var conditionParm = $.extend({
  862. labelWidth: 60,
  863. space: 20,
  864. width: p.selectBoxWidth
  865. }, p.condition);
  866. g.condition = conditionPanel.ligerForm(conditionParm);
  867. } else {
  868. conditionPanel.remove();
  869. }
  870. //列表
  871. grid = $.extend({
  872. columnWidth: 120,
  873. alternatingRow: false,
  874. frozen: true,
  875. rownumbers: true,
  876. allowUnSelectRow:true
  877. }, grid, {
  878. width: "100%",
  879. height: g.getGridHeight(),
  880. inWindow: false,
  881. parms : p.parms,
  882. isChecked: function (rowdata) {
  883. var value = g.getValue();
  884. if (!value) return false;
  885. if (!p.valueField || !rowdata[p.valueField]) return false;
  886. return $.inArray(rowdata[p.valueField].toString(), value.split(p.split)) != -1;
  887. }
  888. });
  889. g.grid = g.gridManager = gridPanel.ligerGrid(grid);
  890. g.grid.bind('afterShowData', function ()
  891. {
  892. g.updateSelectBoxPosition();
  893. });
  894. var selecteds = [], onGridSelect = function () {
  895. var value = [], text = [];
  896. $(selecteds).each(function (i, rowdata) {
  897. value.push(rowdata[p.valueField]);
  898. text.push(rowdata[p.textField]);
  899. });
  900. if (grid.checkbox)
  901. g.selected = selecteds;
  902. else if (selecteds.length)
  903. g.selected = selecteds[0];
  904. else
  905. g.selected = null;
  906. g._changeValue(value.join(p.split), text.join(p.split));
  907. g.trigger('gridSelect', {
  908. value: value.join(p.split),
  909. text: text.join(p.split),
  910. data: selecteds
  911. });
  912. }, removeSelected = function (rowdata) {
  913. for (var i = selecteds.length - 1; i >= 0; i--) {
  914. if (selecteds[i][p.valueField] == rowdata[p.valueField]) {
  915. selecteds.splice(i, 1);
  916. }
  917. }
  918. }, addSelected = function (rowdata) {
  919. for (var i = selecteds.length - 1; i >= 0; i--) {
  920. if (selecteds[i][p.valueField] == rowdata[p.valueField]) {
  921. return;
  922. }
  923. }
  924. selecteds.push(rowdata);
  925. };
  926. if (grid.checkbox)
  927. {
  928. var onCheckRow = function (checked, rowdata) {
  929. checked && addSelected(rowdata);
  930. !checked && removeSelected(rowdata);
  931. };
  932. g.grid.bind('CheckAllRow', function (checked) {
  933. $(g.grid.rows).each(function (rowdata) {
  934. onCheckRow(checked, rowdata);
  935. });
  936. onGridSelect();
  937. });
  938. g.grid.bind('CheckRow', function (checked, rowdata) {
  939. onCheckRow(checked, rowdata);
  940. onGridSelect();
  941. });
  942. }
  943. else
  944. {
  945. g.grid.bind('SelectRow', function (rowdata) {
  946. selecteds = [rowdata];
  947. onGridSelect();
  948. g._toggleSelectBox(true);
  949. });
  950. g.grid.bind('UnSelectRow', function () {
  951. selecteds = [];
  952. onGridSelect();
  953. });
  954. }
  955. g.bind('show', function () {
  956. g.grid.refreshSize();
  957. });
  958. g.bind("clear", function () {
  959. selecteds = [];
  960. g.grid.selecteds = [];
  961. g.grid._showData();
  962. });
  963. if (p.condition) {
  964. var containerBtn1 = $('<li style="margin-right:9px"><div></div></li>');
  965. var containerBtn2 = $('<li style="margin-right:9px;float:right"><div></div></li>');
  966. $("ul:first", conditionPanel).append(containerBtn1).append(containerBtn2).after('<div class="l-clear"></div>');
  967. $("div", containerBtn1).ligerButton({
  968. text: p.Search, width: 40,
  969. click: function () {
  970. var rules = g.condition.toConditions();
  971. g.grid.setParm(grid.conditionParmName || 'condition', $.ligerui.toJSON(rules));
  972. g.grid.reload();
  973. }
  974. });
  975. $("div", containerBtn2).ligerButton({
  976. text: '关闭',width:40,
  977. click: function () {
  978. g.selectBox.hide();
  979. }
  980. });
  981. }
  982. g.grid.refreshSize();
  983. },
  984. getGridHeight: function (height) {
  985. var g = this, p = this.options;
  986. height = height || g.selectBox.height();
  987. height -= g.conditionPanel.height();
  988. return height;
  989. },
  990. _getValue: function ()
  991. {
  992. return $(this.valueField).val();
  993. },
  994. getValue: function ()
  995. {
  996. //获取值
  997. return this._getValue();
  998. },
  999. getSelected : function()
  1000. {
  1001. return this.selected;
  1002. },
  1003. getText: function ()
  1004. {
  1005. return this.inputText.val();
  1006. },
  1007. setText: function (value) {
  1008. this.inputText.val(value);
  1009. },
  1010. updateStyle: function ()
  1011. {
  1012. var g = this, p = this.options;
  1013. p.initValue = g._getValue();
  1014. g._dataInit();
  1015. },
  1016. _dataInit: function ()
  1017. {
  1018. var g = this, p = this.options;
  1019. var value = null;
  1020. if (p.initValue != null && p.initText != null)
  1021. {
  1022. g._changeValue(p.initValue, p.initText);
  1023. }
  1024. //根据值来初始化
  1025. if (p.initValue != null)
  1026. {
  1027. value = p.initValue;
  1028. if (p.tree)
  1029. {
  1030. if(value)
  1031. g.selectValueByTree(value);
  1032. }
  1033. else if (g.data)
  1034. {
  1035. var text = g.findTextByValue(value);
  1036. g._changeValue(value, text);
  1037. }
  1038. }
  1039. else if (g.valueField.val() != "")
  1040. {
  1041. value = g.valueField.val();
  1042. if (p.tree)
  1043. {
  1044. if(value)
  1045. g.selectValueByTree(value);
  1046. }
  1047. else if(g.data)
  1048. {
  1049. var text = g.findTextByValue(value);
  1050. g._changeValue(value, text);
  1051. }
  1052. }
  1053. if (!p.isShowCheckBox)
  1054. {
  1055. $("table tr", g.selectBox).find("td:first").each(function ()
  1056. {
  1057. if (value != null && value == $(this).attr("value"))
  1058. {
  1059. $(this).addClass("l-selected");
  1060. } else
  1061. {
  1062. $(this).removeClass("l-selected");
  1063. }
  1064. });
  1065. }
  1066. else
  1067. {
  1068. $(":checkbox", g.selectBox).each(function ()
  1069. {
  1070. var parentTD = null;
  1071. var checkbox = $(this);
  1072. if (checkbox.parent().get(0).tagName.toLowerCase() == "div")
  1073. {
  1074. parentTD = checkbox.parent().parent();
  1075. } else
  1076. {
  1077. parentTD = checkbox.parent();
  1078. }
  1079. if (parentTD == null) return;
  1080. $(".l-checkbox", parentTD).removeClass("l-checkbox-checked");
  1081. checkbox[0].checked = false;
  1082. var valuearr = (value || "").toString().split(p.split);
  1083. $(valuearr).each(function (i, item)
  1084. {
  1085. if (value != null && item == parentTD.attr("value"))
  1086. {
  1087. $(".l-checkbox", parentTD).addClass("l-checkbox-checked");
  1088. checkbox[0].checked = true;
  1089. }
  1090. });
  1091. });
  1092. }
  1093. },
  1094. //设置值到 文本框和隐藏域
  1095. _changeValue: function (newValue, newText)
  1096. {
  1097. var g = this, p = this.options;
  1098. g.valueField.val(newValue);
  1099. if (p && p.render)
  1100. {
  1101. g.inputText.val(p.render(newValue, newText));
  1102. }
  1103. else
  1104. {
  1105. g.inputText.val(newText);
  1106. }
  1107. g.selectedValue = newValue;
  1108. g.selectedText = newText;
  1109. g.inputText.trigger("change").focus();
  1110. g.trigger('selected', [newValue, newText]);
  1111. },
  1112. //更新选中的值(复选框)
  1113. _checkboxUpdateValue: function ()
  1114. {
  1115. var g = this, p = this.options;
  1116. var valueStr = "";
  1117. var textStr = "";
  1118. $("input:checked", g.selectBox).each(function ()
  1119. {
  1120. var parentTD = null;
  1121. if ($(this).parent().get(0).tagName.toLowerCase() == "div")
  1122. {
  1123. parentTD = $(this).parent().parent();
  1124. } else
  1125. {
  1126. parentTD = $(this).parent();
  1127. }
  1128. if (!parentTD) return;
  1129. valueStr += parentTD.attr("value") + p.split;
  1130. textStr += parentTD.attr("text") + p.split;
  1131. });
  1132. if (valueStr.length > 0) valueStr = valueStr.substr(0, valueStr.length - 1);
  1133. if (textStr.length > 0) textStr = textStr.substr(0, textStr.length - 1);
  1134. g._changeValue(valueStr, textStr);
  1135. },
  1136. _addClickEven: function ()
  1137. {
  1138. var g = this, p = this.options;
  1139. //选项点击
  1140. $(".l-table-nocheckbox td", g.selectBox).click(function ()
  1141. {
  1142. var value = $(this).attr("value");
  1143. var index = parseInt($(this).attr('index'));
  1144. var text = $(this).attr("text");
  1145. if (g.hasBind('beforeSelect') && g.trigger('beforeSelect', [value, text]) == false)
  1146. {
  1147. if (p.slide) g.selectBox.slideToggle("fast");
  1148. else g.selectBox.hide();
  1149. return false;
  1150. }
  1151. if ($(this).hasClass("l-selected"))
  1152. {
  1153. if (p.slide) g.selectBox.slideToggle("fast");
  1154. else g.selectBox.hide();
  1155. return;
  1156. }
  1157. $(".l-selected", g.selectBox).removeClass("l-selected");
  1158. $(this).addClass("l-selected");
  1159. if (g.select)
  1160. {
  1161. if (g.select[0].selectedIndex != index)
  1162. {
  1163. g.select[0].selectedIndex = index;
  1164. g.select.trigger("change");
  1165. }
  1166. }
  1167. if (p.slide)
  1168. {
  1169. g.boxToggling = true;
  1170. g.selectBox.hide("fast", function ()
  1171. {
  1172. g.boxToggling = false;
  1173. })
  1174. } else g.selectBox.hide();
  1175. g._changeValue(value, text);
  1176. });
  1177. },
  1178. updateSelectBoxPosition: function ()
  1179. {
  1180. var g = this, p = this.options;
  1181. if (p && p.absolute)
  1182. {
  1183. var contentHeight = $(document).height();
  1184. if (p.alwayShowInTop || Number(g.wrapper.offset().top + 1 + g.wrapper.outerHeight() + g.selectBox.height()) > contentHeight
  1185. && contentHeight > Number(g.selectBox.height() + 1))
  1186. {
  1187. //若下拉框大小超过当前document下边框,且当前document上留白大于下拉内容高度,下拉内容向上展现
  1188. g.selectBox.css({ left: g.wrapper.offset().left, top: g.wrapper.offset().top - 1 - g.selectBox.height() });
  1189. } else
  1190. {
  1191. g.selectBox.css({ left: g.wrapper.offset().left, top: g.wrapper.offset().top + 1 + g.wrapper.outerHeight() });
  1192. }
  1193. }
  1194. else
  1195. {
  1196. var topheight = g.wrapper.offset().top - $(window).scrollTop();
  1197. var selfheight = g.selectBox.height() + textHeight + 4;
  1198. if (topheight + selfheight > $(window).height() && topheight > selfheight)
  1199. {
  1200. g.selectBox.css("marginTop", -1 * (g.selectBox.height() + textHeight + 5));
  1201. }
  1202. }
  1203. },
  1204. _toggleSelectBox: function (isHide)
  1205. {
  1206. var g = this, p = this.options;
  1207. if (!g || !p) return;
  1208. var textHeight = g.wrapper.height();
  1209. g.boxToggling = true;
  1210. if (isHide)
  1211. {
  1212. if (p.slide)
  1213. {
  1214. g.selectBox.slideToggle('fast', function ()
  1215. {
  1216. g.boxToggling = false;
  1217. });
  1218. }
  1219. else
  1220. {
  1221. g.selectBox.hide();
  1222. g.boxToggling = false;
  1223. }
  1224. }
  1225. else
  1226. {
  1227. g.updateSelectBoxPosition();
  1228. if (p.slide)
  1229. {
  1230. g.selectBox.slideToggle('fast', function ()
  1231. {
  1232. g.boxToggling = false;
  1233. if (!p.isShowCheckBox && $('td.l-selected', g.selectBox).length > 0)
  1234. {
  1235. var offSet = ($('td.l-selected', g.selectBox).offset().top - g.selectBox.offset().top);
  1236. $(".l-box-select-inner", g.selectBox).animate({ scrollTop: offSet });
  1237. }
  1238. });
  1239. }
  1240. else
  1241. {
  1242. g.selectBox.show();
  1243. g.boxToggling = false;
  1244. if (!g.tree && !g.grid && !p.isShowCheckBox && $('td.l-selected', g.selectBox).length > 0)
  1245. {
  1246. var offSet = ($('td.l-selected', g.selectBox).offset().top - g.selectBox.offset().top);
  1247. $(".l-box-select-inner", g.selectBox).animate({ scrollTop: offSet });
  1248. }
  1249. }
  1250. }
  1251. g.isShowed = g.selectBox.is(":visible");
  1252. g.trigger('toggle', [isHide]);
  1253. g.trigger(isHide ? 'hide' : 'show');
  1254. },
  1255. _highLight: function (str, key)
  1256. {
  1257. if (!str) return str;
  1258. var index = str.indexOf(key);
  1259. if (index == -1) return str;
  1260. return str.substring(0, index) + "<span class='l-highLight'>" + key + "</span>" + str.substring(key.length + index);
  1261. },
  1262. _setAutocomplete: function (value) {
  1263. var g = this, p = this.options;
  1264. if (!value) return;
  1265. g.inputText.removeAttr("readonly");
  1266. var lastText = g.inputText.val();
  1267. g.inputText.keyup(function ()
  1268. {
  1269. setTimeout(function ()
  1270. {
  1271. if (lastText == g.inputText.val()) return;
  1272. p.initValue = "";
  1273. g.valueField.val("");
  1274. if (p.url)
  1275. {
  1276. g.setParm('key', g.inputText.val());
  1277. g.set('url', p.url);
  1278. g.selectBox.show();
  1279. } else if (p.grid)
  1280. {
  1281. g.grid.setParm('key', g.inputText.val());
  1282. g.grid.reload();
  1283. }
  1284. lastText = g.inputText.val();
  1285. }, 1);
  1286. });
  1287. }
  1288. });
  1289. $.ligerui.controls.ComboBox.prototype.setValue = $.ligerui.controls.ComboBox.prototype.selectValue;
  1290. //设置文本框和隐藏控件的值
  1291. $.ligerui.controls.ComboBox.prototype.setInputValue = $.ligerui.controls.ComboBox.prototype._changeValue;
  1292. })(jQuery);