queryForm.js 62 KB

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