queryForm.js 50 KB

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