queryForm.js 59 KB

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