ligerEx.js 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  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. showError:function(control,error){
  753. var dom = $(control).closest(".m-form-control");
  754. if(dom.hasClass('m-from-error'))
  755. {
  756. dom.removeClass('m-from-error').find('.m-from-error-msg').remove();
  757. }
  758. var el = $('<div class="m-from-error-msg"></div>');
  759. dom.addClass('m-from-error').append(el);
  760. el.hover(function ()
  761. {
  762. $(this).ligerTip({content:error});
  763. },function (){
  764. $(this).ligerHideTip();
  765. });
  766. }
  767. };
  768. $.ligerui.controls.AutoForm = function (element, options)
  769. {
  770. $.ligerui.controls.AutoForm.base.constructor.call(this, element, options);
  771. };
  772. $.ligerui.controls.AutoForm.ligerExtend($.ligerui.core.UIComponent, {
  773. controls:[], //表单控件
  774. defaultMessage:{
  775. required: "必填项",
  776. email: "请输入有效的电子邮件地址",
  777. ip: "请输入有效的ip地址",
  778. date: "请输入有效的日期",
  779. dateISO: "请输入有效的日期 (YYYY-MM-DD)",
  780. number: "请输入有效的数字",
  781. digits: "只能输入数字",
  782. equalTo: "你的输入不相同",
  783. extension: "请输入有效的后缀",
  784. maxlength: "最多可以输入{0}个字符",
  785. minlength: "最少要输入{0}个字符",
  786. max: "请输入不大于{0}的数值",
  787. min: "请输入不小于{0}的数值",
  788. custom:"自定义函数出错",
  789. phone:"请输入有效的电话号码"
  790. },
  791. __getType: function ()
  792. {
  793. return 'AutoForm'
  794. },
  795. __idPrev: function ()
  796. {
  797. return 'AutoForm';
  798. },
  799. _init: function ()
  800. {
  801. var g = this, p = this.options;
  802. $.ligerui.controls.AutoForm.base._init.call(this);
  803. },
  804. _render: function ()
  805. {
  806. var me = this, option = this.options;
  807. /********** 初始化控件 ***********/
  808. me.controls = [];
  809. var jform = $(this.element);
  810. $("textarea", jform).each(function () {
  811. try {
  812. var name = $(this).attr("name");
  813. if (name != undefined && name.length > 0) {
  814. me.controls.push({name: name, control: $(this)});
  815. }
  816. }
  817. catch(e){
  818. return;
  819. }
  820. });
  821. $("input", jform).each(function ()
  822. {
  823. try{
  824. var name = $(this).attr("name");
  825. var control = null;
  826. var controlType = "html"; //html为html控件 ui为ligerUI控件
  827. var rule = null; //校验规则
  828. var message = null; //校验提示信息
  829. if(name!=undefined && name.length>0)
  830. {
  831. //是否已存在
  832. for(var i=0;i<me.controls.length;i++)
  833. {
  834. if(me.controls[i].name == name)
  835. {
  836. return;
  837. }
  838. }
  839. var type = $(this).attr("type");
  840. if(type == "radio")//单选
  841. {
  842. control = $('input[name="'+name+'"]').ligerRadio();
  843. controlType = "ui";
  844. }
  845. else if(type == "checkbox")//多选
  846. {
  847. control = $('input[name="'+name+'"]').ligerCheckBox();
  848. controlType = "ui";
  849. }
  850. else if(type=="hidden")
  851. {
  852. if($(this).attr("data-ligerid"))
  853. {
  854. return;
  855. }
  856. else{
  857. control = $(this);
  858. }
  859. }
  860. else{
  861. var wrap = $(this).parent();
  862. if(wrap.hasClass("l-text-combobox")) //下拉控件
  863. {
  864. control = $(this).ligerComboBox();
  865. controlType = "ui";
  866. }
  867. else if(wrap.hasClass("l-text-date"))
  868. {
  869. control = $(this).ligerDateEditor();
  870. controlType = "ui";
  871. }
  872. else{
  873. control = $(this);
  874. }
  875. }
  876. //相关校验规则
  877. if(option.validate!=null && option.validate[name]!=null)
  878. {
  879. rule = option.validate[name];
  880. if(option.message!=null && option.message[name]!=null)
  881. {
  882. message = option.message[name];
  883. }
  884. }
  885. me.controls.push({name:name,controlType:controlType,control:control,rule:rule,message:message});
  886. }
  887. }
  888. catch(e)
  889. {
  890. return;
  891. }
  892. });
  893. /********* 初始化数据 **********/
  894. if(option.data)
  895. {
  896. me.setData(option.data);
  897. }
  898. /********** 只读 ************/
  899. if(option.readonly)
  900. {
  901. me.readOnly();
  902. }
  903. },
  904. //初始化数据
  905. setData:function(data){
  906. try{
  907. var me = this, option = this.options;
  908. for(var i=0;i<me.controls.length;i++)
  909. {
  910. var controlName = me.controls[i].name;
  911. var control = me.controls[i].control;
  912. var controlType = me.controls[i].controlType;
  913. var controlData = data[controlName];
  914. if(controlData!=undefined)
  915. {
  916. if(controlType=="ui"&&typeof(control.setValue)=="function")
  917. {
  918. control.setValue(controlData);
  919. }
  920. else{
  921. if(typeof(control.setValue)=="function")
  922. {
  923. control.setValue(controlData);
  924. }
  925. if(typeof(control.val)=="function")
  926. {
  927. control.val(controlData);
  928. }
  929. else{
  930. control.html(controlData);
  931. }
  932. }
  933. }
  934. }
  935. }
  936. catch(e)
  937. {
  938. return;
  939. }
  940. },
  941. //校验规则
  942. validateRule:function(rule,ruleValue,value){
  943. try{
  944. var me = this;
  945. //字符串格式化方法
  946. String.prototype.format= function() {
  947. var args = arguments;
  948. return this.replace(/\{(\d+)\}/g, function (s, i) {
  949. return args[i];
  950. });
  951. };
  952. var message = me.defaultMessage[rule];
  953. if(rule=="required" && (ruleValue ==true ||ruleValue=="true"))
  954. {
  955. if(value == null || value.length ==0)
  956. {
  957. return message;
  958. }
  959. }
  960. else if(rule== "equalTo")
  961. {
  962. if(ruleValue=="") {
  963. return "密码未输入";
  964. }
  965. else if(ruleValue!=value && value!="") {
  966. return message;
  967. }
  968. }
  969. else{
  970. if(value !=null &&value.length>0)
  971. {
  972. switch (rule)
  973. {
  974. case "email":
  975. {
  976. var re =new RegExp("^([a-zA-Z0-9]+[_|\\_|\\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\\_|\\.]?)*[a-zA-Z0-9]+\\.[a-zA-Z]{2,3}$");
  977. if(!re.test(value))
  978. {
  979. return message;
  980. }
  981. break;
  982. }
  983. case "phone":
  984. {
  985. debugger
  986. var isPhone = /^(0[1-9]{2})-\d{8}$|^(0[1-9]{3}-(\d{7,8}))$/;
  987. var isMobile = /^(13|14|15|17|18)[0-9]{9}$/
  988. if (!isPhone.test(value) && !isMobile.test(value)) {
  989. return message;
  990. }
  991. break;
  992. }
  993. case "ip":
  994. {
  995. var re =new RegExp("^(2[5][0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})$");
  996. debugger
  997. if(!re.test(value))
  998. {
  999. return message;
  1000. }
  1001. break;
  1002. }
  1003. case "date":
  1004. {
  1005. break;
  1006. }
  1007. case "dateISO":
  1008. {
  1009. break;
  1010. }
  1011. case "number":
  1012. {
  1013. break;
  1014. }
  1015. case "digits":
  1016. {
  1017. var re =new RegExp("^[0-9]*$");
  1018. if(!re.test(value))
  1019. {
  1020. return message;
  1021. }
  1022. break;
  1023. }
  1024. case "extension"://请输入有效的后缀
  1025. {
  1026. break;
  1027. }
  1028. case "maxlength"://最多可以输入 {0} 个字符
  1029. {
  1030. if(value.length>ruleValue)
  1031. {
  1032. return message.format(ruleValue);
  1033. }
  1034. break;
  1035. }
  1036. case "minlength"://最少要输入 {0} 个字符
  1037. {
  1038. if(value.length<ruleValue)
  1039. {
  1040. return message.format(ruleValue);
  1041. }
  1042. break;
  1043. }
  1044. case "max"://请输入不大于 {0} 的数值
  1045. {
  1046. if(value>ruleValue)
  1047. {
  1048. return message.format(ruleValue);
  1049. }
  1050. break;
  1051. }
  1052. case "min"://请输入不小于 {0} 的数值
  1053. {
  1054. if(value<ruleValue)
  1055. {
  1056. return message.format(ruleValue);
  1057. }
  1058. break;
  1059. }
  1060. }
  1061. }
  1062. }
  1063. return "";
  1064. }
  1065. catch(e)
  1066. {
  1067. return e;
  1068. }
  1069. },
  1070. //校验值
  1071. validate:function(){
  1072. try {
  1073. var me = this, option = this.options;
  1074. me.flag = 100;//错误等级
  1075. var suc = true;
  1076. if (me.controls != null) {
  1077. var data = me.getData();
  1078. for (var i = 0; i < me.controls.length; i++) {
  1079. var control = me.controls[i];
  1080. var name = control["name"];
  1081. var rule = control["rule"];
  1082. var message = control["message"];
  1083. var error = "";
  1084. var terror = "";
  1085. if (rule != null) {
  1086. var value = data[name];
  1087. if (typeof(rule) == "string") {
  1088. error = me.validateRule(rule, true, value);
  1089. }
  1090. else {
  1091. for (var o in rule) {
  1092. var val = rule[o];
  1093. //判断相等情况,以赋值替代val
  1094. if (o == "equalTo") {
  1095. val = data[val];
  1096. }
  1097. terror = me.validateRule(o, val, value);
  1098. if (terror.length > 0) {
  1099. if (o == "required") {
  1100. me.flag = 1;
  1101. error = terror;
  1102. }
  1103. else if (o == "minlength") {
  1104. if (me.flag > 2) {
  1105. me.flag = 2;
  1106. error = terror;
  1107. }
  1108. }
  1109. else if (o == "maxlength") {
  1110. if (me.flag > 3) {
  1111. me.flag = 3;
  1112. error = terror;
  1113. }
  1114. }
  1115. //判断相等情况,以赋值替代val
  1116. else if (o == "equalTo") {
  1117. if (me.flag > 4) {
  1118. me.flag = 4;
  1119. error = terror;
  1120. }
  1121. }
  1122. else if (o == "digits") {
  1123. if (me.flag > 5) {
  1124. me.flag = 5;
  1125. error = terror;
  1126. }
  1127. }
  1128. else if (o == "email") {
  1129. if (me.flag > 6) {
  1130. me.flag = 6;
  1131. error = terror;
  1132. }
  1133. }
  1134. else if (o == "ip") {
  1135. if (me.flag > 7) {
  1136. me.flag = 7;
  1137. error = terror;
  1138. }
  1139. }
  1140. //error += ";"+re;
  1141. }
  1142. else {
  1143. }
  1144. }
  1145. }
  1146. }
  1147. //是否报错
  1148. if (error.length > 0) {
  1149. if(message!=undefined&&message!=null)
  1150. {
  1151. if (typeof(message) == "string")
  1152. {
  1153. error = message;//用户自定义消息覆盖默认消息
  1154. }
  1155. else {
  1156. for (var m in message) {
  1157. var value = message[m];
  1158. if (m == "required" && me.flag == 1) {
  1159. error = value;
  1160. }
  1161. else if (m == "minlength" && me.flag == 2) {
  1162. error = value;
  1163. }
  1164. else if (m == "maxlength" && me.flag == 3) {
  1165. error = value;
  1166. }
  1167. //判断相等情况,以赋值替代val
  1168. else if (m == "equalTo" && me.flag == 4) {
  1169. error = value;
  1170. }
  1171. else if (m == "digits" && me.flag == 5) {
  1172. error = value;
  1173. }
  1174. else if (m == "email" && me.flag == 6) {
  1175. error = value;
  1176. }
  1177. else if (m == "ip" && me.flag == 7) {
  1178. error = value;
  1179. }
  1180. }
  1181. }
  1182. }
  1183. me.showError(control, error);
  1184. /*if (me.flag == 1) {
  1185. me.showError(control, error);//required优先级高
  1186. }
  1187. else if (me.flag == 2) {
  1188. me.showError(control, error);//
  1189. }
  1190. else if (me.flag == 3) {
  1191. me.showError(control, error);//
  1192. }
  1193. else if (me.flag == 4) {
  1194. me.showError(control, error);//
  1195. }
  1196. else if (me.flag == 5) {
  1197. me.showError(control, error);//
  1198. }
  1199. else if (me.flag == 6) {
  1200. me.showError(control, error);//
  1201. }
  1202. else if (me.flag == 7) {
  1203. me.showError(control, error);//
  1204. }*/
  1205. //else if(message!=null && message.length > 0){
  1206. // me.showError(control,message);
  1207. // }
  1208. suc = false;
  1209. }
  1210. else
  1211. {
  1212. me.clearError(control);
  1213. }
  1214. }
  1215. return suc;
  1216. }
  1217. }
  1218. catch(e)
  1219. {
  1220. return false;
  1221. }
  1222. },
  1223. //清除某个控件错误提示
  1224. clearError:function(control){
  1225. var me = this;
  1226. var dom;
  1227. if(control.controlType == "ui")
  1228. {
  1229. dom = $(control.control.element).closest(".m-form-control");
  1230. }
  1231. else{
  1232. dom = control.control.closest(".m-form-control");
  1233. }
  1234. dom.removeClass('m-from-error').find('.m-from-error-msg').remove();
  1235. },
  1236. //某个控件错误提示
  1237. showError:function(control,error){
  1238. var me = this;
  1239. var dom;
  1240. if(control.controlType == "ui")
  1241. {
  1242. dom = $(control.control.element).closest(".m-form-control");
  1243. }
  1244. else{
  1245. dom = control.control.closest(".m-form-control");
  1246. }
  1247. if(dom.hasClass('m-from-error'))
  1248. {
  1249. me.clearError(control);
  1250. }
  1251. var el = $('<div class="m-from-error-msg"></div>');
  1252. dom.addClass('m-from-error').append(el);
  1253. el.hover(function ()
  1254. {
  1255. $(this).ligerTip({content:error});
  1256. },function (){
  1257. $(this).ligerHideTip();
  1258. });
  1259. },
  1260. //只读操作
  1261. readOnly:function(){
  1262. },
  1263. //获取数据
  1264. getData:function(){
  1265. var me = this, option = this.options;
  1266. var data = {};
  1267. for(var i=0;i<me.controls.length;i++)
  1268. {
  1269. var controlName = me.controls[i].name;
  1270. var control = me.controls[i].control;
  1271. var controlType = me.controls[i].controlType;
  1272. if(controlType=="ui"&&typeof(control.getValue)=="function")
  1273. {
  1274. data[controlName] = control.getValue();
  1275. }
  1276. else{
  1277. if(typeof(control.getValue)=="function")
  1278. {
  1279. data[controlName] = control.getValue();
  1280. }
  1281. if(typeof(control.val)=="function")
  1282. {
  1283. data[controlName] = control.val();
  1284. }
  1285. else{
  1286. data[controlName] = control.html();
  1287. }
  1288. }
  1289. }
  1290. return data;
  1291. }
  1292. });
  1293. })(jQuery, window);