queryForm.js 54 KB

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