queryForm.js 61 KB

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