queryForm.js 56 KB

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