queryForm.js 55 KB

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