UU跑腿标准版

ligerCheckBoxList.js 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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.ligerCheckBoxList = function (options)
  12. {
  13. return $.ligerui.run.call(this, "ligerCheckBoxList", arguments);
  14. };
  15. $.ligerDefaults.CheckBoxList = {
  16. rowSize: 3, //每行显示元素数
  17. valueField: 'id', //值成员
  18. textField: 'text', //显示成员
  19. valueFieldID:null, //隐藏域
  20. name : null, //表单名
  21. split: ";", //分隔符
  22. data: null, //数据
  23. parms: null, //ajax提交表单
  24. url: null, //数据源URL(需返回JSON)
  25. onSuccess: null,
  26. onError: null,
  27. css: null, //附加css
  28. value: null, //值
  29. valueFieldCssClass : null
  30. };
  31. //扩展方法
  32. $.ligerMethos.CheckBoxList = $.ligerMethos.CheckBoxList || {};
  33. $.ligerui.controls.CheckBoxList = function (element, options)
  34. {
  35. $.ligerui.controls.CheckBoxList.base.constructor.call(this, element, options);
  36. };
  37. $.ligerui.controls.CheckBoxList.ligerExtend($.ligerui.controls.Input, {
  38. __getType: function ()
  39. {
  40. return 'CheckBoxList';
  41. },
  42. _extendMethods: function ()
  43. {
  44. return $.ligerMethos.CheckBoxList;
  45. },
  46. _init: function ()
  47. {
  48. $.ligerui.controls.CheckBoxList.base._init.call(this);
  49. },
  50. _render: function ()
  51. {
  52. var g = this, p = this.options;
  53. g.data = p.data;
  54. g.valueField = null; //隐藏域(保存值)
  55. if (p.valueFieldID)
  56. {
  57. g.valueField = $("#" + p.valueFieldID + ":input,[name=" + p.valueFieldID + "]:input");
  58. if (g.valueField.length == 0) g.valueField = $('<input type="hidden"/>');
  59. g.valueField[0].id = g.valueField[0].name = p.valueFieldID;
  60. }
  61. else
  62. {
  63. g.valueField = $('<input type="hidden"/>');
  64. g.valueField[0].id = g.valueField[0].name = g.id + "_val";
  65. }
  66. if (g.valueField[0].name == null) g.valueField[0].name = g.valueField[0].id;
  67. if (p.valueFieldCssClass)
  68. {
  69. g.valueField.addClass(p.valueFieldCssClass);
  70. }
  71. g.valueField.attr("data-ligerid", g.id);
  72. g.checkboxList = $(this.element);
  73. g.checkboxList.html('<div class="l-checkboxlist-inner"><table cellpadding="0" cellspacing="0" border="0" class="l-checkboxlist-table"></table></div>').addClass("l-checkboxlist").append(g.valueField);
  74. g.checkboxList.table = $("table:first", g.checkboxList);
  75. g.set(p);
  76. g._addClickEven();
  77. },
  78. destroy: function ()
  79. {
  80. if (this.checkboxList) this.checkboxList.remove();
  81. this.options = null;
  82. $.ligerui.remove(this);
  83. },
  84. clear : function()
  85. {
  86. this._changeValue("");
  87. this.trigger('clear');
  88. },
  89. _setCss : function(css)
  90. {
  91. if (css) {
  92. this.checkboxList.addClass(css);
  93. }
  94. },
  95. _setDisabled: function (value)
  96. {
  97. //禁用样式
  98. if (value)
  99. {
  100. this.checkboxList.addClass('l-checkboxlist-disabled');
  101. $("input:checkbox", this.radioList).attr("disabled", true);
  102. } else
  103. {
  104. this.checkboxList.removeClass('l-checkboxlist-disabled');
  105. $("input:checkbox", this.radioList).removeAttr("disabled");
  106. }
  107. },
  108. _setWidth: function (value)
  109. {
  110. this.checkboxList.width(value);
  111. },
  112. _setHeight: function (value)
  113. {
  114. this.checkboxList.height(value);
  115. },
  116. indexOf : function(item)
  117. {
  118. var g = this, p = this.options;
  119. if (!g.data) return -1;
  120. for (var i = 0, l = g.data.length; i < l; i++)
  121. {
  122. if (typeof (item) == "object")
  123. {
  124. if (g.data[i] == item) return i;
  125. } else
  126. {
  127. if (g.data[i][p.valueField].toString() == item.toString()) return i;
  128. }
  129. }
  130. return -1;
  131. },
  132. removeItems : function(items)
  133. {
  134. var g = this;
  135. if (!g.data) return;
  136. $(items).each(function (i,item)
  137. {
  138. var index = g.indexOf(item);
  139. if (index == -1) return;
  140. g.data.splice(index, 1);
  141. });
  142. g.refresh();
  143. },
  144. removeItem: function (item)
  145. {
  146. if (!this.data) return;
  147. var index = this.indexOf(item);
  148. if (index == -1) return;
  149. this.data.splice(index, 1);
  150. this.refresh();
  151. },
  152. insertItem: function (item,index)
  153. {
  154. var g = this;
  155. if (!g.data) g.data = [];
  156. g.data.splice(index, 0, item);
  157. g.refresh();
  158. },
  159. addItems: function (items)
  160. {
  161. var g = this;
  162. if (!g.data) g.data = [];
  163. $(items).each(function (i, item)
  164. {
  165. g.data.push(item);
  166. });
  167. g.refresh();
  168. },
  169. addItem: function (item)
  170. {
  171. var g = this;
  172. if (!g.data) g.data = [];
  173. g.data.push(item);
  174. g.refresh();
  175. },
  176. _setValue: function (value)
  177. {
  178. var g = this, p = this.options;
  179. p.value = value;
  180. g.valueField.val(p.value);
  181. this._dataInit();
  182. },
  183. setValue: function (value)
  184. {
  185. this._setValue(value);
  186. },
  187. _setUrl: function (url) {
  188. if (!url) return;
  189. var g = this, p = this.options;
  190. var parms = $.isFunction(p.parms) ? p.parms() : p.parms;
  191. $.ajax({
  192. type: 'post',
  193. url: url,
  194. data: parms,
  195. cache: false,
  196. dataType: 'json',
  197. success: function (data) {
  198. g.setData(data);
  199. g.trigger('success', [data]);
  200. },
  201. error: function (XMLHttpRequest, textStatus) {
  202. g.trigger('error', [XMLHttpRequest, textStatus]);
  203. }
  204. });
  205. },
  206. setUrl: function (url) {
  207. return this._setUrl(url);
  208. },
  209. setParm: function (name, value) {
  210. if (!name) return;
  211. var g = this;
  212. var parms = g.get('parms');
  213. if (!parms) parms = {};
  214. parms[name] = value;
  215. g.set('parms', parms);
  216. },
  217. clearContent: function ()
  218. {
  219. var g = this, p = this.options;
  220. $("table", g.checkboxList).html("");
  221. },
  222. _setData : function(data)
  223. {
  224. this.setData(data);
  225. },
  226. setData: function (data)
  227. {
  228. var g = this, p = this.options;
  229. if (!data || !data.length) return;
  230. g.data = data;
  231. g.refresh();
  232. g.updateStyle();
  233. },
  234. refresh:function()
  235. {
  236. var g = this, p = this.options, data = this.data;
  237. this.clearContent();
  238. if (!data) return;
  239. var out = [], rowSize = p.rowSize, appendRowStart = false, name = p.name || g.id;
  240. for (var i = 0; i < data.length; i++)
  241. {
  242. var val = data[i][p.valueField], txt = data[i][p.textField], id = g.id + "-" + i;
  243. var newRow = i % rowSize == 0;
  244. //0,5,10
  245. if (newRow)
  246. {
  247. if (appendRowStart) out.push('</tr>');
  248. out.push("<tr>");
  249. appendRowStart = true;
  250. }
  251. out.push("<td><input type='checkbox' name='" + name + "' value='" + val + "' id='" + id + "'/><label for='" + id + "'>" + txt + "</label></td>");
  252. }
  253. if (appendRowStart) out.push('</tr>');
  254. g.checkboxList.table.append(out.join(''));
  255. },
  256. _getValue: function ()
  257. {
  258. var g = this, p = this.options, name = p.name || g.id;
  259. var values = [];
  260. $('input:checkbox[name="' + name + '"]:checked').each(function ()
  261. {
  262. values.push(this.value);
  263. });
  264. if (!values.length) return null;
  265. return values.join(p.split);
  266. },
  267. getValue: function ()
  268. {
  269. //获取值
  270. return this._getValue();
  271. },
  272. updateStyle: function ()
  273. {
  274. this._dataInit();
  275. },
  276. _dataInit: function ()
  277. {
  278. var g = this, p = this.options;
  279. var value = g.valueField.val();
  280. g._changeValue(value);
  281. },
  282. //设置值到 隐藏域
  283. _changeValue: function (value)
  284. {
  285. var g = this, p = this.options, name = p.name || g.id;
  286. var valueArr = value ? value.split(p.split) : [];
  287. $("input:checkbox[name='" + name + "']", g.checkboxList).each(function ()
  288. {
  289. this.checked = $.inArray(this.value, valueArr) > -1;
  290. });
  291. g.valueField.val(value);
  292. g.selectedValue = value;
  293. },
  294. _addClickEven: function ()
  295. {
  296. var g = this, p = this.options;
  297. //选项点击
  298. g.checkboxList.click(function (e)
  299. {
  300. var value = g.getValue();
  301. if (value) g.valueField.val(value);
  302. });
  303. }
  304. });
  305. })(jQuery);