ligerEx.js 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  1. /**
  2. * ligerUI 扩展组件
  3. */
  4. (function ($, win) {
  5. /******************** 条件选取控件 ***********************/
  6. //add by hzp at 20160223
  7. $.fn.ligerCondition = function ()
  8. {
  9. return $.ligerui.run.call(this, "ligerCondition", arguments);
  10. };
  11. $.ligerDefaults.Condition = {
  12. nameField:"code",
  13. textField:"text",
  14. typeField:"type",
  15. dictField:"dict",
  16. disabled: false,
  17. readonly: false,
  18. andOrData:[{code:" AND ",value:"并且"},{code:" OR ",value:"或者"}],
  19. stringTypeData:[{code:" = ",value:"等于"},{code:" LIKE ",value:"模糊匹配"},{code:" IN ",value:"包含"},{code:" NOT IN ",value:"不包含"}],
  20. numberTypeData:[{code:" = ",value:"等于"},{code:" <> ",value:"不等于"},{code:" > ",value:"大于"},{code:" >= ",value:"大于等于"},{code:" < ",value:"小于"},{code:" <= ",value:"小于等于"}],
  21. dateTypeData:[{code:" = ",value:"等于"},{code:" <> ",value:"不等于"},{code:" > ",value:"大于"},{code:" >= ",value:"大于等于"},{code:" < ",value:"小于"},{code:" <= ",value:"小于等于"}],
  22. fields:[], //条件字段
  23. value:'',
  24. stdDictUrl:'', //字典数据
  25. stdDictVersion:'',//字典版本号
  26. isMoveLeft:true,//默认第二条件开始左移
  27. index:0,
  28. andOrControlFlag:"and_or",
  29. fieldControlFlag:"field",
  30. conditionControlFlag:"condition",
  31. valueControlFlag:"value"
  32. };
  33. $.ligerMethos.Condition = {};
  34. $.ligerui.controls.Condition = function (element, options)
  35. {
  36. $.ligerui.controls.Condition.base.constructor.call(this, element, options);
  37. };
  38. $.ligerui.controls.Condition.ligerExtend($.ligerui.controls.Input,
  39. {
  40. _getType: function ()
  41. {
  42. return 'Condition';
  43. },
  44. _extendMethods: function ()
  45. {
  46. return $.ligerMethos.Condition;
  47. },
  48. _init: function () {
  49. $.ligerui.controls.Condition.base._init.call(this);
  50. },
  51. _render: function ()
  52. {
  53. var me = this, p = this.options;
  54. me.inputValue = null;
  55. me.inputControl = null;
  56. me.value = null;
  57. me.textFieldID = "";
  58. if (this.element.tagName.toLowerCase() == "input" && this.element.type)
  59. {
  60. if(this.element.type == "text")
  61. {
  62. $(this.element).attr("type","hidden");
  63. }
  64. me.inputValue = $(this.element);
  65. if (this.element.id)
  66. me.textFieldID = this.element.id;
  67. }
  68. if (me.textFieldID == "" && p.textFieldID)
  69. me.textFieldID = p.textFieldID;
  70. me.addButton = $('<div class="l-button l-button-green" style="clear:both;margin-left:0px;" id="'+me.textFieldID+'_add_btn"><span>添加条件</span></div>');
  71. me.content = $("<div id='"+me.textFieldID+"_content' class='m-form-condition'></div>");
  72. me.inputValue.after(me.addButton).after(me.content);
  73. /*if (p.disabled)
  74. {
  75. me.inputValue.parent().addClass("l-text-disabled");
  76. }*/
  77. //初始化值
  78. me.addButton.click(function () {
  79. me.newItem();
  80. });
  81. if(p.value!=undefined && p.value.length >0)
  82. {
  83. me.setValue(p.value);
  84. }
  85. else{
  86. me.newItem();
  87. }
  88. },
  89. //设置字段
  90. setFields:function(fields){
  91. var me =this;
  92. var option = me.options;
  93. option.fields = fields;
  94. me.setValue("");
  95. },
  96. //设置字典参数
  97. setDictParms:function(parms){
  98. var me =this;
  99. var option = me.options;
  100. option.stdDictParms = parms;
  101. },
  102. //赋值
  103. setValue:function(data){
  104. try{
  105. var me = this;
  106. me.content.find("div.m-form-group").remove();
  107. if(data!=null && data.length>0 && data!="[]")
  108. {
  109. //判断是否字符串
  110. if(typeof(data)=="string")
  111. {
  112. data = eval(data);
  113. }
  114. for(var i=0;i<data.length;i++)
  115. {
  116. me.newItem(data[i].andOr,data[i].field,data[i].condition,data[i].value);
  117. }
  118. }
  119. else{
  120. me.newItem();
  121. }
  122. }
  123. catch(e)
  124. {
  125. }
  126. },
  127. //取值
  128. getValue:function(){
  129. var me = this;
  130. var option = me.options;
  131. var re = '';
  132. $.each(me.content.find("div.m-form-group"),function(index,item){
  133. var o = '{';
  134. var groupId = this.id;
  135. var andOrControlId = groupId+"_"+option.andOrControlFlag;
  136. var fieldControlId = groupId+"_"+option.fieldControlFlag;
  137. var conditionControlId =groupId+"_"+option.conditionControlFlag;
  138. var valueControlId =groupId+"_"+option.valueControlFlag;
  139. var andOr = " AND ";
  140. if(index!=0) {
  141. andOr = $("#"+andOrControlId).ligerComboBox("getValue");
  142. }
  143. var field = $("#"+fieldControlId).ligerComboBox("getValue");
  144. var condition = $("#"+conditionControlId).ligerComboBox("getValue");
  145. var value = $("#"+valueControlId).val();
  146. var type = $("#"+valueControlId+"_div").attr("controlType");
  147. if(type == "combobox")
  148. {
  149. value = $("#"+valueControlId).ligerComboBox("getValue");
  150. }
  151. else if(type == "mult_combobox" || type =="mult_textbox"){
  152. value = $("#"+valueControlId).ligerMultbox("getValue");
  153. }
  154. if(andOr.length>0 && field.length>0 && condition.length>0 && value.length>0)
  155. {
  156. o+='andOr:"'+andOr+'",';
  157. o+='field:"'+field+'",';
  158. o+='condition:"'+condition+'",';
  159. o+='value:"'+value+'"';
  160. o+='}';
  161. if(re.length>0)
  162. {
  163. re += ","+o;
  164. }
  165. else{
  166. re = o;
  167. }
  168. }
  169. });
  170. return '['+re+']';
  171. },
  172. //创建元素
  173. newItem:function(andOr,field,condition,value){
  174. var me = this;
  175. var option = me.options;
  176. var index = option.index;
  177. var groupId = me.textFieldID+"_"+index;
  178. var andOrControlId = groupId+"_"+option.andOrControlFlag;
  179. var fieldControlId = groupId+"_"+option.fieldControlFlag;
  180. var conditionControlId =groupId+"_"+option.conditionControlFlag;
  181. var valueControlId = groupId+"_"+option.valueControlFlag;
  182. var html = '<div class="m-form-group" id="'+groupId+'">';
  183. if(me.content.find("div.m-form-group").length >0)
  184. {
  185. if(option.isMoveLeft)
  186. {
  187. html = '<div class="m-form-group m-form-move-left" id="'+groupId+'">';
  188. }
  189. html += '<div class="m-form-control"><input type="text" id="'+andOrControlId+'" data-type="select"/></div>';
  190. }
  191. html += '<div class="m-form-control"><input type="text" id="'+fieldControlId+'" data-type="select"/></div>';
  192. html += '<div class="m-form-control"><input type="text" id="'+conditionControlId+'" data-type="select"/></div>';
  193. html += '<div class="m-form-control" controlType="textbox" id="'+valueControlId+'_div" style="margin-left:0px;"><input type="text" id="'+valueControlId+'" class="l-textbox"/></div>';
  194. if(index >0)
  195. {
  196. html += '<div class="m-form-control"><a class="m-btn-cancle" href="javascrip:void()" style="display:block;" onclick="$(\'#'+groupId+'\').remove()"></a></div>';
  197. }
  198. html += '</div>';
  199. me.content.append(html);
  200. if(index!=0)
  201. {
  202. $("#"+andOrControlId).ligerComboBox({width:65,data:option.andOrData,value:"and"});
  203. }
  204. $("#"+fieldControlId).ligerComboBox({width:100,data:option.fields,textField:"text",extendField:"type",
  205. onSelected:function(value,text,row){
  206. //改变条件控件
  207. if(row.type=="NUMERIC")
  208. {
  209. $("#"+conditionControlId).ligerComboBox("setData",option.numberTypeData)
  210. }
  211. else if(row.type=="DATE"){
  212. $("#"+conditionControlId).ligerComboBox("setData",option.dateTypeData)
  213. }
  214. else{
  215. $("#"+conditionControlId).ligerComboBox("setData",option.stringTypeData)
  216. }
  217. $("#"+conditionControlId).ligerComboBox("setValue"," = ");
  218. }
  219. });
  220. $("#"+conditionControlId).ligerComboBox({width:80});
  221. if(andOr!=undefined)
  222. {
  223. $("#"+andOrControlId).ligerComboBox("setValue",andOr);
  224. }
  225. if(field!=undefined)
  226. {
  227. $("#"+fieldControlId).ligerComboBox("setValue",field);
  228. }
  229. if(condition!=undefined)
  230. {
  231. $("#"+conditionControlId).ligerComboBox("setValue",condition);
  232. }
  233. if(value!=undefined)
  234. {
  235. me.changeValueType(index,value);
  236. //$("#"+conditionControlId).ligerComboBox("setValue",condition);
  237. }
  238. //绑定事件
  239. $("#"+conditionControlId).ligerComboBox({
  240. onSelected:function(value,text){
  241. //改变值控件
  242. me.changeValueType(index);
  243. }
  244. });
  245. option.index = index+1;
  246. },
  247. //改变值控件类型
  248. changeValueType:function(index,value){
  249. try{
  250. var me = this;
  251. var option = me.options;
  252. var groupId = me.textFieldID+"_"+index;
  253. var fieldControlId = groupId+"_"+option.fieldControlFlag;
  254. var conditionControlId = groupId+"_"+option.conditionControlFlag;
  255. var field = $("#"+fieldControlId).ligerComboBox("getSelected");
  256. var condition = $("#"+conditionControlId).ligerComboBox("getValue");
  257. if(field==null || condition==null)
  258. {
  259. return;
  260. }
  261. var dict =field.dict;
  262. var dataType = field.type;
  263. var type="textbox";
  264. if(dataType == "NUMERIC")//数值
  265. {
  266. type = "numberbox";
  267. }
  268. else if(dataType == "DATE") //时间
  269. {
  270. type = "datebox";
  271. }
  272. else{
  273. if(dict!=null && dict!="0")//字典控件
  274. {
  275. if(condition == " IN " || condition == " NOT IN ")
  276. {
  277. type = "mult_combobox";
  278. }
  279. else{
  280. type = "combobox";
  281. }
  282. }
  283. else{
  284. if(condition == " IN " || condition == " NOT IN ")
  285. {
  286. type = "mult_textbox";
  287. }
  288. }
  289. }
  290. me.createValueControl(index,type,dict,value);
  291. }
  292. catch(e)
  293. {
  294. return;
  295. }
  296. },
  297. //创建值控件
  298. createValueControl:function(index,type,dict,value){
  299. var me = this;
  300. var option = me.options;
  301. var groupId = me.textFieldID+"_"+index;
  302. var valueControlId = groupId+"_"+option.valueControlFlag;
  303. var valueDiv =$("#"+groupId+"_"+option.valueControlFlag+"_div");
  304. var oldType = valueDiv.attr("controlType");
  305. if(oldType != type || value!=undefined)
  306. {
  307. if(value==undefined) value = "";
  308. valueDiv.attr("controlType",type)
  309. if(type=="combobox") //字典
  310. {
  311. valueDiv.html('<input type="text" id="'+valueControlId+'" data-type="select"/>');
  312. var parms = {dictId:dict};
  313. $.extend(parms,option.stdDictParms);
  314. $("#"+valueControlId).ligerComboBox({width:200,url:option.stdDictUrl,urlParms:parms,value:value});
  315. }
  316. else if(type=="datebox") //时间
  317. {
  318. valueDiv.html('<input type="text" id="'+valueControlId+'" />');
  319. $("#"+valueControlId).ligerDateEditor({width:200,value:value});
  320. }
  321. else if(type=="numberbox") //数值
  322. {
  323. valueDiv.html('<input type="text" id="'+valueControlId+'" />');
  324. $("#"+valueControlId).ligerTextBox({width: 200,digits:true,value:value});
  325. }
  326. else if(type=="mult_combobox") //多选字典
  327. {
  328. valueDiv.html('<input type="text" id="'+valueControlId+'" />');
  329. var parms = {dictId:dict};
  330. $.extend(parms,option.stdDictParms);
  331. $("#"+valueControlId).ligerMultbox({width:200,stdDictUrl:option.stdDictUrl,urlParms:parms,value:value});
  332. }
  333. else if(type=="mult_textbox") //多项文本
  334. {
  335. valueDiv.html('<input type="text" id="'+valueControlId+'" />');
  336. $("#"+valueControlId).ligerMultbox({width:200,value:value});
  337. }
  338. else //文本框
  339. {
  340. valueDiv.html('<input type="text" id="'+valueControlId+'" class="l-textbox" value="'+value+'"/>');
  341. }
  342. }
  343. }
  344. });
  345. /******************* 搜索控件 *************************************/
  346. //add by hzp at 20160223
  347. $.fn.ligerSearch = function ()
  348. {
  349. return $.ligerui.run.call(this, "ligerSearch", arguments);
  350. };
  351. $.ligerDefaults.Search = {
  352. onChange: null, //改变值事件
  353. onClick: null, //搜索按钮事件
  354. disabled: false,
  355. readonly: false //是否只读
  356. };
  357. $.ligerMethos.Search = {};
  358. $.ligerui.controls.Search = function (element, options)
  359. {
  360. $.ligerui.controls.Search.base.constructor.call(this, element, options);
  361. };
  362. $.ligerui.controls.Search.ligerExtend($.ligerui.controls.Input, {
  363. __getType: function ()
  364. {
  365. return 'Search';
  366. },
  367. _extendMethods: function ()
  368. {
  369. return $.ligerMethos.Search;
  370. },
  371. _init: function () {
  372. $.ligerui.controls.Search.base._init.call(this);
  373. },
  374. _render: function ()
  375. {
  376. var g = this, p = this.options;
  377. g.inputText = null;
  378. g.value = null;
  379. g.textFieldID = "";
  380. if (this.element.tagName.toLowerCase() == "input" && this.element.type && this.element.type == "text")
  381. {
  382. g.inputText = $(this.element);
  383. if (this.element.id)
  384. g.textFieldID = this.element.id;
  385. }
  386. else
  387. {
  388. g.inputText = $('<input type="text"/>');
  389. g.inputText.appendTo($(this.element));
  390. }
  391. if (g.textFieldID == "" && p.textFieldID)
  392. g.textFieldID = p.textFieldID;
  393. g.searchBtn = $('<div class="l-trigger l-trigger-search"><div class="l-trigger-icon"></div></div>');
  394. g.wrapper = g.inputText.wrap('<div class="l-text"></div>').parent();
  395. g.wrapper.append(g.searchBtn);
  396. g.inputText.addClass("l-text-field");
  397. //设置宽度
  398. if(p.width)
  399. {
  400. g.wrapper.width(p.width);
  401. g.inputText.width(p.width-12);
  402. }
  403. if (p.disabled)
  404. {
  405. g.wrapper.addClass("l-text-disabled");
  406. }
  407. //初始化
  408. if (g.value!=null)
  409. {
  410. g.setValue(g.value);
  411. }
  412. //点击事件
  413. if(!p.disabled && p.onClick!=null && typeof(p.onClick)=="function")
  414. {
  415. g.searchBtn.click(function(){
  416. p.onClick(g.getValue());
  417. });
  418. }
  419. //值改变事件
  420. if(!p.disabled && p.onChange!=null && typeof(p.onChange)=="function")
  421. {
  422. g.inputText.change(function () {
  423. p.onChange(g.getValue());
  424. });
  425. }
  426. g.inputText.blur(function ()
  427. {
  428. if (!p.disabled)
  429. g.wrapper.removeClass("l-text-focus");
  430. }).focus(function ()
  431. {
  432. if (!p.disabled)
  433. g.wrapper.addClass("l-text-focus");
  434. });
  435. g.wrapper.hover(function ()
  436. {
  437. if (!p.disabled)
  438. g.wrapper.addClass("l-text-over");
  439. }, function ()
  440. {
  441. g.wrapper.removeClass("l-text-over");
  442. });
  443. },
  444. setValue: function (value)
  445. {
  446. var g = this, p = this.options;
  447. if (!value || value == "NaN") value = "";
  448. this.inputText.val(value)
  449. },
  450. getValue:function()
  451. {
  452. return this.inputText.val();
  453. }
  454. });
  455. /******************* 多选控件 *************************************/
  456. //add by hzp at 20160223
  457. $.fn.ligerMultbox = function ()
  458. {
  459. return $.ligerui.run.call(this, "ligerMultbox", arguments);
  460. };
  461. $.ligerDefaults.Multbox = {
  462. disabled: false,
  463. readonly: false, //是否只读
  464. boxType:"text",
  465. gridOptions: {
  466. },
  467. valueField: 'CustomerID',
  468. textField: 'CustomerID'
  469. };
  470. $.ligerMethos.Multbox = {};
  471. $.ligerui.controls.Multbox = function (element, options)
  472. {
  473. $.ligerui.controls.Multbox.base.constructor.call(this, element, options);
  474. };
  475. $.ligerui.controls.Multbox.ligerExtend($.ligerui.controls.Input, {
  476. __getType: function ()
  477. {
  478. return 'Multbox';
  479. },
  480. _extendMethods: function ()
  481. {
  482. return $.ligerMethos.Multbox;
  483. },
  484. _init: function () {
  485. $.ligerui.controls.Multbox.base._init.call(this);
  486. },
  487. _render: function ()
  488. {
  489. var g = this, p = this.options;
  490. g.inputValue = null;
  491. g.inputControl = null;
  492. g.value = null;
  493. g.textFieldID = "";
  494. if (this.element.tagName.toLowerCase() == "input" && this.element.type)
  495. {
  496. if(this.element.type == "text")
  497. {
  498. $(this.element).attr("type","hidden");
  499. }
  500. g.inputValue = $(this.element);
  501. if (this.element.id)
  502. g.textFieldID = this.element.id;
  503. }
  504. if (g.textFieldID == "" && p.textFieldID)
  505. g.textFieldID = p.textFieldID;
  506. g.addBtn = $('<div class="m-form-mult-add"></div>');
  507. g.addBtnWrapper = g.addBtn.wrap('<div class="m-form-mult-add-panel"></div>').parent();
  508. g.addBtnWrapper.append('<div class="m-form-mult-control-panel"><input type="text" id="'+g.textFieldID+'_control" class="l-textbox" style="float:left;width:135px;margin-top:5px;margin-left:5px;"/><a href="javascript:void(0);" id="'+g.textFieldID+'_control_cancle" class="m-btn-cancle" style="float: right"></a><a href="javascript:void(0);" id="'+g.textFieldID+'_control_ok" class="m-btn-ok" style="float: right"></a></div>');
  509. g.wrapper = g.inputValue.wrap('<div class="m-form-mult"></div>').parent();
  510. g.wrapper.append('<div class="m-form-mult-blank"></div><div class="m-form-mult-content" id="'+g.textFieldID+'_content"></div>').after(g.addBtnWrapper);
  511. g.inputControl = $("#"+g.textFieldID+"_control");
  512. g.content = $("#"+g.textFieldID+"_content");
  513. g.wrapper.wrap('<div style="position: relative"></div>');
  514. //字典控件
  515. if(p.stdDictUrl!=null)
  516. {
  517. p.boxType = "combo";
  518. /*g.inputControl.ligerComboBox({width:135,url:p.stdDictUrl,urlParms:p.urlParms});
  519. if(g.inputControl[0].type == "text")
  520. {
  521. $(g.inputControl).attr("type","hidden");
  522. }*/
  523. }
  524. g.setWidth();
  525. //初始化
  526. if (p.value!=null)
  527. {
  528. g.setValue(p.value);
  529. }
  530. //禁用
  531. if (p.disabled)
  532. {
  533. g.wrapper.addClass("l-text-disabled");
  534. //按钮失效
  535. return;
  536. }
  537. //添加按钮
  538. g.addBtn.click(function(){
  539. if(p.boxType != "text")
  540. {
  541. //弹窗多选字典数据
  542. var val = g.getValue();
  543. g.selectDialog(val,function(val,text){
  544. g.setValue(val,text);
  545. });
  546. }
  547. else
  548. {
  549. g.inputControl.val('');
  550. $(this).next().show();
  551. }
  552. })
  553. //添加确认按钮
  554. $("#"+g.textFieldID+"_control_ok").click(function(){
  555. var val = g.inputControl.val();
  556. var text = g.inputControl.val();
  557. if(val!=null && val.length>0)
  558. {
  559. if(g.inputControl.prev().hasClass('l-text-combobox')){
  560. text = g.inputControl.ligerComboBox("getText");
  561. }
  562. if(val.indexOf(',')>=0)
  563. {
  564. $.ligerDialog.error("不能包含特殊字符!");
  565. return;
  566. }
  567. g.newItem(val,text);
  568. $(this).parent().hide();
  569. }
  570. });
  571. //添加取消按钮
  572. $("#"+g.textFieldID+"_control_cancle").click(function(){
  573. $(this).parent().hide();
  574. });
  575. },
  576. newItem:function(val,text){
  577. var me = this;
  578. var b = true;
  579. $.each(me.content.find("div"),function(index,item){
  580. var value = $(item).attr("value");
  581. if(value == val)
  582. {
  583. b=false;
  584. return;
  585. }
  586. });
  587. if(b)
  588. {
  589. me.content.append('<div value="'+val+'">'+text+'<a href="javascript:void(0);" onclick="$(this).parent().remove()"></a></div>');
  590. }
  591. },
  592. setWidth:function(){
  593. var me = this;
  594. var option = me.options;
  595. if(option.width!=null)
  596. {
  597. me.wrapper.css({width:option.width});
  598. me.content.css({width:option.width-5});
  599. me.addBtnWrapper.css({width:option.width});
  600. }
  601. },
  602. setValue: function (val,text)
  603. {
  604. var me = this, option = me.options;
  605. if (!val || val == "NaN") val = "";
  606. me.content.find("div").remove();
  607. if(text!=undefined&&text.length>0)
  608. {
  609. var arr = val.split(',');
  610. var arrText = text.split(',');
  611. if(arr.length == arrText.length)
  612. {
  613. for(var i=0;i<arr.length;i++)
  614. {
  615. me.newItem(arr[i],arrText[i]);
  616. }
  617. }
  618. }
  619. else{
  620. if(val.length>0)
  621. {
  622. var arr = val.split(',');
  623. if(option.boxType!="text")
  624. {
  625. $.ajax({ //获取表的字段列表
  626. type: "POST",
  627. url : option.stdDictUrl,
  628. dataType : "json",
  629. data:option.urlParms,
  630. cache:false,
  631. success :function(data){
  632. if(data.successFlg) {
  633. if(data.detailModelList!=null && data.detailModelList.length>0)
  634. {
  635. for(var i=0;i<arr.length;i++)
  636. {
  637. for(var j=0;j<data.detailModelList.length;j++)
  638. {
  639. var code = data.detailModelList[j].code;
  640. if(code==arr[i])
  641. {
  642. me.newItem(code,data.detailModelList[j].value);
  643. break;
  644. }
  645. }
  646. }
  647. }
  648. }
  649. else{
  650. $.ligerDialog.error(data.message);
  651. }
  652. },
  653. error :function(data){
  654. $.ligerDialog.error("Status:"+data.status +"(" +data.statusText+")");
  655. }
  656. });
  657. }
  658. else{
  659. for(var i=0;i<arr.length;i++)
  660. {
  661. me.newItem(arr[i],arr[i]);
  662. }
  663. }
  664. }
  665. }
  666. me.inputValue.val(val);
  667. },
  668. getValue:function()
  669. {
  670. var me = this;
  671. var val = "";
  672. $.each(me.content.find("div"),function(index,item){
  673. var value = $(item).attr("value");
  674. if(val.length==0)
  675. {
  676. val = value;
  677. }
  678. else{
  679. val += ","+value;
  680. }
  681. });
  682. me.inputValue.val(val);
  683. return val;
  684. },
  685. //弹窗选取字典
  686. selectDialog:function(val,fun){
  687. var me = this;
  688. var option = me.options;
  689. if(option.stdDictUrl!=null)
  690. {
  691. me.popupFn = $.ligerui.getPopupFn({
  692. width:550,
  693. /*condition: {
  694. autoEditors:true,
  695. fields: [{ display: "名称", name: "value", type: 'text' }]
  696. },*/
  697. grid: {
  698. columns: [
  699. { display: '代码', name: 'code' },
  700. { display: '名称', name: 'value' },
  701. { display: '属性', name: 'extend' }
  702. ],
  703. switchPageSizeApplyComboBox: false,
  704. url: option.stdDictUrl,
  705. parms:option.urlParms,
  706. pageSize: 50,
  707. checkbox: true
  708. },
  709. valueField: 'code',
  710. textField: 'value',
  711. split: ',',
  712. onSelect: function (e)
  713. {
  714. fun(e.value,e.text);
  715. me.trigger('selected', e);
  716. },
  717. selectInit: function (rowdata)
  718. {
  719. var re =false;
  720. if(val!=undefined&&val.length>0)
  721. {
  722. var arr = val.split(',');
  723. for(var i=0;i<arr.length;i++)
  724. {
  725. if(arr[i]==rowdata.code)
  726. {
  727. re = true;
  728. break;
  729. }
  730. }
  731. }
  732. return re;
  733. }
  734. });
  735. me.popupFn();
  736. }
  737. }
  738. });
  739. /***************** 表单控件 **********************************/
  740. //add by hzp at 20160312
  741. $.fn.ligerAutoForm = function ()
  742. {
  743. return $.ligerui.run.call(this, "ligerAutoForm", arguments);
  744. };
  745. $.ligerDefaults.AutoForm = {
  746. data:null,
  747. readonly: false, //是否只读
  748. validate:null, //校验
  749. message:null //校验提示
  750. };
  751. $.ligerMethos.AutoForm = {};
  752. $.ligerui.controls.AutoForm = function (element, options)
  753. {
  754. $.ligerui.controls.AutoForm.base.constructor.call(this, element, options);
  755. };
  756. $.ligerui.controls.AutoForm.ligerExtend($.ligerui.core.UIComponent, {
  757. controls:[], //表单控件
  758. defaultMessage:{
  759. required: "必填项",
  760. email: "请输入有效的电子邮件地址",
  761. url: "请输入有效的网址",
  762. date: "请输入有效的日期",
  763. dateISO: "请输入有效的日期 (YYYY-MM-DD)",
  764. number: "请输入有效的数字",
  765. digits: "只能输入数字",
  766. equalTo: "你的输入不相同",
  767. extension: "请输入有效的后缀",
  768. maxlength: "最多可以输入{0}个字符",
  769. minlength: "最少要输入{0}个字符",
  770. max: "请输入不大于{0}的数值",
  771. min: "请输入不小于{0}的数值",
  772. custom:"自定义函数出错"
  773. },
  774. __getType: function ()
  775. {
  776. return 'AutoForm'
  777. },
  778. __idPrev: function ()
  779. {
  780. return 'AutoForm';
  781. },
  782. _init: function ()
  783. {
  784. var g = this, p = this.options;
  785. $.ligerui.controls.AutoForm.base._init.call(this);
  786. },
  787. _render: function ()
  788. {
  789. var me = this, option = this.options;
  790. /********** 初始化控件 ***********/
  791. me.controls = [];
  792. var jform = $(this.element);
  793. $("textarea", jform).each(function () {
  794. var name = $(this).attr(name);
  795. if(name!=undefined && name.length>0) {
  796. me.controls.push({name:name,control:$(this)});
  797. }
  798. });
  799. $("input", jform).each(function ()
  800. {
  801. try{
  802. var name = $(this).attr("name");
  803. var control = null;
  804. var controlType = "html"; //html为html控件 ui为ligerUI控件
  805. var rule = null; //校验规则
  806. var message = null; //校验提示信息
  807. if(name!=undefined && name.length>0)
  808. {
  809. //是否已存在
  810. for(var i=0;i<me.controls.length;i++)
  811. {
  812. if(me.controls[i].name == name)
  813. {
  814. return;
  815. }
  816. }
  817. var type = $(this).attr("type");
  818. if(type == "radio")//单选
  819. {
  820. control = $('input[name="'+name+'"]').ligerRadio();
  821. controlType = "ui";
  822. }
  823. else if(type == "checkbox")//多选
  824. {
  825. control = $('input[name="'+name+'"]').ligerCheckBox();
  826. controlType = "ui";
  827. }
  828. else if(type=="hidden")
  829. {
  830. if($(this).attr("data-ligerid"))
  831. {
  832. return;
  833. }
  834. else{
  835. control = $(this);
  836. }
  837. }
  838. else{
  839. var wrap = $(this).parent();
  840. if(wrap.hasClass("l-text-combobox")) //下拉控件
  841. {
  842. control = $(this).ligerComboBox();
  843. controlType = "ui";
  844. }
  845. else if(wrap.hasClass("l-text-date"))
  846. {
  847. control = $(this).ligerDateEditor();
  848. controlType = "ui";
  849. }
  850. else{
  851. control = $(this);
  852. }
  853. }
  854. //相关校验规则
  855. if(option.validate!=null && option.validate[name]!=null)
  856. {
  857. rule = option.validate[name];
  858. if(option.message!=null && option.message[name]!=null)
  859. {
  860. message = option.message[name];
  861. }
  862. }
  863. me.controls.push({name:name,controlType:controlType,control:control,rule:rule,message:message});
  864. }
  865. }
  866. catch(e)
  867. {
  868. return;
  869. }
  870. });
  871. /********* 初始化数据 **********/
  872. if(option.data)
  873. {
  874. me.setData(option.data);
  875. }
  876. /********** 只读 ************/
  877. if(option.readonly)
  878. {
  879. me.readOnly();
  880. }
  881. },
  882. //初始化数据
  883. setData:function(data){
  884. try{
  885. var me = this, option = this.options;
  886. for(var i=0;i<me.controls.length;i++)
  887. {
  888. var controlName = me.controls[i].name;
  889. var control = me.controls[i].control;
  890. var controlType = me.controls[i].controlType;
  891. var controlData = data[controlName];
  892. if(controlData!=undefined)
  893. {
  894. if(controlType=="ui"&&typeof(control.setValue)=="function")
  895. {
  896. control.setValue(controlData);
  897. }
  898. else{
  899. if(typeof(control.setValue)=="function")
  900. {
  901. control.setValue(controlData);
  902. }
  903. if(typeof(control.val)=="function")
  904. {
  905. control.val(controlData);
  906. }
  907. else{
  908. control.html(controlData);
  909. }
  910. }
  911. }
  912. }
  913. }
  914. catch(e)
  915. {
  916. return;
  917. }
  918. },
  919. //校验规则
  920. validateRule:function(rule,ruleValue,value){
  921. try{
  922. var me = this;
  923. //字符串格式化方法
  924. String.prototype.format= function() {
  925. var args = arguments;
  926. return this.replace(/\{(\d+)\}/g, function (s, i) {
  927. return args[i];
  928. });
  929. };
  930. var message = me.defaultMessage[rule];
  931. if(rule=="required" && (ruleValue ==true ||ruleValue=="true"))
  932. {
  933. if(value == null || value.length ==0)
  934. {
  935. return message;
  936. }
  937. }
  938. else{
  939. if(value !=null &&value.length>0)
  940. {
  941. switch (rule)
  942. {
  943. case "email":
  944. {
  945. break;
  946. }
  947. case "url":
  948. {
  949. break;
  950. }
  951. case "date":
  952. {
  953. break;
  954. }
  955. case "dateISO":
  956. {
  957. break;
  958. }
  959. case "number":
  960. {
  961. break;
  962. }
  963. case "digits":
  964. {
  965. break;
  966. }
  967. case "equalTo"://你的输入不相同
  968. {
  969. break;
  970. }
  971. case "extension"://请输入有效的后缀
  972. {
  973. break;
  974. }
  975. case "maxlength"://最多可以输入 {0} 个字符
  976. {
  977. if(value.length>ruleValue)
  978. {
  979. return message.format(ruleValue);
  980. }
  981. break;
  982. }
  983. case "minlength"://最少要输入 {0} 个字符
  984. {
  985. if(value.length<ruleValue)
  986. {
  987. return message.format(ruleValue);
  988. }
  989. break;
  990. }
  991. case "max"://请输入不大于 {0} 的数值
  992. {
  993. if(value>ruleValue)
  994. {
  995. return message.format(ruleValue);
  996. }
  997. break;
  998. }
  999. case "min"://请输入不小于 {0} 的数值
  1000. {
  1001. if(value<ruleValue)
  1002. {
  1003. return message.format(ruleValue);
  1004. }
  1005. break;
  1006. }
  1007. }
  1008. }
  1009. }
  1010. return "";
  1011. }
  1012. catch(e)
  1013. {
  1014. return e;
  1015. }
  1016. },
  1017. //校验值
  1018. validate:function(){
  1019. try {
  1020. var me = this, option = this.options;
  1021. var suc = true;
  1022. if(me.controls!=null)
  1023. {
  1024. var data = me.getData();
  1025. for(var i=0;i<me.controls.length;i++)
  1026. {
  1027. var control = me.controls[i];
  1028. var name = control["name"];
  1029. var rule = control["rule"];
  1030. var message = control["message"];
  1031. var error = "";
  1032. if(rule!=null)
  1033. {
  1034. var value = data[name];
  1035. if(typeof(rule)=="string")
  1036. {
  1037. error = me.validateRule(rule,true,value);
  1038. }
  1039. else{
  1040. for(var o in rule)
  1041. {
  1042. var val = rule[o];
  1043. var re = me.validateRule(o,val,value);
  1044. if(error.length>0)
  1045. {
  1046. error += ";"+re;
  1047. }
  1048. else{
  1049. error = re;
  1050. }
  1051. }
  1052. }
  1053. }
  1054. //是否报错
  1055. if(error.length>0)
  1056. {
  1057. if(message!=null && message.length > 0)
  1058. {
  1059. me.showError(control,message);
  1060. }
  1061. else{
  1062. me.showError(control,error);
  1063. }
  1064. suc = false;
  1065. }
  1066. else{
  1067. me.clearError(control);
  1068. }
  1069. }
  1070. }
  1071. return suc;
  1072. }
  1073. catch(e)
  1074. {
  1075. return false;
  1076. }
  1077. },
  1078. //清除某个控件错误提示
  1079. clearError:function(control){
  1080. var me = this;
  1081. var dom;
  1082. if(control.controlType == "ui")
  1083. {
  1084. dom = $(control.control.element).closest(".m-form-control");
  1085. }
  1086. else{
  1087. dom = control.control.closest(".m-form-control");
  1088. }
  1089. dom.removeClass('m-from-error').find('.m-from-error-msg').remove();
  1090. },
  1091. //某个控件错误提示
  1092. showError:function(control,error){
  1093. var me = this;
  1094. var dom;
  1095. if(control.controlType == "ui")
  1096. {
  1097. dom = $(control.control.element).closest(".m-form-control");
  1098. }
  1099. else{
  1100. dom = control.control.closest(".m-form-control");
  1101. }
  1102. if(dom.hasClass('m-from-error'))
  1103. {
  1104. me.clearError(control);
  1105. }
  1106. var el = $('<div class="m-from-error-msg"></div>');
  1107. dom.addClass('m-from-error').append(el);
  1108. el.hover(function ()
  1109. {
  1110. $(this).ligerTip({content:error});
  1111. },function (){
  1112. $(this).ligerHideTip();
  1113. });
  1114. },
  1115. //只读操作
  1116. readOnly:function(){
  1117. },
  1118. //获取数据
  1119. getData:function(){
  1120. var me = this, option = this.options;
  1121. var data = {};
  1122. for(var i=0;i<me.controls.length;i++)
  1123. {
  1124. var controlName = me.controls[i].name;
  1125. var control = me.controls[i].control;
  1126. var controlType = me.controls[i].controlType;
  1127. if(controlType=="ui"&&typeof(control.getValue)=="function")
  1128. {
  1129. data[controlName] = control.getValue();
  1130. }
  1131. else{
  1132. if(typeof(control.getValue)=="function")
  1133. {
  1134. data[controlName] = control.getValue();
  1135. }
  1136. if(typeof(control.val)=="function")
  1137. {
  1138. data[controlName] = control.val();
  1139. }
  1140. else{
  1141. data[controlName] = control.html();
  1142. }
  1143. }
  1144. }
  1145. return data;
  1146. }
  1147. });
  1148. })(jQuery, window);