queryForm.js 62 KB

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