queryForm.js 62 KB

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