lab.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. var lab = {};
  2. lab.loading = false;
  3. lab.PrevImage = function (rootpath, paths)
  4. {
  5. for (var i in paths)
  6. {
  7. $('<img />').attr('src', rootpath+paths[i]);
  8. }
  9. };
  10. lab.tip = function (message)
  11. {
  12. if (window.wintip)
  13. {
  14. window.wintip.set('content', message);
  15. window.wintip.show();
  16. }
  17. else
  18. {
  19. window.wintip = $.ligerDialog.tip({ content: message });
  20. }
  21. setTimeout(function ()
  22. {
  23. window.wintip.hide()
  24. }, 4000);
  25. };
  26. //显示loading
  27. lab.showLoading = function (message) {
  28. message = message || "正在加载中...";
  29. $('body').append("<div class='jloading'>" + message + "</div>");
  30. $.ligerui.win.mask();
  31. };
  32. //隐藏loading
  33. lab.hideLoading = function (message) {
  34. $('body > div.jloading').remove();
  35. $.ligerui.win.unmask({ id: new Date().getTime() });
  36. }
  37. lab.ajax = function (options)
  38. {
  39. var p = options || {};
  40. $.ajax({
  41. cache: false,
  42. url: p.url,
  43. data: p.data,
  44. dataType: 'json', type: 'post',
  45. beforeSend: function () {
  46. lab.loading = true;
  47. lab.showLoading(p.loading);
  48. },
  49. complete: function () {
  50. lab.loading = false;
  51. lab.hideLoading();
  52. },
  53. success: function (result) {
  54. if (!result.Error) {
  55. if (p.success)
  56. p.success(result.Data, result.Message);
  57. }
  58. else {
  59. if (p.error)
  60. p.error(result.Message);
  61. }
  62. },
  63. error: function (result) {
  64. lab.tip('发现系统错误:' + result);
  65. }
  66. });
  67. };
  68. //填充表单数据
  69. lab.loadForm = function (mainform, data, prefix)
  70. {
  71. //根据返回的属性名,找到相应ID的表单元素,并赋值
  72. prefix = prefix || "";
  73. if (data)
  74. {
  75. for (var p in data)
  76. {
  77. $("#" + prefix + p).val(data[p]);
  78. }
  79. }
  80. //下面是更新表单的样式
  81. var managers = $.ligerui.find($.ligerui.controls.Input);
  82. for (var i = 0, l = managers.length; i < l; i++)
  83. {
  84. //改变了表单的值,需要调用这个方法来更新ligerui样式
  85. managers[i].updateStyle();
  86. }
  87. };
  88. //带验证、带loading的提交
  89. lab.submitForm = function (mainform, success)
  90. {
  91. if (!mainform)
  92. mainform = $("form:first");
  93. if (mainform.valid())
  94. {
  95. mainform.ajaxSubmit({
  96. dataType: 'json',
  97. success: success,
  98. beforeSend: function ()
  99. {
  100. $('body').append("<div class='jloading'>正在保存数据中...</div>");
  101. $.ligerui.win.mask();
  102. },
  103. complete: function ()
  104. {
  105. $('body > div.jloading').remove();
  106. $.ligerui.win.unmask();
  107. }
  108. });
  109. }
  110. else
  111. {
  112. lab.showInvalid();
  113. }
  114. };
  115. //提示 验证错误信息
  116. lab.showInvalid = function (validator)
  117. {
  118. validator = validator || lab.validator;
  119. if (!validator) return;
  120. var message = '<div class="invalid">存在' + validator.errorList.length + '个字段验证不通过,请检查!</div>';
  121. $.ligerDialog.error(message);
  122. };
  123. //表单验证
  124. lab.validate = function (form, options)
  125. {
  126. if (typeof (form) == "string")
  127. form = $(form);
  128. else if (typeof (form) == "object" && form.NodeType == 1)
  129. form = $(form);
  130. options = $.extend({
  131. errorPlacement: function (lable, element)
  132. {
  133. if (!element.attr("id"))
  134. element.attr("id", new Date().getTime());
  135. if (element.hasClass("l-textarea"))
  136. {
  137. element.addClass("l-textarea-invalid");
  138. }
  139. else if (element.hasClass("l-text-field"))
  140. {
  141. element.parent().addClass("l-text-invalid");
  142. }
  143. $(element).removeAttr("title").ligerHideTip();
  144. $(element).attr("title", lable.html()).ligerTip({
  145. distanceX: 5,
  146. distanceY: -3,
  147. auto: true
  148. });
  149. },
  150. success: function (lable)
  151. {
  152. if (!lable.attr("for")) return;
  153. var element = $("#" + lable.attr("for"));
  154. if (element.hasClass("l-textarea"))
  155. {
  156. element.removeClass("l-textarea-invalid");
  157. }
  158. else if (element.hasClass("l-text-field"))
  159. {
  160. element.parent().removeClass("l-text-invalid");
  161. }
  162. $(element).removeAttr("title").ligerHideTip();
  163. }
  164. }, options || {});
  165. lab.validator = form.validate(options);
  166. return lab.validator;
  167. };
  168. //附加表单搜索按钮:搜索、高级搜索
  169. lab.appendSearchButtons = function (form, grid, filterbtn,callback)
  170. {
  171. if (!form) return;
  172. form = $(form);
  173. var container = $('<ul><li id="btn1container"></li><li id="btn2container"></li></ul><div class="l-clear"></div>').appendTo(form);
  174. if (!filterbtn) container.find("#btn2container").remove();
  175. lab.addSearchButtons(form, grid, container.find("li:eq(0)"), filterbtn ? container.find("li:eq(1)") : null,callback);
  176. };
  177. //创建过滤规则
  178. lab.bulidFilterGroup = function (form)
  179. {
  180. if (!form) return null;
  181. var group = { op: "and", rules: [] };
  182. $(":input", form).not(":submit, :reset, :image,:button, [disabled]")
  183. .each(function ()
  184. {
  185. if (!this.name) return;
  186. //field : 标示为搜索用到的字段
  187. if (!$(this).hasClass("field")) return;
  188. if ($(this).val() == null || $(this).val() == "") return;
  189. var op = $(this).attr("op") || "like";
  190. //get the value type(number or date)
  191. var vt = $(this).attr("vt") || "string";
  192. group.rules.push({
  193. op: op,
  194. field: this.name,
  195. value: $(this).val()
  196. });
  197. });
  198. return group;
  199. };
  200. //创建按钮
  201. lab.createButton = function (options)
  202. {
  203. var p = $.extend({
  204. appendTo: $('body')
  205. }, options || {});
  206. var btn = $('<div class="button button2 buttonnoicon" style="width:60px"><div class="button-l"> </div><div class="button-r"> </div> <span></span></div>');
  207. if (p.icon)
  208. {
  209. btn.removeClass("buttonnoicon");
  210. btn.append('<div class="button-icon"> <img src="../' + p.icon + '" /> </div> ');
  211. }
  212. //绿色皮肤
  213. if (p.green)
  214. {
  215. btn.removeClass("button2");
  216. }
  217. if (p.width)
  218. {
  219. btn.width(p.width);
  220. }
  221. if (p.click)
  222. {
  223. btn.click(p.click);
  224. }
  225. if (p.text)
  226. {
  227. $("span", btn).html(p.text);
  228. }
  229. if (typeof (p.appendTo) == "string") p.appendTo = $(p.appendTo);
  230. btn.appendTo(p.appendTo);
  231. };
  232. //创建表单搜索按钮:搜索、高级搜索
  233. lab.addSearchButtons = function (form, grid, btn1Container, btn2Container, callback)
  234. {
  235. if (!form) return;
  236. if (btn1Container)
  237. {
  238. lab.createButton({
  239. appendTo: btn1Container,
  240. text: '搜索',
  241. click: function ()
  242. {
  243. var rule = lab.bulidFilterGroup(form);
  244. loadClientData(rule);
  245. function loadClientData(data)
  246. {
  247. //filterTranslator 在demos/filter/ligerGrid.showFilter.js中定义
  248. var fnbody = ' return ' + filterTranslator.translateGroup(data);
  249. grid.loadData(new Function("o", fnbody));
  250. if (callback) callback();
  251. }
  252. }
  253. });
  254. }
  255. if (btn2Container)
  256. {
  257. lab.createButton({
  258. appendTo: btn2Container,
  259. text: '高级搜索>>',
  260. click: function ()
  261. {
  262. //showFilter 在demos/filter/ligerGrid.showFilter.js中定义
  263. grid.showFilter();
  264. }
  265. });
  266. }
  267. };