RoadFlow2.1 临时演示

roadui.combox.js 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. //Combox
  2. ; RoadUI.Combox = function ()
  3. {
  4. var instance = this;
  5. this.init = function ($comboxs)
  6. {
  7. $comboxs.each(function ()
  8. {
  9. var id = $(this).attr('id');
  10. var name = $(this).attr('name') || id;
  11. var validate = $(this).attr('validate');
  12. var title = $(this).attr("title") || "";
  13. var readonly = true;// $(this).attr("readonly") && "readonly" == $(this).attr("readonly").toLowerCase();
  14. var disabled = $(this).prop("disabled");
  15. var width1 = $(this).attr("width1");
  16. var height1 = $(this).attr("height1");
  17. var datatype = $(this).attr("datatype");
  18. var change = $(this).attr("onchange");
  19. var isflow = $(this).attr("isflow");
  20. var type1 = $(this).attr("type1");
  21. var datasource = $(this).attr("datasource");
  22. var listmode = $(this).attr("listmode");
  23. if (RoadUI.Core.isIe6Or7())
  24. {
  25. $(this).css({ "display": "inline-block" });
  26. }
  27. var offsetTop = RoadUI.Core.offsetTop($(this).get(0));
  28. var offsetLeft = RoadUI.Core.offsetLeft($(this).get(0));
  29. var tagName = $(this).get(0).tagName.toLowerCase();
  30. var comboxWidth = $(this).width();
  31. var divWidth = width1 ? "width:" + width1 + "px;" : RoadUI.Core.isIe6Or7() ? "width:" + $(this).width() + "px;" : "width:auto;";
  32. var divHeight = height1 ? "height:" + height1 + "px;" : RoadUI.Core.isIe6Or7() ? "" : "height:auto;";
  33. var $hide = $('<input type="text" style="display:none;" id="' + id + '" name="' + name + '" ' + (change ? change.indexOf('"') >= 0 ? 'onchange=\'' + change + '\'' : 'onchange="' + change + '"' : '')
  34. + (isflow ? 'isflow="' + isflow + '"' : '') + (type1 ? 'type1="' + type1 + '"' : '')
  35. + (datasource ? 'datasource="' + datasource + '"' : '') + (listmode ? 'listmode="' + listmode + '"' : '')
  36. + 'tagname="' + tagName + '"' + '/>');
  37. var $input = $('<input type="text" autocomplete="off" class="comboxtext1" style="' + $(this).attr("style") + '" id="' + id + '_text" name="' + name + '_text" value="" />');
  38. var $div = $('<div id="' + id + '_selectdiv" style="' + divWidth + divHeight + 'white-space:nowrap;' + '" class="comboxdiv"></div>');
  39. if (readonly)
  40. {
  41. $input.prop("readonly", true);
  42. }
  43. initElement($input, "comboxtext");
  44. var $wrapDiv = $('<span style="display:inline-block;"></span>');
  45. var $wrapDiv1 = $('<div></div>');
  46. $wrapDiv1.append($hide).append($input);
  47. $wrapDiv.append($wrapDiv1).append($div);
  48. $(this).after($wrapDiv);
  49. $div.hide();
  50. var isTable = false;
  51. switch (tagName)
  52. {
  53. case "select":
  54. var multiple = "1" == $(this).attr("more") || $(this).prop("multiple");
  55. var $options = $('option,optgroup', $(this));
  56. var texts = [];
  57. var values = [];
  58. for (var i = 0; i < $options.size() ; i++)
  59. {
  60. if ($options.eq(i).get(0).tagName.toLowerCase() == "optgroup")
  61. {
  62. var $divoptgroup = $('<div class="comboxoptgroup">' + $options.eq(i).text() + '</div>');
  63. $div.append($divoptgroup);
  64. continue;
  65. }
  66. if ($options.eq(i).attr("selected") && $options.eq(i).prop("selected"))
  67. {
  68. texts.push($options.eq(i).text());
  69. values.push($options.eq(i).val());
  70. }
  71. var optionValue = $options.eq(i).val();
  72. var $divoptions = $('<div class="' + ($options.eq(i).prop("selected") ? "comboxoption1" : "comboxoption") + '" ' +
  73. 'value="' + optionValue + '"></div>');
  74. if (multiple)
  75. {
  76. var $checkbox = $('<input type="checkbox" id="checkbox_' + id + "_" + optionValue + '" name="radio_' + id + '" value="' + optionValue + '" '
  77. + ($options.eq(i).prop("selected") && $options.eq(i).attr("selected") ? 'checked="checked"' : '')
  78. + ' style="vertical-align:middle;"/>');
  79. $divoptions.append($checkbox);
  80. $checkbox.bind("click", function ()
  81. {
  82. instance.setValue($(this).val(), $(this).next().text(), id, true, $(this).prop("checked"));
  83. $(this).parent().children("div").removeClass().addClass("comboxoption");
  84. $(this).removeClass().addClass("comboxoption1");
  85. });
  86. }
  87. else
  88. {
  89. $divoptions.bind("click", function ()
  90. {
  91. instance.setValue($(this).attr("value"), $(this).text(), id, false, $(this).prop("checked"));
  92. $(this).parent().children("div[class!='comboxoptgroup']").removeClass().addClass("comboxoption");
  93. $(this).removeClass().addClass("comboxoption1");
  94. });
  95. }
  96. $divoptions.hover(function ()
  97. {
  98. $(this).removeClass().addClass('comboxoption1');
  99. }, function ()
  100. {
  101. var cvalue = $("#" + id).val();
  102. if (!cvalue || (',' + cvalue + ',').indexOf(',' + $(this).attr("value") + ',') == -1)
  103. {
  104. $(this).removeClass().addClass('comboxoption');
  105. }
  106. });
  107. var $label = $('<label style="vertical-align:middle;" ' + (multiple ? 'for="checkbox_' + id + "_" + optionValue + '"' : '') + '>' + $options.eq(i).text() + '</label>');
  108. $divoptions.append($label);
  109. $div.append($divoptions);
  110. }
  111. $hide.val(values.join(','));
  112. $input.val(texts.join(',')).attr("title", texts.join(','));
  113. break;
  114. case "span":
  115. case "div":
  116. isTable = true;
  117. var $table = $(this).children("table");
  118. var $query = $("[class='querybar']", $(this));
  119. if ($query.size() > 0)
  120. {
  121. $query.css("height", "22px");
  122. $("input[type='button']", $query).bind('click', function ()
  123. {
  124. var query = [];
  125. $(this).parent().children("input[type!='button']").each(function ()
  126. {
  127. query.push($(this).attr("id") + "=" + encodeURIComponent($(this).val()));
  128. });
  129. $table.attr("query", "&" + query.join('&'));
  130. instance.loadData(10, 1, $table);
  131. });
  132. $div.append($query);
  133. }
  134. var multiple = ($(this).attr("multiple") && ("multiple" == $(this).attr("multiple").toLowerCase()
  135. || "1" == $(this).attr("multiple") || "true" == $(this).attr("multiple").toLowerCase()))
  136. || ($(this).attr("more") && ("1" == $(this).attr("more").toLowerCase() || "true" == $(this).attr("more").toLowerCase()));
  137. if (multiple)
  138. {
  139. $table.prop("multiple", true);
  140. }
  141. $table.attr("comboxid", id);
  142. if ($("tbody tr", $table).size() == 0)
  143. {
  144. instance.loadData(10, 1, $table);
  145. }
  146. else
  147. {
  148. instance.setTable(id, $table, $('tbody tr', $table), "");
  149. }
  150. $div.append($table);
  151. //$div.css("overflow", "hidden");
  152. //new RoadUI.Grid({ table: $table, resizeCol:false });
  153. break;
  154. }
  155. if (offsetTop + (RoadUI.Core.isIE() ? 30 : 0) + $div.height() > $(window).height())
  156. {
  157. offsetTop = offsetTop - $div.height() - (RoadUI.Core.isIE() ? 2 : 3);
  158. if (offsetTop)
  159. {
  160. $div.css("top", (offsetTop < 0 ? 0 : offsetTop) + "px");
  161. }
  162. }
  163. if (offsetLeft + $div.width() >= $(window).width()-30)
  164. {
  165. offsetLeft = offsetLeft - ($div.width() - comboxWidth) - 2;
  166. }
  167. if (offsetLeft)
  168. {
  169. $div.css("left", (offsetLeft < 0 ? 0 : offsetLeft) + "px");
  170. }
  171. if (disabled)
  172. {
  173. $input.prop("disabled", true);
  174. }
  175. else
  176. {
  177. $input.bind("focus", function ()
  178. {
  179. $div.show();
  180. }).bind("click", function ()
  181. {
  182. $div.show();
  183. }).bind("blur", function ()
  184. {
  185. $div.hover(function () { $div.show(); }, function () { $div.hide(); });
  186. });
  187. }
  188. $(this).remove();
  189. });
  190. };
  191. this.loadData = function (size, number, $table)
  192. {
  193. if (typeof ($table) == "string")
  194. {
  195. $table = $("#" + $table.replace('.', '\\.') + "_selectdiv table");
  196. }
  197. var url = $table.attr("dataurl");
  198. var query = $table.attr("query");
  199. $.ajax({
  200. url: (url || "") + (url.indexOf('?') >= 0 ? "&" : "?") + "pagesize=" + (size || 10) + "&pagenumber=" + (number || 1) + (query || ""), dataType: "json", async: false, cache: false, success: function (json)
  201. {
  202. var pager = RoadUI.Core.getPager(json.count, size || 10, number || 1, "", "new RoadUI.Combox().loadData", $table.attr("comboxid"));
  203. instance.setTable($table.attr('comboxid'), $table, json.data, pager);
  204. }
  205. });
  206. };
  207. this.initValue = function (id, value)
  208. {
  209. var $obj = $('#' + id.replace('.', '\\.'));
  210. $obj.val(value);
  211. var tagname = $obj.attr('tagname');
  212. var text = "";
  213. switch (tagname)
  214. {
  215. case "select":
  216. var texts = [];
  217. var $divs = $("#" + id.replace('.', '\\.') + "_selectdiv div");
  218. for (var i = 0; i < $divs.size() ; i++)
  219. {
  220. var value1 = $divs.eq(i).attr("value");
  221. if (("," + value + ",").indexOf("," + value1 + ",") >= 0)
  222. {
  223. texts.push($divs.eq(i).text());
  224. $divs.eq(i).removeClass().addClass('comboxoption1');
  225. }
  226. else
  227. {
  228. $divs.eq(i).removeClass().addClass('comboxoption');
  229. }
  230. }
  231. text = texts.join(',');
  232. break;
  233. case "span":
  234. case "div":
  235. var listmode = $obj.attr("listmode");
  236. var datasource = $obj.attr("datasource");
  237. if ("3" != datasource)
  238. {
  239. var $trs = $("#" + id.replace('.', '\\.') + "_selectdiv table tbody tr");
  240. var texts = [];
  241. for (var i = 0; i < $trs.size() ; i++)
  242. {
  243. var $td = $("td:first", $trs.eq(i));
  244. var value1 = $td.attr("value");
  245. if (("," + value + ",").indexOf("," + value1 + ",") >= 0)
  246. {
  247. var txt = $td.text();
  248. texts.push(txt);
  249. $td.attr("selected", "selected");
  250. $("input[type='radio'],input[type='checkbox']", $td).prop("checked", true);
  251. }
  252. }
  253. text = texts.join(',');
  254. }
  255. else
  256. {
  257. var $table = $("#" + id.replace('.', '\\.') + "_selectdiv table");
  258. var gettextsurl = $table.attr("gettextsurl");
  259. $.ajax({
  260. url: gettextsurl + (gettextsurl.indexOf('?') >= 0 ? '&' : '?') + "values=" + encodeURIComponent(value), cache: false, async: false, success: function (txt)
  261. {
  262. text = txt;
  263. var $trs = $("tbody tr", $table);
  264. for (var i = 0; i < $trs.size() ; i++)
  265. {
  266. var $td = $("td:first", $trs.eq(i));
  267. var value1 = $td.attr("value");
  268. if (("," + value + ",").indexOf("," + value1 + ",") >= 0)
  269. {
  270. $td.attr("selected", "selected");
  271. $("input[type='radio'],input[type='checkbox']", $td).prop("checked", true);
  272. }
  273. }
  274. }
  275. });
  276. }
  277. break;
  278. }
  279. $obj.next().val(text);
  280. };
  281. this.setTable = function (id, $table, trs, pager)
  282. {
  283. $table = $table || $("#" + id.replace('.', '\\.') + "_selectdiv table");
  284. var multiple = $table.prop("multiple");
  285. $("tbody", $table).html(trs || "");
  286. if (pager)
  287. {
  288. if ($('tfoot', $table).size() == 0)
  289. {
  290. $table.append('<tfoot><tr><td style="text-align:center" colspan="' + $('thead tr th', $table).size() + '">' + (pager || "") + '</td></tr></tfoot>');
  291. }
  292. else
  293. {
  294. $('tfoot tr td', $table).html(pager || "");
  295. }
  296. }
  297. else
  298. {
  299. $('tfoot tr td', $table).remove();
  300. }
  301. var $hidde = $("#" + id.replace('.', '\\.'));
  302. var $text = $("#" + id.replace('.', '\\.') + "_text");
  303. var values = ($hidde.val() || "").split(',');
  304. var texts = ($text.val() || "").split(',');
  305. $("tbody tr", $table).each(function ()
  306. {
  307. var $td = $("td:first", $(this));
  308. var $tdHtml = $td.html();
  309. var selected = $td.attr("selected") && ("selected" == $td.attr("selected").toLowerCase() || "1" == $td.attr("selected") || "true" == $td.attr("selected").toLowerCase());
  310. if (!selected)
  311. {
  312. selected = $td.attr("isselected") && ("selected" == $td.attr("isselected").toLowerCase() || "1" == $td.attr("isselected") || "true" == $td.attr("isselected").toLowerCase());
  313. }
  314. if (!selected)
  315. {
  316. var value = $("#" + id.replace('.', '\\.')).val() || "";
  317. if (value && $td.attr("value") && ("," + value + ",").indexOf("," + ($td.attr("value") || "") + ",") >= 0)
  318. {
  319. selected = true;
  320. }
  321. }
  322. if (selected)
  323. {
  324. values.push($td.attr("value"));
  325. texts.push($td.text());
  326. }
  327. if (multiple)
  328. {
  329. var $checkbox = $('<input ' + (selected ? 'checked="checked"' : '') + ' id="checkbox_' + id.replace('.', '_') + '_' + $td.attr("value") + '" name="radio_' + id.replace('.', '_') + '" type="checkbox" value="' + $td.attr("value") + '" style="vertical-align:middle;"/>');
  330. var $label = $('<label for="checkbox_' + id.replace('.', '_') + '_' + $td.attr("value") + '" style="vertical-align:middle;">' + $tdHtml + '</label>');
  331. $checkbox.bind("click", function ()
  332. {
  333. instance.setValue($(this).val(), $(this).next().text(), id, true, $(this).prop('checked'));
  334. });
  335. $td.html('').append($checkbox, $label);
  336. }
  337. else
  338. {
  339. var $radio = $('<input ' + (selected ? 'checked="checked"' : '') + ' id="radio_' + id.replace('.', '_') + '_' + $td.attr("value") + '" name="radio_' + id.replace('.', '_') + '" type="radio" value="' + $td.attr("value") + '" style="vertical-align:middle;"/>');
  340. var $label = $('<label for="radio_' + id.replace('.', '_') + '_' + $td.attr("value") + '" style="vertical-align:middle;">' + $tdHtml + '</label>');
  341. $radio.bind("click", function ()
  342. {
  343. instance.setValue($(this).val(), $(this).next().text(), id, false, $(this).prop('checked'));
  344. });
  345. $td.html('').append($radio, $label);
  346. }
  347. });
  348. $table.attr('border', '0');
  349. $table.attr('cellpadding', '1');
  350. $table.attr('cellspacing', '0');
  351. $table.css('width', '100%');
  352. $('tbody tr td', $table).css("padding", "0 0 0 3px");
  353. $('thead tr td', $table).css("padding", "0 0 0 3px");
  354. $table.removeClass().addClass("listtable");
  355. new RoadUI.Init().table();
  356. var values1 = [];
  357. var texts1 = [];
  358. for (var i = 0; i < values.length; i++)
  359. {
  360. var isIn = false;
  361. for (var j = 0; j < values1.length; j++)
  362. {
  363. if (values1[j] == values[i])
  364. {
  365. isIn = true;
  366. break;
  367. }
  368. }
  369. if (!isIn)
  370. {
  371. values1.push(values[i]);
  372. texts1.push(texts[i]);
  373. }
  374. }
  375. $hidde.val(values1.join(','));
  376. var txts = texts1.join(',');
  377. $text.val(txts).attr("title", txts);
  378. return $table;
  379. };
  380. this.setValue = function (value, text, id, isMultiple, checked)
  381. {
  382. var $valueele = $("#" + id.replace('.', '\\.'));
  383. var $textele = $("#" + id.replace('.', '\\.') + "_text");
  384. if (isMultiple)
  385. {
  386. if (checked)
  387. {
  388. value = $valueele.val() ? $valueele.val() + "," + value : value;
  389. text = $textele.val() ? $textele.val() + "," + text : text;
  390. }
  391. else
  392. {
  393. var values = ($valueele.val() || "").split(',');
  394. var texts = ($textele.val() || "").split(',');
  395. var values1=[];
  396. var texts1=[];
  397. for (var i = 0; i < values.length; i++)
  398. {
  399. if (values[i] != value)
  400. {
  401. values1.push(values[i]);
  402. texts1.push(texts[i]);
  403. }
  404. }
  405. value = values1.join(',');
  406. text = texts1.join(',');
  407. }
  408. }
  409. $textele.val(text).attr("title", text);
  410. $valueele.val(value);
  411. if (!isMultiple)
  412. {
  413. $("#" + id.replace('.', '\\.') + "_selectdiv").hide();
  414. }
  415. $valueele.change();
  416. };
  417. this.reSet = function (id, options)
  418. {
  419. var $obj = $("#" + id.replace(".", "\\."));
  420. if ($obj.size() == 0)
  421. {
  422. return;
  423. }
  424. var $selectdiv = $("#" + id.replace(".", "\\.") + "_selectdiv");
  425. if ($selectdiv.size() == 0)
  426. {
  427. return;
  428. }
  429. var isflow = $obj.attr("isflow");
  430. var type1 = $obj.attr("type1");
  431. var change = $obj.attr("onchange");
  432. var multiple = $("input[type='checkbox']", $selectdiv).size() > 0;
  433. var width = $selectdiv.width();
  434. var height = $selectdiv.height();
  435. var select = '<select class="mycombox" id="' + id + '" name="' + id + '" ' + (multiple ? ' multiple="multiple"' : '') +
  436. (width ? ' width1="' + width + '"' : '') + (height ? ' height1="' + height + '"' : '') +
  437. (isflow ? ' isflow="' + isflow + '"' : '') + (type1 ? ' type1="' + type1 + '"' : '') +
  438. (change ? change.indexOf('"') >= 0 ? 'onchange=\'' + change + '\'' : 'onchange="' + change + '"' : '');
  439. select += '>';
  440. select += options;
  441. select += '</select>';
  442. var $select = $(select);
  443. $obj.parent().parent().after($select);
  444. $obj.parent().parent().remove();
  445. this.init($select);
  446. }
  447. }