queryForm.js 54 KB

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