queryForm.js 54 KB

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