queryForm.js 56 KB

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