queryForm.js 59 KB

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