queryForm.js 55 KB

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