queryForm.js 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514
  1. const query = function getQueryForm(data) {
  2. {
  3. this.el = data.el || '#form_div';
  4. this.form = data.form;
  5. this.method = data.method || 'get';
  6. this.url = data.url || getPathname();
  7. this.condition = data.condition;
  8. this.paginations = data.paginations ? data.paginations : [50, 100, 200, 500, 1000];
  9. this.isPaginations = data.isPaginations !== false;
  10. this.keydownfun = data.keydownfun || undefined;
  11. this.selectChange = data.selectChange || undefined;
  12. this.searchClick = data.searchClick || undefined;
  13. this.selectfun = data.selectfun || undefined;
  14. this._onsubmit = data._onsubmit || undefined;
  15. this.keydownSearch = data.keydownSearch || true;
  16. this.selectChangeSearch = data.selectChangeSearch || true;
  17. this.tip = data.tip || '';
  18. this.nextPage = nextPage;
  19. this.pervPage = pervPage;
  20. this.goPage = goPage;
  21. this.getSearchData = getSearchData;
  22. this.autoSubmit = (data.autoSubmit !== false);
  23. this.param = data.param || [];
  24. this.appendDom = data.appendDom || undefined;
  25. // 提交表单
  26. this.onsubmit = function () {
  27. if (!this.autoSubmit) {
  28. return;
  29. }
  30. saveSearchOnCookie();
  31. let form = $("<form method='" + _this.method + "'></form>");
  32. for (const key in _data) {
  33. let map = _data[key];
  34. if (map['required'] == true && (map.value === '' || map.value === null || map.value === undefined)) return;
  35. if (["string", "number"].includes(fetchJsType(map.value)) && map.value !== '') {
  36. // if(map.value){
  37. // let string = new String(map.value);
  38. // if(string.length > 1 ){
  39. // string = string.replace(/\+/g,',');
  40. // map.value = string
  41. // }
  42. // }
  43. //form.append("<input name='" + key + "' value='" + map.value.replace(" ","%20") + "'>");
  44. form.append("<input name='" + key + "' value='" + encodeURIComponent(map.value) + "'>")
  45. } else if ('array' === fetchJsType(map.value)) {
  46. let string = map.value.join(',');
  47. //form.append("<input name='" + key + "' value='" + string.replace(" ","%20") + "'>");
  48. form.append("<input name='" + key + "' value='" + encodeURIComponent(string) + "'>")
  49. }
  50. }
  51. for (let key in this.param) {
  52. form.append("<input type='text' name='" + key + "' value='" + this.param[key] + "'>");
  53. }
  54. if (_this.method !== 'get') {
  55. form.append("<input type='hidden' name='_token' value='" + $('meta[name="csrf-token"]').attr('content') + "'>");
  56. form.append("<input type='hidden' name='_method' value='" + _this.method + "'>");
  57. form.attr('action', _this.url);
  58. }
  59. _hidden_div.html('');
  60. _hidden_div.append(form);
  61. form.submit();
  62. }
  63. }
  64. let _this = this;
  65. let _parentNode = null;
  66. let _data = {};
  67. let _form = [];
  68. let _table = null;
  69. let _clearTr = null;
  70. let _pagination = null;
  71. let _baseData = data;
  72. let _searchBtn = null;
  73. let _hidden_div = null;
  74. let _parentNode_top = null;
  75. let _page = '';
  76. let _toggle_btn = null;
  77. this.init = function () {
  78. _parentNode = $(_this.el);
  79. _form = $("<div style='z-index:95'></div>");
  80. _table = $("<table class='table table-sm table-bordered m-0 position-static' style='background: rgb(255, 255, 255);style='z-index:95''></table>");
  81. fillTable();
  82. switchData();
  83. addHiedDiv();
  84. rendererSearchForm();
  85. visibleClearBtn();
  86. fixed();
  87. inputKeyDown();
  88. selectChange();
  89. createTip();
  90. fixed();
  91. $('[data-toggle="tooltip"]').tooltip({'trigger': 'hover'})
  92. }
  93. function isSmallScreen() {
  94. return $(window).width() <= 640;
  95. }
  96. function getToggleBtn(){
  97. _toggle_btn = $("<button type='button' class='btn btn-sm btn-success position-fixed' style='left:-5px;top:290px;z-index:10;width: 28px;'>展开筛选</button>")
  98. _toggle_btn.click(function(){
  99. if(_toggle_btn.text() === "展开筛选"){
  100. _toggle_btn.text("收缩");
  101. _table.removeClass("d-none")
  102. _table.css("z-index",9);
  103. }else if(_toggle_btn.text() === "收缩"){
  104. _toggle_btn.text("展开筛选");
  105. _table.addClass("d-none")
  106. }
  107. });
  108. return _toggle_btn;
  109. }
  110. function fixed(){
  111. _parentNode.addClass('header-float');
  112. let dom = _this.appendDom ? $("#" + _this.appendDom + "") : null;
  113. if (dom){
  114. dom.css('top', (_parentNode.height())+'px');
  115. dom.css('background', 'rgb(255, 255, 255)');
  116. dom.css('position', 'sticky');
  117. dom.css('z-index', 60);
  118. }
  119. _parentNode.css('top',0);
  120. _parentNode.css('z-index',95);
  121. }
  122. // form fixed
  123. function parentNodeFloat() {
  124. _parentNode_top = _parentNode.offset().top;
  125. let height = _parentNode.height();
  126. let dom = _this.appendDom ? $("#" + _this.appendDom + "") : null;
  127. // $(window).scroll(function () {
  128. // let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
  129. // if(isSmallScreen()){
  130. // return;
  131. // }
  132. // if (scrollTop > _parentNode_top) {
  133. // _form.addClass('fixed-top');
  134. // if (dom) {
  135. // dom.css('position', 'fixed');
  136. // dom.css('background', 'rgb(255, 255, 255)');
  137. // dom.css('top', height);
  138. // dom.css('z-index', 1000);
  139. // }
  140. // } else {
  141. // _form.removeClass('fixed-top');
  142. // if (dom) {
  143. // dom.attr('style', '');
  144. // }
  145. // }
  146. // });
  147. }
  148. // add hide div (on submit form )
  149. function addHiedDiv() {
  150. _parentNode.append(_form.append(_table));
  151. _hidden_div = $('<div></div>');
  152. _hidden_div.hide();
  153. $("body").append(_hidden_div);
  154. }
  155. // fill table
  156. function fillTable() {
  157. _table.html('');
  158. _table.append(crateClearBtn());
  159. _table.append(createPagination());
  160. let isSmall = isSmallScreen();
  161. _baseData.condition.forEach(function (conditions, index, arrays) {
  162. let tr = $("<tr></tr>");
  163. conditions.forEach(function (condition, index, array) {
  164. if (isSmall) tr = $("<tr></tr>");
  165. let td = $("<td style='width: 260px'></td>");
  166. if ([null, '', undefined].includes(condition.type) && ![null, undefined].includes(condition.name)) {
  167. condition.type = 'input';
  168. }
  169. if (['dateTime_dateTime', 'dateTimeLocal', 'input_input'].includes(condition.type)) {
  170. td = $("<td style='width: 330px'</td>")
  171. } else if (['select_multiple_select', 'time', 'dateTime_select'].includes(condition.type)) {
  172. td = $("<td style='width: 280px'</td>");
  173. } else if (['time', 'dateTime_select'].includes(condition.type)) {
  174. td = $("<td style='width: 320px'</td>");
  175. }
  176. td.append(createModule(condition));
  177. tr.append(td);
  178. if (isSmall) {
  179. _table.append(tr);
  180. }
  181. })
  182. if (!isSmall) {
  183. _table.append(tr);
  184. if (arrays.length === index + 1) {
  185. let td = $("<td ></td>");
  186. tr.append(td.append(createSearchBt()));
  187. }
  188. }
  189. })
  190. if (isSmall) {
  191. let tr = $("<tr></tr>");
  192. let td = $("<td style='width: 260px'></td>");
  193. _table.append(tr.append(td.append(createSearchBt())));
  194. _table.addClass("d-none").addClass("position-fixed").css({"z-index":"9","top":0});
  195. _form.append(getToggleBtn());
  196. }
  197. $.cookie.raw = true;
  198. $.cookie.json = true;
  199. setTableWidth();
  200. }
  201. function setTableWidth() {
  202. let max_width = 0;
  203. _baseData.condition.forEach(function (conditions, xindex, xarrays) {
  204. let _width = 0;
  205. conditions.forEach(function (condition, index, array) {
  206. if (['input', 'dateMonth', 'select', 'dateTime', 'input_select'].includes(condition.type)) {
  207. _width += 260;
  208. } else if (['dateTime_dateTime', 'dateTimeLocal', 'input_input'].includes(condition.type)) {
  209. _width += 330;
  210. } else if (['select_multiple_select', 'search_select'].includes(condition.type)) {
  211. _width += 280;
  212. } else if (['checkbox'].includes(condition.type)) {
  213. _width += 260;
  214. } else if (['time', 'dateTime_select'].includes(condition.type)) {
  215. _width += 320;
  216. } else if ([undefined, null, ''].includes(condition)) {
  217. _width += 260;
  218. }
  219. if (index === array.length - 1 && xindex === xarrays.length - 1) {
  220. _width += 260;
  221. }
  222. if (max_width < _width) {
  223. max_width = _width;
  224. }
  225. })
  226. })
  227. if (isSmallScreen()) {
  228. _parentNode.css("width", 340 + "px");
  229. _table.css("width", 340 + "px").addClass("m-0");
  230. } else {
  231. _parentNode.css('min-width', 1920 - 30)
  232. _parentNode.css('max-width', (max_width || window.screen.availWidth) - 30);
  233. _table.css('width', max_width + 'px');
  234. }
  235. }
  236. // 清空筛选按钮
  237. function crateClearBtn() {
  238. let clearbtn = $("<button type='button' class='btn btn-warning text-dark '>清除筛选</button>");
  239. clearbtn.click(function () {
  240. clearData();
  241. visibleClearBtn();
  242. })
  243. let tr = $("<tr ><td colspan='10'></td></tr>");
  244. tr.find('td').append(clearbtn);
  245. _clearTr = tr;
  246. return tr;
  247. }
  248. // create SearchBtn
  249. function createSearchBt() {
  250. _searchBtn = $("<span class='btn btn-sm btn-outline-dark ml-1' >按条件搜索</span>");
  251. let div = $("<div class='form-inline'></div>");
  252. div.append(_searchBtn);
  253. _searchBtn.click(function () {
  254. if (controlJsType(_this.searchClick, 'fun')) {
  255. _this.searchClick();
  256. }
  257. if (controlJsType(_this._onsubmit, 'fun')) {
  258. _this._onsubmit();
  259. } else {
  260. _this.onsubmit();
  261. }
  262. })
  263. return div;
  264. }
  265. // create _pagination
  266. function createPagination() {
  267. let isPaginations = _this.isPaginations ? '' : 'hidden';
  268. _pagination = $("<select id='paginate' " + isPaginations + " name='paginate' class='form-control form-control-sm' style='vertical-align: middle; max-width: 150px;'></select>");
  269. let tr = $("<tr ><td colspan='10'><div class='col p-0' style='height: 100%'></div></td></tr>");
  270. tr.find('div').append(_pagination);
  271. _this.paginations.forEach(function (map) {
  272. let option = $("<option value='" + map + "'>每页显示" + map + "</option>");
  273. _pagination.append(option);
  274. })
  275. _pagination.change(function () {
  276. let dom = {name: 'paginate', type: 'select', value: _pagination.val(), mold: 'select'};
  277. _data['paginate'] = dom;
  278. modifyData(dom);
  279. _data['paginate'] = dom;
  280. _this.onsubmit();
  281. })
  282. return tr;
  283. }
  284. // create Tip
  285. function createTip() {
  286. let data = fetchCookie();
  287. let search = window.location.search;
  288. if (!data || !search) {
  289. return;
  290. }
  291. let cookies = JSON.parse(data);
  292. if (!cookies['success_tip']) {
  293. return;
  294. }
  295. if (!cookies['success_tip'] && !!_this.tip) {
  296. cookies['success_tip'].value = _this.tip;
  297. cookies['success_tip'].count = 1;
  298. _data['page'].value = _page;
  299. saveCookie(cookies);
  300. set_dataOnCookie();
  301. _this.onsubmit();
  302. // 重定向
  303. } else if (!cookies['success_tip'] && cookies['success_tip'].count === 1) {
  304. let tip = $("<div class='alert alert-success h1'></div>")
  305. let tipdiv = $("<div style='max-width: " + window.screen.availWidth + "px'>" + cookies['success_tip'].value + "</div>").append(tip);
  306. _form.append(tipdiv);
  307. cookies['success_tip'] = '';
  308. saveCookie(cookies);
  309. }
  310. }
  311. // 数据清空
  312. function clearData() {
  313. saveCookie({});
  314. let url = _this.url;
  315. if (_this.param !== []) {
  316. url += "?";
  317. let i = 0;
  318. for (let key in _this.param) {
  319. if (i !== 0) url += "&";
  320. url += key + "=" + _this.param[key];
  321. i++;
  322. }
  323. }
  324. window.location.href = url;
  325. }
  326. // 存cookie
  327. function saveCookie(data) {
  328. $.cookie(getPathname(), data, {expires: 1});
  329. }
  330. // 取cookie
  331. function fetchCookie() {
  332. return $.cookie(getPathname());
  333. }
  334. // 获取路径
  335. function getPathname() {
  336. return window.location.pathname;
  337. }
  338. // 获取js对象类型types.call
  339. function fetchJsType(JsObj) {
  340. let types = Object.prototype.toString;
  341. let obj = types.call(JsObj);
  342. return (obj === "[object Number]" && 'number')
  343. || (obj === "[object Boolean]" && 'boolean')
  344. || (obj === "[object String]" && 'string')
  345. || (obj === "[object Array]" && 'array')
  346. || (obj === "[object Function]" && 'fun')
  347. || (obj === "[object Object]" && 'object')
  348. || (obj === "[object Null]" && 'null')
  349. || (obj === "[object Undefined]" && 'undefined');
  350. }
  351. // 判断js类型
  352. function controlJsType(JsObj, type) {
  353. if (fetchJsType(type) === 'array') {
  354. return type.includes(fetchJsType(JsObj));
  355. } else if (fetchJsType(type) === 'string') {
  356. return fetchJsType(JsObj) === type;
  357. } else {
  358. return JsObj === type;
  359. }
  360. }
  361. // create Module
  362. function createModule(condition) {
  363. let type = condition.type;
  364. return (['input', '', null].includes(type) && getInput(condition))
  365. || (type === "select" && getSelect(condition))
  366. || (type === "search_select" && getSearchSelect(condition))
  367. || (type === "input_select" && getInputSelect(condition))
  368. || (type === "dateTime" && getdateTime(condition))
  369. || (type === "dateTimeLocal" && getDateTimeLocal(condition))
  370. || (type === "dateMonth" && getDateMonth(condition))
  371. || (type === "time" && getTime(condition))
  372. || (type === "input_input" && getInputInput(condition))
  373. || (type === "dateTime_dateTime" && getdateTimedateTime(condition))
  374. || (type === "dateTime_select" && getdateTimeSelect(condition))
  375. || (type === "select_multiple_select" && getSelectMultipleSelect(condition))
  376. || (type === "checkbox" && getCheckBox(condition));
  377. }
  378. // dateMonth
  379. function getDateMonth(condition) {
  380. let dateMonth = $("<input name = '" + condition.name + "' type='month' class='form-control form-control-sm' style='vertical-align: middle; max-width: 200px;' placeholder='" + condition.placeholder + "' autocomplete='off' data-toggle='tooltip' >");
  381. dateMonth.attr('title', condition.tip === undefined ? '' : condition.tip);
  382. dateMonth.blur(function () {
  383. modifyData({name: condition.name, type: 'dateMonth', value: this.value, mold: 'dateMonth'});
  384. });
  385. dateMonth.keypress(function (event) {
  386. if (event.which === 13) {
  387. modifyData({name: condition.name, type: 'input', value: this.value, mold: 'input'});
  388. }
  389. });
  390. return dateMonth;
  391. }
  392. // input
  393. function getInput(condition) {
  394. let input = $("<input name='" + condition.name + "' class='form-control form-control-sm' style='vertical-align: middle; max-width: 200px;' placeholder='" + condition.placeholder + "' autocomplete='off' data-toggle='tooltip' data-placement='top' >");
  395. input.attr('title', condition.tip === undefined ? '' : condition.tip);
  396. input.blur(function () {
  397. modifyData({name: condition.name, type: 'input', value: this.value, mold: 'input'});
  398. });
  399. input.keypress(function (event) {
  400. if (event.which === 13) modifyData({name: condition.name, type: 'input', value: this.value, mold: 'input'});
  401. });
  402. return input;
  403. }
  404. // select
  405. function getSelect(condition) {
  406. let select = $("<select name='" + condition.name + "' class='form-control form-control-sm' style='max-width: 200px;' data-toggle='tooltip' data-placement='top'></select>")
  407. select.attr('title', condition.tip === undefined ? '' : condition.tip);
  408. let tip = $("<option value class='text-secondary'>" + condition.placeholder + "</option>")
  409. select.append(tip);
  410. condition.data.forEach(function (map) {
  411. let option = $("<option value='" + map.name + "' class='text-secondary'>" + map.value + "</option>")
  412. select.append(option);
  413. })
  414. select.on('change', function () {
  415. let dom = {name: condition.name, type: 'select', value: this.value, mold: 'select'}
  416. modifyData(dom)
  417. })
  418. return select;
  419. }
  420. // search_select
  421. function getSearchSelect(condition) {
  422. let div = $("<div class='form-inline'></div>");
  423. let input = $("<input class='form-control form-control-sm' placeholder='" + condition.placeholder[0] + "' style='width: 70px;' autocomplete='off' data-toggle='tooltip' data-placement='top'>");
  424. input.attr('title', controlJsType(condition.tip[0], 'undefined') ? '输入关键词快速定位下拉列表,回车确定' : condition.tip[0]);
  425. let select = $("<select class='form-control form-control-sm ml-2' name='" + condition.name + "' style='min-width: 120px;max-width: 150px;' data-toggle='tooltip' data-placement='top' data-toggle='tooltip' data-placement='top'></select>");
  426. select.attr('title', controlJsType(condition.tip[1], 'undefined') ? '' : condition.tip[1]);
  427. let placeholder = '';
  428. if (controlJsType(condition.placeholder, 'array') && !['', undefined, null].includes(condition.placeholder[1])) {
  429. placeholder = condition.placeholder[1];
  430. }
  431. fillSelectOption(select, condition.data, placeholder);
  432. input.blur(function () {
  433. let value = this.value;
  434. let data = condition.data.filter(function (map) {
  435. return map.value.includes(value);
  436. });
  437. fillSelectOption(select, data || condition.data);
  438. select.val(_data[condition.name].select);
  439. });
  440. input.keypress(function (event) {
  441. if (event.which === 13) {
  442. let value = this.value;
  443. let data = condition.data.filter(function (map) {
  444. return map.value.includes(value);
  445. })
  446. fillSelectOption(select, data || condition.data);
  447. select.val(_data[condition.name].select);
  448. }
  449. });
  450. select.change(function () {
  451. modifyData({name: condition.name, type: 'input_select', value: this.value, mold: 'select'});
  452. })
  453. return div.append(input).append(select);
  454. }
  455. // input_select
  456. function getInputSelect(condition) {
  457. let div = $("<div class='form-inline'></div>");
  458. let input = $("<input class='form-control form-control-sm' name='" + condition.name[0] + "' placeholder='" + condition.placeholder[0] + "' autocomplete='off' style='max-width: 120px;' data-toggle='tooltip' data-placement='top' >");
  459. input.attr('title', controlJsType(condition.tip[0], 'undefined') ? '' : condition.tip[0]);
  460. let select = $("<select class='form-control form-control-sm ml-2' name='" + condition.name[1] + "' style='max-width: 200px;' title='" + condition.tip[1] + "''>");
  461. fillSelectOption(select, condition.data);
  462. input.blur(function () {
  463. modifyData({name: this.name, type: 'input_select_longtext', value: this.value, mold: 'input'});
  464. });
  465. input.keypress(function (event) {
  466. if (event.which === 13) modifyData({
  467. name: this.name,
  468. type: 'input_select_longtext',
  469. value: this.value,
  470. mold: 'input'
  471. });
  472. });
  473. select.change(function () {
  474. modifyData({name: this.name, type: 'input_select_longtext', value: this.value, mold: 'select'});
  475. })
  476. return div.append(input).append(select);
  477. }
  478. // dataTime
  479. function getdateTime(condition) {
  480. let dateTime = $("<input type='date' class='form-control form-control-sm' name='" + condition.name + "' style='max-width: 150px' data-toggle='tooltip' data-placement='top' title='" + condition.tip + "' style='width: 200px'>");
  481. dateTime.attr('title', controlJsType(condition.tip, 'undefined') ? '' : condition.tip);
  482. dateTime.bind('input propertychange', function () {
  483. modifyData({name: condition.name, type: 'dateTime', value: this.value, mold: 'dateTime'});
  484. });
  485. return dateTime;
  486. }
  487. //dateTimeLocal
  488. function getDateTimeLocal(condition) {
  489. let dateTimeLocal = $("<input name = '" + condition.name + "' type='datetime-local' step='01' class='form-control form-control-sm' style='vertical-align: middle; max-width: 280px;' placeholder='" + condition.placeholder + "' autocomplete='off' data-toggle='tooltip' data-placement='top' >");
  490. dateTimeLocal.attr('title', condition.tip === undefined ? '' : condition.tip);
  491. dateTimeLocal.bind('input propertychange', function () {
  492. modifyData({name: condition.name, type: 'dateTimeLocal', value: this.value, mold: 'dateTimeLocal'});
  493. });
  494. return dateTimeLocal;
  495. }
  496. // time
  497. function getTime(condition) {
  498. let div = $("<div class='form-inline'></div>");
  499. let dateTime = $("<input type='date' class='form-control form-control-sm' name='" + condition.name + "' style='width:150px' data-toggle='tooltip' data-placement='top' >");
  500. dateTime.attr('title', controlJsType(condition.tip[0], 'undefined') ? '' : condition.tip[0]);
  501. let min = $("<input id='" + condition.name + "_min' class='form-control form-control-sm ml-2' style='max-width: 100px;' placeholder='00:00' data-toggle='tooltip' data-placement='top' >");
  502. min.attr('title', controlJsType(condition.tip[1], 'undefined') ? '' : condition.tip[1]);
  503. dateTime.bind('input propertychange', function () {
  504. let hm = '00:00';
  505. if(!condition.name.includes('start'))hm = '23:59';
  506. let value = this.value !== '' ? this.value + ' ' + (!!min.val() ? min.val() : hm) : '';
  507. modifyData({name: condition.name, type: 'time', value: value, mold: 'time'});
  508. // modifyData({name: condition.name, type: 'dateTime', value: this.value, mold: 'dateTime'});
  509. });
  510. min.bind('input propertychange', function () {
  511. if (['null', 'undefined', ''].includes(dateTime.val())) return;
  512. let value = ['null', 'undefined', ''].includes(dateTime.val()) ? '' : dateTime.val() + ' ' + min.val();
  513. modifyData({name: condition.name, type: 'time', value: value, mold: 'time'});
  514. })
  515. return div.append(dateTime).append(min);
  516. }
  517. // input_input
  518. function getInputInput(condition) {
  519. let div = $("<div class='form-inline'></div>");
  520. let input1 = $("<input name='" + condition.name[0] + "' class='form-control form-control-sm' style='vertical-align: middle; max-width: 150px;' placeholder='" + condition.placeholder[0] + "' data-toggle='tooltip' data-placement='top' title='" + condition.tip[0] + "'>");
  521. input1.attr('title', controlJsType(condition.tip[0], 'undefined') ? '' : condition.tip[0]);
  522. let input2 = $("<input name='" + condition.name[1] + "' class='form-control form-control-sm ml-2' style='vertical-align: middle; max-width: 150px;' placeholder='" + condition.placeholder[1] + "' data-toggle='tooltip' data-placement='top' title='" + condition.tip[1] + "'>");
  523. input2.attr('title', controlJsType(condition.tip[1], 'undefined') ? '' : condition.tip[1]);
  524. input1.blur(function () {
  525. modifyData({name: this.name, type: 'input_input', value: this.value, mold: 'input'});
  526. });
  527. input1.keypress(function (event) {
  528. if (event.which === 13) modifyData({
  529. name: this.name,
  530. type: 'input_input',
  531. value: this.value,
  532. mold: 'input'
  533. });
  534. });
  535. input2.blur(function () {
  536. modifyData({name: this.name, type: 'input_input', value: this.value, mold: 'input'});
  537. });
  538. input2.keypress(function (event) {
  539. if (event.which === 13) modifyData({
  540. name: this.name,
  541. type: 'input_input',
  542. value: this.value,
  543. mold: 'input'
  544. });
  545. });
  546. return div.append(input1).append(input2);
  547. }
  548. // dateTime_dateTime
  549. function getdateTimedateTime(condition) {
  550. let div = $("<div class='form-inline'></div>");
  551. let dateTime1 = $("<input type='date' class='form-control form-control-sm' name='" + condition.name[0] + "' style='width: 150px' data-toggle='tooltip' data-placement='top' >");
  552. dateTime1.attr('title', controlJsType(condition.tip[0], 'undefined') ? '' : condition.tip[0]);
  553. let dateTime2 = $("<input type='date' class='form-control form-control-sm ml-2' name='" + condition.name[1] + "' style='width: 150px;' data-toggle='tooltip' data-placement='top' >");
  554. dateTime2.attr('title', controlJsType(condition.tip[1], 'undefined') ? '' : condition.tip[1]);
  555. dateTime1.bind('input propertychange', function () {
  556. let dom = {name: this.name, type: 'dateTime_dateTime', value: this.value, mold: 'dateTime'};
  557. modifyData(dom);
  558. })
  559. dateTime2.bind('input propertychange', function () {
  560. let dom = {name: this.name, type: 'dateTime_dateTime', value: this.value, mold: 'dateTime'};
  561. modifyData(dom);
  562. })
  563. return div.append(dateTime1).append(dateTime2);
  564. }
  565. // dateTime_select
  566. function getdateTimeSelect(condition) {
  567. let div = $("<div class='form-inline'></div>");
  568. let dateTime = $("<input type='date' class='form-control form-control-sm' name='" + condition.name[0] + "' style='max-width: 150px;' data-toggle='tooltip' data-placement='top' >");
  569. dateTime.attr('title', controlJsType(condition.tip[0], 'undefined') ? '' : condition.tip[0]);
  570. let select = $("<select name='" + condition.name[1] + "' class='form-control form-control-sm ml-2' data-toggle='tooltip' data-placement='top' style='max-width: 100px'></select>");
  571. select.attr('title', controlJsType(condition.tip[1], 'undefined') ? '' : condition.tip[1]);
  572. fillSelectOption(select, condition.data)
  573. dateTime.bind('input propertychange', function () {
  574. modifyData({name: this.name, type: 'dateTime_select', value: this.value, mold: 'dateTime'});
  575. })
  576. select.change(function () {
  577. modifyData({name: this.name, type: 'dateTime_select', value: this.value, mold: 'select'});
  578. })
  579. return div.append(dateTime).append(select);
  580. }
  581. // select_multiple_select
  582. function getSelectMultipleSelect(condition) {
  583. let div = $("<div class='form-inline' style='position: relative;z-index:95'></div>");
  584. let select = $("<select class='form-control form-control-sm' name='" + condition.name + "_sel' id='" + condition.name + "_sel' style='vertical-align: middle; width: 100px;z-index=95' data-toggle='tooltip' data-placement='top' ></select>");
  585. let label = $("<label id='" + condition.name + "_lab' style='vertical-align: middle; width: 100px' >(多选)</label>").hide();
  586. let select_div = $("<div style='z-index:95'></div>");
  587. select.attr('title', controlJsType(condition.tip[0], 'undefined') ? '' : condition.tip[0]);
  588. let placeholder = controlJsType(condition.placeholder[0], 'undefined') ? '' : condition.placeholder[0];
  589. fillSelectOption(select, condition.data, placeholder);
  590. let input = $("<input name='" + condition.name + "_tip' class='form-control form-control-sm ml-2' style='max-width: 150px' data-toggle='tooltip' data-placement='top' autocomplete='off'>");
  591. input.attr('title', controlJsType(condition.tip[1], 'undefined') ? '' : condition.tip[1]);
  592. input.attr('placeholder', controlJsType(condition.placeholder[1], 'undefined') ? '' : condition.placeholder[1])
  593. let ul_div = $("<div class='pl-0 tooltipTarget' style='position: absolute;left: 100px;top:25px; max-height: 150px; overflow-y: scroll; border: 1px solid rgb(221, 221, 221); border-radius: 5px; text-align: center; z-index:95' ></div>");
  594. let ul = $("<ul name='" + condition.name + "' class='list-group pl-0 m-0 p-0 bg-white' style='list-style-type: none;width: 150px;top:35px; z-index: 95' ></ul>");
  595. let check = $("<input id='multipleCheckBox' type = 'checkbox' style='z-index:101;position:fixed;top:0;right: 0;border:1px green' >");
  596. let isMouseenterCheckBox = false;
  597. select_div.append(input).append(ul_div.append(ul).append(check));
  598. div.append(select).append(label).append(select_div);
  599. fillMultipleSelectOption(ul, condition.data, condition.name);
  600. check.click(function () {
  601. _data[condition.name].value = [];
  602. _data[condition.name].select = [];
  603. if (check[0].checked === true) {
  604. condition.data.forEach(function (data) {
  605. if (data.value.indexOf(input.val())!==-1){
  606. _data[condition.name].value.push(data.name);
  607. if (_data[condition.name].select) _data[condition.name].select.push(data.name);
  608. else _data[condition.name].select = [data.name];
  609. }
  610. });
  611. } else {
  612. let dom = {
  613. name: condition.name,
  614. mold: 'select_multiple_select',
  615. value: [],
  616. select: [],
  617. };
  618. modifyData(dom);
  619. }
  620. redenerUl(ul);
  621. });
  622. check.mouseleave(() => {
  623. isMouseenterCheckBox = false;
  624. });
  625. check.mouseenter(() => {
  626. isMouseenterCheckBox = true;
  627. });
  628. input.bind('input propertychange', function () {
  629. let value = this.value;
  630. if (value === '') {
  631. fillMultipleSelectOption(ul, _data[condition.name].data, condition.name);
  632. redenerUl(ul);
  633. return;
  634. }
  635. let select = _data[condition.name].select;
  636. if ([undefined, null, ''].includes(select)) {
  637. select = [];
  638. }
  639. let select_data = condition.data.filter(function (map) {
  640. return map.value.includes(value) || select.includes(map.name);
  641. })
  642. fillMultipleSelectOption(ul, select_data, condition.name);
  643. redenerUl(ul);
  644. });
  645. select.change(function () {
  646. let dom = {
  647. name: condition.name,
  648. mold: 'select_multiple_select',
  649. value: [this.value]
  650. }
  651. modifyData(dom);
  652. redenerUl(ul);
  653. })
  654. input.focus(function () {
  655. ul_div.show();
  656. })
  657. input.blur(function () {
  658. setTimeout(function () {
  659. if (!ul_div.is(':focus') && !check.is(':focus')) {
  660. ul_div.hide();
  661. }
  662. }, 100);
  663. })
  664. select_div.focus(function () {
  665. ul_div.show();
  666. })
  667. select_div.blur(function () {
  668. ul_div.hide();
  669. })
  670. /*ul_div.blur(function () {
  671. if (!isMouseenterCheckBox){
  672. ul_div.hide();
  673. }
  674. })*/
  675. ul_div.hide();
  676. ul_div.mouseleave(function () {
  677. if (_data[condition.name].value.length > 0) {
  678. // _this.onsubmit();
  679. ul_div.hide();
  680. return;
  681. }
  682. if (!isMouseenterCheckBox) {
  683. ul_div.hide();
  684. }
  685. });
  686. // ul_div.mouseenter(function () {
  687. // if(_data[condition.name].value.length>0){
  688. // setTimeout(()=> {
  689. // if (ul_div.is(':visible')){
  690. // ul_div.attr("title","移出多选区域即可自动提交");
  691. // ul_div.tooltip('show');
  692. // }
  693. // },1000);
  694. // }
  695. // });
  696. return div;
  697. }
  698. // checkbox
  699. function getCheckBox(condition) {
  700. let div1 = $("<div class='form-inline'></div>")
  701. condition.data.forEach(function (map, index) {
  702. let div = $('<div class="custom-control custom-control-inline custom-checkbox"></div>');
  703. let checkbox = $("<input type='checkbox' name='" + condition.name + "' class='custom-control-input' id='" + condition.name + index + "' value='" + map.name + "' >");
  704. let label = $("<label class='custom-control-label text-left font-weight-norma' for='" + condition.name + index + "'>" + map.value + "</label>")
  705. div1.append(div.append(checkbox).append(label));
  706. checkbox.click(function () {
  707. let value = [];
  708. $.each($("input:checkbox[name='" + condition.name + "']:checked"), function () {
  709. value.push($(this).val());
  710. })
  711. let dom = {name: condition.name, type: 'checkbox', value: value, mold: 'check'};
  712. modifyData(dom);
  713. _this.onsubmit();
  714. });
  715. })
  716. return div1;
  717. }
  718. // fill select
  719. function fillSelectOption(select, data, placeholder = '') {
  720. select.html('');
  721. if (![undefined, null].includes(placeholder)) {
  722. let option = $("<option value></option>");
  723. if ('' === placeholder) {
  724. option = $("<option value>" + '全部' + placeholder + "</option>");
  725. } else {
  726. option = $("<option value>" + placeholder + "</option>");
  727. }
  728. select.append(option);
  729. }
  730. data.forEach(function (map) {
  731. let option = $("<option value='" + map.name + "'>" + map.value + "</optio>");
  732. select.append(option);
  733. })
  734. if (data !== undefined) {
  735. }
  736. }
  737. // fill multiple select ul
  738. function fillMultipleSelectOption(ul, data, name) {
  739. ul.html('');
  740. data.forEach(function (map) {
  741. let span = $("<span style='cursor:default'>" + map.value + "</span>");
  742. let li = $("<li class='list-group-item pt-0 pb-0' value='" + map.name + "'></li>");
  743. li.append(span);
  744. ul.append(li);
  745. li.click(function () {
  746. let value = li.attr('value');
  747. let dom_data = _data[name].value;
  748. if (!dom_data) {
  749. dom_data = [];
  750. }
  751. if (controlJsType(dom_data, 'string')) {
  752. dom_data = [dom_data];
  753. }
  754. if (dom_data.includes(value)) {
  755. dom_data.splice(dom_data.indexOf(value), 1);
  756. } else {
  757. dom_data.push(value);
  758. }
  759. dom_data = arrDuplicate(dom_data);
  760. let dom = {
  761. name: ul.attr('name'),
  762. type: 'select_multiple_select',
  763. value: dom_data,
  764. select: dom_data,
  765. mold: 'select_multiple_select'
  766. };
  767. modifyData(dom);
  768. redenerUl(ul);
  769. isMultiple(ul.attr('name'));
  770. })
  771. })
  772. }
  773. function isMultiple(name) {
  774. let label = $('#' + name + '_lab');
  775. let select = $('#' + name + '_sel');
  776. let dom_data = _data[name].value;
  777. if (dom_data && dom_data.length === 1) {
  778. select.show();
  779. select.val(dom_data[0]);
  780. label.hide();
  781. } else if (controlJsType(dom_data, 'string')) {
  782. select.show();
  783. select.val(dom_data);
  784. label.hide();
  785. } else if (dom_data.length >= 2) {
  786. select.hide();
  787. label.show();
  788. }
  789. }
  790. // modify _data
  791. function modifyData(dom) {
  792. _data[dom.name].mold = dom.mold;
  793. _data[dom.name].value = dom.value;
  794. _data[dom.name].select = dom.value;
  795. redenerSearchFormOnData(dom.name, dom.value, dom.mold);
  796. let killings = _data[dom.name].killings;
  797. if (killings) {
  798. killings.forEach(function (targetName) {
  799. _data[targetName].value = '';
  800. redenerSearchFormOnData(_data[targetName].name, '', _data[targetName].mold);
  801. })
  802. }
  803. /* rules 规则
  804. * son 子级元素 对象形式多传递 子级元素不存在时且 required_without_all_if 忽略列表内元素皆无值时使子级元素设置默认值
  805. * {son:{key:{required_without_all_if:[],default:''}}}
  806. * date_relevance 时间关联 对象形式传递 date为起终时间 relevance为关联元素name
  807. * killing为标记干掉哪种类型元素(enum('date','relevance')) default为relevance列表值,时间差存在于该值中才会去选中
  808. * {date_relevance:{date:['',''],relevance:'',killing:'',default:[]}}
  809. * */
  810. let rules = _data[dom.name].rules;
  811. if (rules && JSON.stringify(rules) !== '{}') {
  812. if (rules['son']) {
  813. for (let key in rules['son']) {
  814. if (!dom.value) {
  815. redenerSearchFormOnData(_data[key].name, '', _data[key].mold);
  816. continue;
  817. }
  818. if (!_data[key].value) {
  819. if (rules['son'][key].required_without_all_if && Array.isArray(rules['son'][key].required_without_all_if)) {
  820. let sign = true;
  821. rules['son'][key].required_without_all_if.some(function (name) {
  822. if (_data[name].value) {
  823. sign = false;
  824. return true;
  825. }
  826. });
  827. if (sign) {
  828. _data[_data[key].name].mold = _data[key].mold;
  829. _data[_data[key].name].value = rules['son'][key].default;
  830. _data[_data[key].name].select = rules['son'][key].default;
  831. redenerSearchFormOnData(_data[key].name, rules['son'][key].default, _data[key].mold);
  832. }
  833. }
  834. }
  835. }
  836. }
  837. if (rules['date_relevance']) {
  838. if (!rules['date_relevance']['date']
  839. || !Array.isArray(rules['date_relevance']['date'])
  840. || rules['date_relevance']['date'].length !== 2
  841. || !rules['date_relevance']['relevance']) return;
  842. let start = _data[rules['date_relevance']['date'][0]];
  843. let end = _data[rules['date_relevance']['date'][1]];
  844. let relevance = _data[rules['date_relevance']['relevance']];
  845. if (dom.name === rules['date_relevance']['relevance']) {
  846. if (relevance.value) {
  847. let today = getToday();
  848. let tarDate = getSubDate(getToday(), relevance.value);
  849. _data[end.name].value = today + " 23:59";
  850. _data[start.name].value = tarDate + " 00:00";
  851. /*redenerSearchFormOnData(end.name, tarDate+" 23:59", end.mold);
  852. redenerSearchFormOnData(start.name, tarDate+" 00:00", start.mold);*/
  853. }
  854. return;
  855. }
  856. if (!start.value || !end.value) {
  857. if (rules['date_relevance']['killing'] === 'date') {
  858. _data[start.name].value = '';
  859. redenerSearchFormOnData(start.name, '', start.mold);
  860. _data[start.name].value = '';
  861. redenerSearchFormOnData(end.name, '', end.mold);
  862. } else {
  863. _data[relevance.name].value = '';
  864. redenerSearchFormOnData(relevance.name, '', relevance.mold);
  865. }
  866. }
  867. if (start.value && end.value) {
  868. start.value = start.value.replace('+', ' ');
  869. end.value = end.value.replace('+', ' ');
  870. let diff = getDiffDay(start.value, end.value);
  871. if (rules['date_relevance']['default'] && rules['date_relevance']['default'].includes(diff)) {
  872. _data[relevance.name].value = diff;
  873. redenerSearchFormOnData(relevance.name, diff, relevance.mold);
  874. } else {
  875. _data[relevance.name].value = '';
  876. redenerSearchFormOnData(relevance.name, '', relevance.mold);
  877. }
  878. }
  879. }
  880. }
  881. }
  882. function getDiffDay(startDate, endDate) {
  883. startDate = new Date(startDate);
  884. endDate = new Date(endDate);
  885. return Math.floor((endDate.getTime() - startDate.getTime()) / (24 * 3600 * 1000));
  886. }
  887. function getSubDate(date, value) {
  888. date = new Date(date);
  889. date.setDate(date.getDate() - value);
  890. return dateFormat(date);
  891. }
  892. function getToday() {
  893. let myDate = new Date();
  894. return dateFormat(myDate);
  895. }
  896. function dateFormat(date) {
  897. let year = date.getFullYear();
  898. let month = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
  899. let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  900. return year + "-" + month + "-" + day;
  901. }
  902. // save search on cookie
  903. function saveSearchOnCookie() {
  904. let saveData = {};
  905. for (const key in _data) {
  906. if (!['', null, undefined].includes(_data[key].value)) {
  907. if (controlJsType(_data[key].value, 'array') && _data[key].value.length === 0) {
  908. continue;
  909. }
  910. saveData[key] = {
  911. value: _data[key].value,
  912. mold: _data[key].mold,
  913. }
  914. }
  915. }
  916. if (!_this.page) {
  917. saveData['page'] = _this.page;
  918. }
  919. saveCookie(saveData);
  920. }
  921. // hour_minute
  922. function verifyTime(time) {
  923. setTimeout(function () {
  924. time.value = time.value.replace(':', ':');
  925. time.value = time.value.replace(/[a-zA-Z]/, '');
  926. time.value = time.value.replace(/[^\d^\:]/, '');
  927. time.value = time.value.replace(/\:\:/, ':');
  928. time.value = time.value.replace(/^([\d]{1})([\s]{1})/, "$1:");
  929. time.value = time.value.replace(/^([\d]{1})([\d]{1})([\s]{1})/, "$1$2:");
  930. time.value = time.value.replace(/^([\d]{1})([\d]{1})([\d]{1})/, "$1$2:$3");
  931. time.value = time.value.replace(/^([\d]{1})([\d]{1})([\d]{1})([\d]{1})(.*?)/, "$1$2$3$4");
  932. time.value = time.value.replace(/^([\d]{1})([\d]{1})([\d]{1})([\d]{1})/, "$1$2:$3$4");
  933. time.value = time.value.replace(/^([\d]{1})([\d]{1}):([\d]{1})([\d]{1})([\s\S]{1})/, "$1$2:$3$4");
  934. time.value = time.value.replace(/^([\d]{1})([\d]{1}):([\D]{1,99})/, "$1$2:");
  935. time.value = time.value.replace(/^([\d]{1})([\d]{1})([\d]{1})\./, "$1:$2$3");
  936. time.value = time.value.replace(/^([\d]{1})\.([\d]{1})([\d]{1})/, "$1:$2$3");
  937. time.value = time.value.replace(/^([\d]{1})([\d]{2})([\S\s]{1,99})/, "$1$2:$3");
  938. time.value = time.value.replace(/^([\d]{1}):([\d]{2})([\S\s]{1,99})/, "$1:$2");
  939. time.value = time.value.replace(/^([\d])/, "$1");
  940. time.value = time.value.replace(/^([1])([\d]{1})/, "$1$2");
  941. time.value = time.value.replace(/^([3-9])([\d]{1})/, "2$2");
  942. time.value = time.value.replace(/^([2-9]{1})([4-9]{1})/, "$13");
  943. time.value = time.value.replace(/^([\d]{1})([\d]{1}):([6-9]{1})/, "$1$2:5");
  944. time.value = time.value.replace(/^([\d]{1})([\d]{1})$/, "$1$2:");
  945. }, 10);
  946. }
  947. // array duplicate
  948. function arrDuplicate(arr) {
  949. let res = [];
  950. let obj = {};
  951. for (let i = 0; i < arr.length; i++) {
  952. if ([undefined, null, ''].includes(arr[i])) {
  953. continue;
  954. }
  955. if (!obj[arr[i]]) {
  956. obj[arr[i]] = 1;
  957. res.push(arr[i]);
  958. }
  959. }
  960. return res;
  961. }
  962. // get search data
  963. function getSearchData(type = 'array') {
  964. let data = {};
  965. if (type === 'array') {
  966. for (let key in _data) {
  967. if (_data[key].value !== undefined && _data[key].value !== '') {
  968. data[key] = _data[key].value
  969. }
  970. }
  971. } else if (type === 'string') {
  972. data = '';
  973. for (let key in _data) {
  974. if (![undefined, null, ''].includes(_data[key].value)) {
  975. let value = _data[key].value;
  976. if (controlJsType(value, 'string')) {
  977. data = data + "&" + key + "=" + value;
  978. } else if (controlJsType(value, 'array')) {
  979. value.forEach(function (val) {
  980. data = data + "&" + key + "=" + val;
  981. });
  982. }
  983. }
  984. }
  985. }
  986. return data;
  987. }
  988. // renderer input
  989. function rendererSearchFormOnInput(key, value, mold) {
  990. if ([undefined, null, ''].includes(value)) {
  991. $(_form).find("input[name='" + key + "']").val('');
  992. $(_form).find("input[name='" + key + "']").removeClass('bg-warning');
  993. } else {
  994. $(_form).find("input[name='" + key + "']").val(value);
  995. $(_form).find("input[name='" + key + "']").addClass('bg-warning');
  996. }
  997. }
  998. // renderer select
  999. function rendererSearchFormOnSelect(key, value, mold) {
  1000. if ([undefined, null, ''].includes(value)) {
  1001. $(_form).find("select[name='" + key + "']").val('').removeClass('bg-warning');
  1002. if (key === 'paginate') {
  1003. $(_form).find("select[name='" + key + "']").val(_this.paginations[0]).removeClass('bg-warning');
  1004. }
  1005. } else {
  1006. $(_form).find("select[name='" + key + "']").val(value);
  1007. if (key === 'paginate' && value == _this.paginations[0]) {
  1008. return;
  1009. }
  1010. $(_form).find("select[name='" + key + "']").addClass('bg-warning');
  1011. }
  1012. }
  1013. // renderer check
  1014. function rendererSearchFormOnCheck(key, value, mold) {
  1015. if ([undefined, null, ''].includes(value)) {
  1016. $(_form).find("input[name='" + key + "']").prop("checked", false).removeClass('bg-warning');
  1017. } else {
  1018. if (controlJsType(value, 'array')) {
  1019. value.forEach(function (_value) {
  1020. $(_form).find("input[name='" + key + "'][value='" + _value + "']").prop("checked", true).addClass('bg-warning');
  1021. })
  1022. } else if (controlJsType(value, 'string')) {
  1023. $(_form).find("input[name='" + key + "'][value='" + value + "']").prop("checked", true).addClass('bg-warning');
  1024. }
  1025. }
  1026. }
  1027. // redener multiple select input
  1028. function rendererSearchFormOnMultipleSelect(key, value, mold) {
  1029. let ul = $(_form).find("ul[name='" + key + "']");
  1030. if ([undefined, null, ''].includes(value) || (controlJsType(value, 'array') && value.length === 0)) {
  1031. $(_form).find("input[name='" + key + "_tip']").removeClass('bg-warning');
  1032. ul.addClass('bg-warning');
  1033. } else if (!!value) {
  1034. $(_form).find("input[name='" + key + "_tip']").addClass('bg-warning');
  1035. ul.addClass('bg-warning');
  1036. } else if (controlJsType(value, 'array') && value.length >= 1) {
  1037. $(_form).find("input[name='" + key + "_tip']").addClass('bg-warning');
  1038. ul.addClass('bg-warning');
  1039. }
  1040. redenerUl(ul);
  1041. }
  1042. // redener multiple select ul
  1043. function redenerUl(ul) {
  1044. let name = ul.attr('name');
  1045. let select = _data[name].select;
  1046. if ([null, undefined].includes(select)) {
  1047. _data[name].select = [];
  1048. select = [];
  1049. }
  1050. let lis = ul.find('li');
  1051. if ([null, undefined, ''].includes(lis)) {
  1052. return;
  1053. } else {
  1054. lis.get().forEach(function (dom) {
  1055. if (select.includes($(dom).attr('value'))) {
  1056. dom.style.backgroundColor = "#4AA0E6";
  1057. $(dom).addClass('active');
  1058. } else {
  1059. dom.style.backgroundColor = "#FFFFFF";
  1060. $(dom).removeClass('active');
  1061. }
  1062. });
  1063. if (lis.length === select.length) {
  1064. $("#multipleCheckBox").prop("checked", true);
  1065. }
  1066. isMultiple(ul.attr('name'));
  1067. }
  1068. }
  1069. function rendererSearchFormOnTime(key, value, mold) {
  1070. if (![undefined, null, ''].includes(value)) {
  1071. $(_form).find("input[name=" + key + "]").val(value.substring(0, 10)).addClass('bg-warning');
  1072. if (value.length > 11 + 5) {
  1073. $(_form).find("input[name='" + key + "_tim']").val(value.substring(11, 5));
  1074. }
  1075. } else {
  1076. $(_form).find("input[name=" + key + "]").val("").removeClass('bg-warning');
  1077. $(_form).find("input[name='" + key + "_tim']").val("");
  1078. }
  1079. }
  1080. function visibleClearBtn() {
  1081. let bool = false;
  1082. for (const key in _data) {
  1083. if (key === 'paginate') {
  1084. if ((key === 'paginate' && _data[key].value !== _this.paginations[0])) {
  1085. bool = true;
  1086. }
  1087. } else {
  1088. if (![undefined, null, ''].includes(_data[key].value)) {
  1089. if (controlJsType(_data[key].value, 'array') && _data[key].value.length === 0) {
  1090. continue;
  1091. }
  1092. bool = true;
  1093. }
  1094. }
  1095. }
  1096. if (bool) {
  1097. _clearTr.show();
  1098. } else {
  1099. _clearTr.hide();
  1100. }
  1101. }
  1102. function inputKeyDown() {
  1103. $(_form).find('input').keydown(function (e) {
  1104. if (e.keyCode === 13) {
  1105. setTimeout(function () {
  1106. if (_this.keydownSearch) _this.onsubmit();
  1107. if (controlJsType(_this.keydownfun, 'fun')) _this.keydownfun();
  1108. }, 100);
  1109. }
  1110. });
  1111. $(_form).find("input[type='date']").change(function (e) {
  1112. setTimeout(function () {
  1113. // if (_this.keydownSearch)_this.onsubmit();
  1114. if (controlJsType(_this.keydownfun, 'fun')) _this.keydownfun();
  1115. }, 100);
  1116. });
  1117. }
  1118. function selectChange() {
  1119. // $(_form).find('select').change(function () {
  1120. // _this.onsubmit();
  1121. // })
  1122. }
  1123. function redenerSearchFormOnData(key, value, mold) {
  1124. if (mold === 'input' || mold === 'dateTime' || mold === 'dateTimeLocal' || mold === 'dateMonth') {
  1125. rendererSearchFormOnInput(key, value, mold);
  1126. } else if (mold === 'select') {
  1127. rendererSearchFormOnSelect(key, value, mold);
  1128. } else if (mold === 'check') {
  1129. rendererSearchFormOnCheck(key, value, mold);
  1130. } else if (mold === 'select_multiple_select') {
  1131. rendererSearchFormOnMultipleSelect(key, value, mold);
  1132. } else if (mold === 'time') {
  1133. rendererSearchFormOnTime(key, value, mold);
  1134. }
  1135. }
  1136. // 数据转换
  1137. function switchData() {
  1138. _this.condition.forEach(function (conditions) {
  1139. conditions.forEach(function (condition) {
  1140. if (['input', 'select', 'dateTime', 'dateTimeLocal', 'dateMonth', 'search_select', 'time', 'select_multiple_select', 'checkbox'].includes(condition.type)) {
  1141. if (!condition.select) {
  1142. condition.select = '';
  1143. }
  1144. let data = {
  1145. name: condition.name,
  1146. type: condition.type,
  1147. value: condition.value,
  1148. select: condition.select,
  1149. mold: ['input', 'dateTime'].includes(condition.type) ? 'input' : condition.type,
  1150. data: condition.data,
  1151. killings: condition.killings,
  1152. rules: condition.rules,
  1153. required: condition.required,
  1154. }
  1155. if (condition.type === 'search_select') {
  1156. data.mold = 'select';
  1157. }
  1158. if (condition.type === 'select_multiple_select') {
  1159. data.value = [];
  1160. }
  1161. _data[condition.name] = data;
  1162. } else if (['dateTime_dateTime', 'input_input', 'input_select', 'dateTime_select'].includes(condition.type)) {
  1163. let types = condition.type.split("_");
  1164. if (!condition.select) {
  1165. condition.select = ['', ''];
  1166. }
  1167. if (!condition.value) {
  1168. condition.value = ['', ''];
  1169. }
  1170. condition.name.forEach(function (_name, index) {
  1171. let data = {
  1172. name: _name,
  1173. type: types[index],
  1174. value: condition.value[index],
  1175. select: condition.select[index],
  1176. mold: ['input', 'dateTime'].includes(types[index]) ? 'input' : types[index],
  1177. killings: condition.killings,
  1178. rules: condition.rules,
  1179. required: condition.required,
  1180. }
  1181. if (condition.killings && Array.isArray(condition.killings)) {
  1182. data.killings = condition.killings[index];
  1183. }
  1184. if (condition.rules && Array.isArray(condition.rules)) {
  1185. data.rules = condition.rules[index];
  1186. }
  1187. _data[_name] = data;
  1188. })
  1189. }
  1190. })
  1191. })
  1192. }
  1193. // redererSearchForm on cookie
  1194. function rendererSearchForm() {
  1195. let url = document.referrer;
  1196. let partt = /(Edit\/)|(\/eidt)|(eidt\/)/i;
  1197. if (url.search(partt) > 0) {
  1198. rendererOptionOnCookie();
  1199. } else {
  1200. rendererOptionOnUrl();
  1201. }
  1202. }
  1203. function rendererOptionOnCookie() {
  1204. let data = fetchCookie();
  1205. if (data === undefined) {
  1206. return;
  1207. }
  1208. data = JSON.parse(data);
  1209. for (let key in data) {
  1210. _data[key].value = data[key].value;
  1211. _data[key].mold = data[key].mold;
  1212. _data[key].select = data[key].select;
  1213. }
  1214. rendererSearchFormOn_data();
  1215. }
  1216. function rendererOptionOnUrl() {
  1217. if (!window.location.search) {
  1218. return;
  1219. }
  1220. searchOptionToUrlsearch();
  1221. rendererSearchFormOn_data();
  1222. }
  1223. function rendererSearchFormOn_data() {
  1224. for (let key in _data) {
  1225. let value = _data[key].value, type = _data[key].type, mold = _data[key].mold;
  1226. // if(value){
  1227. // let string = new String(value);
  1228. // if(string.length > 1 ){
  1229. // string = string.replace(/\+/g,' ');
  1230. // value = string
  1231. // }
  1232. // }
  1233. _data[key].select = value;
  1234. if (['input', 'dateTime', 'dateTimeLocal', 'dateMonth'].includes(mold)) {
  1235. rendererSearchFormOnInput(key, value, mold);
  1236. } else if (['select'].includes(mold)) {
  1237. rendererSearchFormOnSelect(key, value, mold);
  1238. } else if (['checkbox'].includes(mold)) {
  1239. rendererSearchFormOnCheck(key, value, mold);
  1240. } else if (['select_multiple_select'].includes(mold)) {
  1241. if (controlJsType(value, ['array', 'string'])) {
  1242. let ul = $(_form).find("ul[name='" + key + "']");
  1243. redenerUl(ul);
  1244. rendererSearchFormOnMultipleSelect(key, value, mold);
  1245. }
  1246. } else if (['time'].includes(type) && value !== undefined) {
  1247. $(_form).find("input[name='" + key + "']").val(value.substr(0, 10)).addClass('bg-warning');
  1248. $(_form).find("input[id='" + key + "_min']").val(value.substr(11, value.length));
  1249. }
  1250. }
  1251. if ([undefined, null, ''].includes(data['paginate'])) {
  1252. return;
  1253. }
  1254. if ([undefined, null, ''].includes(data['paginate'].value)) {
  1255. $(_form).find("select[name='paginate']").val(_this.paginations[0]);
  1256. } else {
  1257. $(_form).find("select[name='paginate']").val(data['paginate'].value);
  1258. }
  1259. }
  1260. // url path on search form option renderer
  1261. function searchOptionToUrlsearch() {
  1262. let data = (window.location.search);
  1263. data = decodeURIComponent(decodeURIComponent(data));
  1264. //data = decodeURIComponent(data).replace('%20'," ");
  1265. if (!!data) {
  1266. data = (data.substr(1)).split('&');
  1267. data = convertArrayToObj(data);
  1268. for (const key in data) {
  1269. if (!data[key] || _this.param[key]) {
  1270. continue;
  1271. }
  1272. if (key === 'paginate') {
  1273. _data[key] = {
  1274. name: key,
  1275. value: data[key],
  1276. type: 'select',
  1277. mold: 'select'
  1278. }
  1279. } else if (key === 'page') {
  1280. _page = data[key];
  1281. } else if (_data[key].type === 'select_multiple_select') {
  1282. if (!_data[key]) {
  1283. _data[key].value = [];
  1284. } else {
  1285. _data[key].value = data[key].split(',');
  1286. }
  1287. } else {
  1288. _data[key].value = data[key];
  1289. }
  1290. }
  1291. }
  1292. }
  1293. // cookie -> data
  1294. function set_dataOnCookie() {
  1295. let data = fetchCookie();
  1296. data = JSON.parse(data);
  1297. if (!data) {
  1298. return;
  1299. }
  1300. for (const key in data) {
  1301. if (!!data[key]) {
  1302. _data[key].value = data[key].value;
  1303. }
  1304. }
  1305. }
  1306. // array - > object
  1307. function convertArrayToObj(arr) {
  1308. if (!arr || !controlJsType(arr, 'array')) {
  1309. return {};
  1310. } else if (controlJsType(arr, 'array') && arr.length > 0) {
  1311. let data = {};
  1312. arr.forEach(function (map) {
  1313. let arr = map.split('='), key = arr[0], value = arr[1];
  1314. if (!data[key]) {
  1315. data[key] = value;
  1316. } else {
  1317. if (controlJsType(data[key], 'string')) {
  1318. data[key] = [data[key], value];
  1319. } else if (controlJsType(data[key], 'array')) {
  1320. data[key].push(value);
  1321. }
  1322. }
  1323. })
  1324. return data;
  1325. }
  1326. }
  1327. // paginate
  1328. function pervPage() {
  1329. let obj = getSearchData();
  1330. if (!!obj['page'] && obj['page'] > 1) {
  1331. obj['page'] = obj['page'] - 1;
  1332. }
  1333. return getSearchUri(obj);
  1334. }
  1335. function nextPage() {
  1336. let obj = getSearchData();
  1337. if (!!obj['page']) {
  1338. obj['page'] = obj['page'] + 1;
  1339. } else {
  1340. obj['page'] = 2;
  1341. }
  1342. if (!obj['paginate']) {
  1343. obj['paginate'] = 50;
  1344. }
  1345. return getSearchUri(obj);
  1346. }
  1347. function getSearchObj() {
  1348. let data = window.location.search;
  1349. data = decodeURIComponent(data);
  1350. if (!data) {
  1351. return {};
  1352. }
  1353. let saveData = {};
  1354. data = data.substr(1).split('&');
  1355. data.forEach(function (map) {
  1356. let arr = map.split('&'), key = arr[0], value = arr[1];
  1357. if (!!value) {
  1358. if (!saveData[key]) {
  1359. saveData[key] = value;
  1360. } else {
  1361. if (controlJsType(data[key], 'string')) {
  1362. saveData[key] = [saveData[key], value];
  1363. } else if (controlJsType(data[key], 'array')) {
  1364. saveData[key].push(value);
  1365. }
  1366. }
  1367. }
  1368. })
  1369. return saveData;
  1370. }
  1371. function getPage() {
  1372. return getSearchObj()['page'];
  1373. }
  1374. // paginate
  1375. function pervPage() {
  1376. let obj = getSearchData();
  1377. _page = getPage();
  1378. if (!obj['paginate']) {
  1379. obj['paginate'] = 50;
  1380. }
  1381. if (Number(_page) > 1) {
  1382. _page = Number(_page) - 1;
  1383. } else {
  1384. _page = 1;
  1385. }
  1386. return getSearchUri(obj);
  1387. }
  1388. function nextPage() {
  1389. let obj = getSearchData();
  1390. _page = getPage();
  1391. if (!_page) {
  1392. _page = 1;
  1393. }
  1394. if (_page) {
  1395. _page = Number(_page) + 1;
  1396. }
  1397. if (!obj['paginate']) {
  1398. obj['paginate'] = 50;
  1399. }
  1400. return getSearchUri(obj);
  1401. }
  1402. function goPage(page) {
  1403. let obj = getSearchData();
  1404. _page = page;
  1405. return getSearchUri(obj);
  1406. }
  1407. function getSearchUri(obj) {
  1408. let string = "?";
  1409. for (const key in obj) {
  1410. if (controlJsType(obj[key], ['string', 'number'])) {
  1411. string += key + "=" + obj[key] + '&';
  1412. } else if (controlJsType(obj[key], 'array') && obj[key].length > 0) {
  1413. obj[key].forEach(function (value) {
  1414. string += key + "=" + value + '&';
  1415. })
  1416. }
  1417. }
  1418. return string;
  1419. }
  1420. };