queryForm.js 57 KB

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