queryForm.js 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  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.blur(function(){
  330. modifyData({name: condition.name, type: 'dateMonth', value: this.value, mold: 'dateMonth'});
  331. });
  332. dateMonth.keypress(function(event){
  333. if(event.which === 13){
  334. modifyData({name: condition.name, type: 'input', value: this.value, mold: 'input'});
  335. }
  336. });
  337. return dateMonth;
  338. }
  339. // input
  340. function getInput(condition) {
  341. 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' >");
  342. input.attr('title', condition.tip === undefined ? '' : condition.tip);
  343. input.blur(function(){
  344. modifyData({name: condition.name, type: 'input', value: this.value, mold: 'input'});
  345. });
  346. input.keypress(function(event){
  347. if(event.which === 13)modifyData({name: condition.name, type: 'input', value: this.value, mold: 'input'});
  348. });
  349. return input;
  350. }
  351. // select
  352. function getSelect(condition) {
  353. let select = $("<select name='" + condition.name + "' class='form-control form-control-sm' style='max-width: 200px;' data-toggle='tooltip' data-placement='top'></select>")
  354. select.attr('title', condition.tip === undefined ? '' : condition.tip);
  355. let tip = $("<option value class='text-secondary'>" + condition.placeholder + "</option>")
  356. select.append(tip);
  357. condition.data.forEach(function (map) {
  358. let option = $("<option value='" + map.name + "' class='text-secondary'>" + map.value + "</option>")
  359. select.append(option);
  360. })
  361. select.on('change', function () {
  362. let dom = {name: condition.name, type: 'select', value: this.value, mold: 'select'}
  363. modifyData(dom)
  364. })
  365. return select;
  366. }
  367. // search_select
  368. function getSearchSelect(condition) {
  369. let div = $("<div class='form-inline'></div>");
  370. let input = $("<input class='form-control form-control-sm' placeholder='" + condition.placeholder[0] + "' style='width: 70px;' autocomplete='off' data-toggle='tooltip' data-placement='top'>");
  371. input.attr('title', controlJsType(condition.tip[0], 'undefined') ? '输入关键词快速定位下拉列表,回车确定' : condition.tip[0]);
  372. 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>");
  373. select.attr('title', controlJsType(condition.tip[1], 'undefined') ? '' : condition.tip[1]);
  374. let placeholder = '';
  375. if (controlJsType(condition.placeholder, 'array') && !['', undefined, null].includes(condition.placeholder[1])) {
  376. placeholder = condition.placeholder[1];
  377. }
  378. fillSelectOption(select, condition.data, placeholder);
  379. input.blur(function(){
  380. let value = this.value;
  381. let data = condition.data.filter(function (map) {
  382. return map.value.includes(value);
  383. });
  384. fillSelectOption(select, data || condition.data);
  385. select.val(_data[condition.name].select);
  386. });
  387. input.keypress(function(event){
  388. if(event.which === 13){
  389. let value = this.value;
  390. let data = condition.data.filter(function (map) {
  391. return map.value.includes(value);
  392. })
  393. fillSelectOption(select, data || condition.data);
  394. select.val(_data[condition.name].select);
  395. }
  396. });
  397. select.change(function () {
  398. modifyData({name: condition.name, type: 'input_select', value: this.value, mold: 'select'});
  399. })
  400. return div.append(input).append(select);
  401. }
  402. // input_select
  403. function getInputSelect(condition) {
  404. let div = $("<div class='form-inline'></div>");
  405. 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' >");
  406. input.attr('title', controlJsType(condition.tip[0], 'undefined') ? '' : condition.tip[0]);
  407. let select = $("<select class='form-control form-control-sm ml-2' name='" + condition.name[1] + "' style='max-width: 200px;' title='" + condition.tip[1] + "''>");
  408. fillSelectOption(select, condition.data);
  409. input.blur(function(){
  410. modifyData({name: this.name, type: 'input_select_longtext', value: this.value, mold: 'input'});
  411. });
  412. input.keypress(function(event){
  413. if(event.which === 13)modifyData({name: this.name, type: 'input_select_longtext', value: this.value, mold: 'input'});
  414. });
  415. select.change(function () {
  416. modifyData({name: this.name, type: 'input_select_longtext', value: this.value, mold: 'select'});
  417. })
  418. return div.append(input).append(select);
  419. }
  420. // datTime
  421. function getdateTime(condition) {
  422. 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'>");
  423. dateTime.attr('title', controlJsType(condition.tip, 'undefined') ? '' : condition.tip);
  424. dateTime.blur(function(){
  425. modifyData({name: condition.name, type: 'dateTime', value: this.value, mold: 'dateTime'});
  426. });
  427. return dateTime;
  428. }
  429. //dateTimeLocal
  430. function getDateTimeLocal(condition) {
  431. 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' >");
  432. dateTimeLocal.attr('title', condition.tip === undefined ? '' : condition.tip);
  433. dateTimeLocal.bind('input propertychange', function () {
  434. modifyData({name: condition.name, type: 'dateTimeLocal', value: this.value, mold: 'dateTimeLocal'});
  435. });
  436. return dateTimeLocal;
  437. }
  438. // time
  439. function getTime(condition) {
  440. let div = $("<div class='form-inline'></div>");
  441. let dateTime = $("<input type='date' class='form-control form-control-sm' name='" + condition.name + "' style='width:150px' data-toggle='tooltip' data-placement='top' >");
  442. dateTime.attr('title', controlJsType(condition.tip[0], 'undefined') ? '' : condition.tip[0]);
  443. 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' >");
  444. min.attr('title', controlJsType(condition.tip[1], 'undefined') ? '' : condition.tip[1]);
  445. dateTime.blur(function(){
  446. let value = this.value !== '' ? this.value + ' ' +(!!min.val() ? min.val() :'00:00' ) : '';
  447. modifyData( {name: condition.name, type: 'time', value: value, mold: 'time'});
  448. });
  449. dateTime.keypress(function(event){
  450. if(event.which === 13){
  451. let value = this.value !== '' ? this.value + ' ' +(!!min.val() ? min.val() :'00:00' ) : '';
  452. modifyData({name: this.name, type: 'input_select_longtext', value: value, mold: 'input'});
  453. }
  454. });
  455. min.bind('input propertychange', function () {
  456. if (['null', 'undefined', ''].includes(dateTime.val()))return;
  457. let value = ['null', 'undefined', ''].includes(dateTime.val()) ? '' : dateTime.val() + ' ' + min.val();
  458. modifyData({name: condition.name, type: 'time', value: value, mold: 'time'});
  459. })
  460. return div.append(dateTime).append(min);
  461. }
  462. // input_input
  463. function getInputInput(condition) {
  464. let div = $("<div class='form-inline'></div>");
  465. 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] + "'>");
  466. input1.attr('title', controlJsType(condition.tip[0], 'undefined') ? '' : condition.tip[0]);
  467. 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] + "'>");
  468. input2.attr('title', controlJsType(condition.tip[1], 'undefined') ? '' : condition.tip[1]);
  469. input1.blur(function(){
  470. modifyData({name: this.name, type: 'input_input', value: this.value, mold: 'input'});
  471. });
  472. input1.keypress(function(event){
  473. if(event.which === 13)modifyData({name: this.name, type: 'input_input', value: this.value, mold: 'input'});
  474. });
  475. input2.blur(function(){
  476. modifyData({name: this.name, type: 'input_input', value: this.value, mold: 'input'});
  477. });
  478. input2.keypress(function(event){
  479. if(event.which === 13)modifyData({name: this.name, type: 'input_input', value: this.value, mold: 'input'});
  480. });
  481. return div.append(input1).append(input2);
  482. }
  483. // dateTime_dateTime
  484. function getdateTimedateTime(condition) {
  485. let div = $("<div class='form-inline'></div>");
  486. let dateTime1 = $("<input type='date' class='form-control form-control-sm' name='" + condition.name[0] + "' style='width: 150px' data-toggle='tooltip' data-placement='top' >");
  487. dateTime1.attr('title', controlJsType(condition.tip[0], 'undefined') ? '' : condition.tip[0]);
  488. 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' >");
  489. dateTime2.attr('title', controlJsType(condition.tip[1], 'undefined') ? '' : condition.tip[1]);
  490. dateTime1.bind('input propertychange', function () {
  491. let dom = {name: this.name, type: 'dateTime_dateTime', value: this.value, mold: 'dateTime'};
  492. modifyData(dom);
  493. })
  494. dateTime2.bind('input propertychange', function () {
  495. let dom = {name: this.name, type: 'dateTime_dateTime', value: this.value, mold: 'dateTime'};
  496. modifyData(dom);
  497. })
  498. return div.append(dateTime1).append(dateTime2);
  499. }
  500. // dateTime_select
  501. function getdateTimeSelect(condition) {
  502. let div = $("<div class='form-inline'></div>");
  503. 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' >");
  504. dateTime.attr('title', controlJsType(condition.tip[0], 'undefined') ? '' : condition.tip[0]);
  505. 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>");
  506. select.attr('title', controlJsType(condition.tip[1], 'undefined') ? '' : condition.tip[1]);
  507. fillSelectOption(select, condition.data)
  508. dateTime.bind('input propertychange', function () {
  509. modifyData( {name: this.name, type: 'dateTime_select', value: this.value, mold: 'dateTime'});
  510. })
  511. select.change(function () {
  512. modifyData({name: this.name, type: 'dateTime_select', value: this.value, mold: 'select'});
  513. })
  514. return div.append(dateTime).append(select);
  515. }
  516. // select_multiple_select
  517. function getSelectMultipleSelect(condition) {
  518. let div = $("<div class='form-inline' style='position: relative'></div>");
  519. 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>");
  520. let label = $("<label id='" + condition.name + "_lab' style='vertical-align: middle; width: 100px' >(多选)</label>").hide();
  521. let select_div = $("<div ></div>");
  522. select.attr('title', controlJsType(condition.tip[0], 'undefined') ? '' : condition.tip[0]);
  523. let placeholder = controlJsType(condition.placeholder[0], 'undefined') ? '' : condition.placeholder[0];
  524. fillSelectOption(select, condition.data, placeholder);
  525. 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'>");
  526. input.attr('title', controlJsType(condition.tip[1], 'undefined') ? '' : condition.tip[1]);
  527. input.attr('placeholder', controlJsType(condition.placeholder[1], 'undefined') ? '' : condition.placeholder[1])
  528. 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>");
  529. 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>");
  530. let check = $("<input id='multipleCheckBox' type = 'checkbox' style='z-index:101;position:fixed;top:0;right: 0;border:1px green' >");
  531. let isMouseenterCheckBox = false;
  532. select_div.append(input).append(ul_div.append(ul).append(check));
  533. div.append(select).append(label).append(select_div);
  534. fillMultipleSelectOption(ul, condition.data, condition.name);
  535. check.click(function () {
  536. _data[condition.name].value = [];
  537. _data[condition.name].select = [];
  538. if (check[0].checked === true){
  539. condition.data.forEach(function (data) {
  540. _data[condition.name].value.push(data.name);
  541. if (_data[condition.name].select) _data[condition.name].select.push(data.name);
  542. else _data[condition.name].select = [data.name];
  543. });
  544. }else{
  545. let dom = {
  546. name: condition.name,
  547. mold: 'select_multiple_select',
  548. value: [],
  549. select:[],
  550. };
  551. modifyData(dom);
  552. }
  553. redenerUl(ul);
  554. });
  555. check.mouseleave(()=>{
  556. isMouseenterCheckBox = false;
  557. });
  558. check.mouseenter(()=>{
  559. isMouseenterCheckBox = true;
  560. });
  561. input.bind('input propertychange', function () {
  562. let value = this.value;
  563. if (value === '') {
  564. fillMultipleSelectOption(ul, _data[condition.name].data, condition.name);
  565. redenerUl(ul);
  566. return;
  567. }
  568. let select = _data[condition.name].select;
  569. if ([undefined, null, ''].includes(select)) {
  570. select = [];
  571. }
  572. let select_data = condition.data.filter(function (map) {
  573. return map.value.includes(value) || select.includes(map.name);
  574. })
  575. fillMultipleSelectOption(ul, select_data, condition.name);
  576. redenerUl(ul);
  577. });
  578. select.change(function () {
  579. let dom = {
  580. name: condition.name,
  581. mold: 'select_multiple_select',
  582. value: [this.value]
  583. }
  584. modifyData(dom);
  585. redenerUl(ul);
  586. })
  587. input.focus(function () {
  588. ul_div.show();
  589. })
  590. input.blur(function () {
  591. setTimeout(function () {
  592. if (!ul_div.is(':focus') && !check.is(':focus')) {
  593. ul_div.hide();
  594. }
  595. }, 100);
  596. })
  597. select_div.focus(function () {
  598. ul_div.show();
  599. })
  600. select_div.blur(function () {
  601. ul_div.hide();
  602. })
  603. /*ul_div.blur(function () {
  604. if (!isMouseenterCheckBox){
  605. ul_div.hide();
  606. }
  607. })*/
  608. ul_div.hide();
  609. ul_div.mouseleave(function () {
  610. if(_data[condition.name].value.length>0){
  611. _this.onsubmit();
  612. ul_div.hide();
  613. return;
  614. }
  615. if (!isMouseenterCheckBox){
  616. ul_div.hide();
  617. }
  618. });
  619. ul_div.mouseenter(function () {
  620. if(_data[condition.name].value.length>0){
  621. setTimeout(()=> {
  622. if (ul_div.is(':visible')){
  623. ul_div.attr("title","移出多选区域即可自动提交");
  624. ul_div.tooltip('show');
  625. }
  626. },1000);
  627. }
  628. });
  629. return div;
  630. }
  631. // checkbox
  632. function getCheckBox(condition) {
  633. let div1 = $("<div class='form-inline'></div>")
  634. condition.data.forEach(function (map, index) {
  635. let div = $('<div class="custom-control custom-control-inline custom-checkbox"></div>');
  636. let checkbox = $("<input type='checkbox' name='" + condition.name + "' class='custom-control-input' id='" + condition.name + index + "' value='" + map.name + "' >");
  637. let label = $("<label class='custom-control-label text-left font-weight-norma' for='" + condition.name + index + "'>" + map.value + "</label>")
  638. div1.append(div.append(checkbox).append(label));
  639. checkbox.click(function () {
  640. let value = [];
  641. $.each($("input:checkbox[name='" + condition.name + "']:checked"), function () {
  642. value.push($(this).val());
  643. })
  644. let dom = {name: condition.name, type: 'checkbox', value: value, mold: 'check'};
  645. modifyData(dom);
  646. _this.onsubmit();
  647. });
  648. })
  649. return div1;
  650. }
  651. // fill select
  652. function fillSelectOption(select, data, placeholder = '') {
  653. select.html('');
  654. if (![undefined, null].includes(placeholder)) {
  655. let option = $("<option value></option>");
  656. if ('' === placeholder) {
  657. option = $("<option value>" + '全部' + placeholder + "</option>");
  658. } else {
  659. option = $("<option value>" + placeholder + "</option>");
  660. }
  661. select.append(option);
  662. }
  663. data.forEach(function (map) {
  664. let option = $("<option value='" + map.name + "'>" + map.value + "</optio>");
  665. select.append(option);
  666. })
  667. if (data !== undefined) {
  668. }
  669. }
  670. // fill multiple select ul
  671. function fillMultipleSelectOption(ul, data, name) {
  672. ul.html('');
  673. data.forEach(function (map) {
  674. let span = $("<span style='cursor:default'>" + map.value + "</span>");
  675. let li = $("<li class='list-group-item pt-0 pb-0' value='" + map.name + "'></li>");
  676. li.append(span);
  677. ul.append(li);
  678. li.click(function () {
  679. let value = li.attr('value');
  680. let dom_data = _data[name].value;
  681. if(!dom_data){
  682. dom_data = [];
  683. }
  684. if (controlJsType(dom_data,'string')){
  685. dom_data = [dom_data];
  686. }
  687. if (dom_data.includes(value)) {
  688. dom_data.splice(dom_data.indexOf(value), 1);
  689. } else {
  690. dom_data.push(value);
  691. }
  692. dom_data = arrDuplicate(dom_data);
  693. let dom = {
  694. name: ul.attr('name'),
  695. type: 'select_multiple_select',
  696. value: dom_data,
  697. select: dom_data,
  698. mold: 'select_multiple_select'
  699. };
  700. modifyData(dom);
  701. redenerUl(ul);
  702. isMultiple(ul.attr('name'));
  703. })
  704. })
  705. }
  706. function isMultiple(name) {
  707. let label = $('#' + name + '_lab');
  708. let select = $('#' + name + '_sel');
  709. let dom_data = _data[name].value;
  710. if (dom_data && dom_data.length === 1 ) {
  711. select.show();
  712. select.val(dom_data[0]);
  713. label.hide();
  714. }else if(controlJsType(dom_data,'string')){
  715. select.show();
  716. select.val(dom_data);
  717. label.hide();
  718. } else if (dom_data.length >= 2) {
  719. select.hide();
  720. label.show();
  721. }
  722. }
  723. // modify _data
  724. function modifyData(dom) {
  725. _data[dom.name].mold = dom.mold;
  726. _data[dom.name].value = dom.value;
  727. _data[dom.name].select = dom.value;
  728. redenerSearchFormOnData(dom.name, dom.value, dom.mold);
  729. let killings=_data[dom.name].killings;
  730. if(killings){
  731. killings.forEach(function(targetName){
  732. _data[targetName].value= '';
  733. redenerSearchFormOnData(_data[targetName].name, '', _data[targetName].mold);
  734. })
  735. }
  736. /* rules 规则
  737. * son 子级元素 对象形式多传递 子级元素不存在时且 required_without_all_if 忽略列表内元素皆无值时使子级元素设置默认值
  738. * {son:{key:{required_without_all_if:[],default:''}}}
  739. * date_relevance 时间关联 对象形式传递 date为起终时间 relevance为关联元素name
  740. * killing为标记干掉哪种类型元素(enum('date','relevance')) default为relevance列表值,时间差存在于该值中才会去选中
  741. * {date_relevance:{date:['',''],relevance:'',killing:'',default:[]}}
  742. * */
  743. let rules = _data[dom.name].rules;
  744. if (rules && JSON.stringify(rules) !== '{}'){
  745. if (rules['son']){
  746. for (let key in rules['son']){
  747. if (!dom.value){
  748. redenerSearchFormOnData(_data[key].name, '', _data[key].mold);
  749. continue;
  750. }
  751. if (!_data[key].value){
  752. if (rules['son'][key].required_without_all_if && Array.isArray(rules['son'][key].required_without_all_if)){
  753. let sign = true;
  754. rules['son'][key].required_without_all_if.some(function (name) {
  755. if (_data[name].value){ sign=false; return true;}
  756. });
  757. if (sign) {
  758. _data[_data[key].name].mold = _data[key].mold;
  759. _data[_data[key].name].value = rules['son'][key].default;
  760. _data[_data[key].name].select = rules['son'][key].default;
  761. redenerSearchFormOnData(_data[key].name, rules['son'][key].default, _data[key].mold);
  762. }
  763. }
  764. }
  765. }
  766. }
  767. if (rules['date_relevance']){
  768. if (!rules['date_relevance']['date']
  769. || !Array.isArray(rules['date_relevance']['date'])
  770. || rules['date_relevance']['date'].length !== 2
  771. || !rules['date_relevance']['relevance']) return;
  772. let start = _data[rules['date_relevance']['date'][0]];
  773. let end = _data[rules['date_relevance']['date'][1]];
  774. let relevance = _data[rules['date_relevance']['relevance']];
  775. if (dom.name === rules['date_relevance']['relevance']){
  776. if (relevance.value){
  777. let today = getToday();
  778. let tarDate = getSubDate(getToday(),relevance.value);
  779. _data[end.name].value = today;
  780. _data[start.name].value = tarDate;
  781. redenerSearchFormOnData(end.name, tarDate, end.mold);
  782. redenerSearchFormOnData(start.name, tarDate, start.mold);
  783. }
  784. return;
  785. }
  786. if (!start.value || !end.value){
  787. if (rules['date_relevance']['killing'] === 'date'){
  788. _data[start.name].value = '';
  789. redenerSearchFormOnData(start.name, '', start.mold);
  790. _data[start.name].value = '';
  791. redenerSearchFormOnData(end.name, '', end.mold);
  792. }else{
  793. _data[relevance.name].value = '';
  794. redenerSearchFormOnData(relevance.name, '', relevance.mold);
  795. }
  796. }
  797. if (start.value && end.value){
  798. start.value = start.value.replace('+',' ');
  799. end.value = end.value.replace('+',' ');
  800. let diff = getDiffDay(start.value,end.value);
  801. if (rules['date_relevance']['default'] && rules['date_relevance']['default'].includes(diff)){
  802. _data[relevance.name].value = diff;
  803. redenerSearchFormOnData(relevance.name, diff, relevance.mold);}
  804. else {
  805. _data[relevance.name].value = '';
  806. redenerSearchFormOnData(relevance.name, '', relevance.mold);
  807. }
  808. }
  809. }
  810. }
  811. }
  812. function getDiffDay(startDate,endDate) {
  813. startDate = new Date(startDate);
  814. endDate = new Date(endDate);
  815. return Math.floor((endDate.getTime() - startDate.getTime())/(24*3600*1000));
  816. }
  817. function getSubDate(date,value) {
  818. date = new Date(date);
  819. date.setDate(date.getDate() - value);
  820. return dateFormat(date);
  821. }
  822. function getToday() {
  823. let myDate = new Date();
  824. return dateFormat(myDate);
  825. }
  826. function dateFormat(date) {
  827. let year=date.getFullYear();
  828. let month=(date.getMonth()+1)<10 ? '0'+(date.getMonth()+1) : date.getMonth()+1;
  829. let day=date.getDate()<10 ? '0'+date.getDate() : date.getDate();
  830. return year+"-"+month+"-"+day;
  831. }
  832. // save search on cookie
  833. function saveSearchOnCookie() {
  834. let saveData = {};
  835. for (const key in _data) {
  836. if (!['', null, undefined].includes(_data[key].value)) {
  837. if (controlJsType(_data[key].value, 'array') && _data[key].value.length === 0) {
  838. continue;
  839. }
  840. saveData[key] = {
  841. value: _data[key].value,
  842. mold: _data[key].mold,
  843. }
  844. }
  845. }
  846. if (!_this.page) {
  847. saveData['page'] = _this.page;
  848. }
  849. saveCookie(saveData);
  850. }
  851. // hour_minute
  852. function verifyTime(time) {
  853. setTimeout(function () {
  854. time.value = time.value.replace(':', ':');
  855. time.value = time.value.replace(/[a-zA-Z]/, '');
  856. time.value = time.value.replace(/[^\d^\:]/, '');
  857. time.value = time.value.replace(/\:\:/, ':');
  858. time.value = time.value.replace(/^([\d]{1})([\s]{1})/, "$1:");
  859. time.value = time.value.replace(/^([\d]{1})([\d]{1})([\s]{1})/, "$1$2:");
  860. time.value = time.value.replace(/^([\d]{1})([\d]{1})([\d]{1})/, "$1$2:$3");
  861. time.value = time.value.replace(/^([\d]{1})([\d]{1})([\d]{1})([\d]{1})(.*?)/, "$1$2$3$4");
  862. time.value = time.value.replace(/^([\d]{1})([\d]{1})([\d]{1})([\d]{1})/, "$1$2:$3$4");
  863. time.value = time.value.replace(/^([\d]{1})([\d]{1}):([\d]{1})([\d]{1})([\s\S]{1})/, "$1$2:$3$4");
  864. time.value = time.value.replace(/^([\d]{1})([\d]{1}):([\D]{1,99})/, "$1$2:");
  865. time.value = time.value.replace(/^([\d]{1})([\d]{1})([\d]{1})\./, "$1:$2$3");
  866. time.value = time.value.replace(/^([\d]{1})\.([\d]{1})([\d]{1})/, "$1:$2$3");
  867. time.value = time.value.replace(/^([\d]{1})([\d]{2})([\S\s]{1,99})/, "$1$2:$3");
  868. time.value = time.value.replace(/^([\d]{1}):([\d]{2})([\S\s]{1,99})/, "$1:$2");
  869. time.value = time.value.replace(/^([\d])/, "$1");
  870. time.value = time.value.replace(/^([1])([\d]{1})/, "$1$2");
  871. time.value = time.value.replace(/^([3-9])([\d]{1})/, "2$2");
  872. time.value = time.value.replace(/^([2-9]{1})([4-9]{1})/, "$13");
  873. time.value = time.value.replace(/^([\d]{1})([\d]{1}):([6-9]{1})/, "$1$2:5");
  874. time.value = time.value.replace(/^([\d]{1})([\d]{1})$/, "$1$2:");
  875. }, 10);
  876. }
  877. // array duplicate
  878. function arrDuplicate(arr) {
  879. let res = [];
  880. let obj = {};
  881. for (let i = 0; i < arr.length; i++) {
  882. if ([undefined, null, ''].includes(arr[i])) {
  883. continue;
  884. }
  885. if (!obj[arr[i]]) {
  886. obj[arr[i]] = 1;
  887. res.push(arr[i]);
  888. }
  889. }
  890. return res;
  891. }
  892. // get search data
  893. function getSearchData(type = 'array') {
  894. let data = {};
  895. if (type === 'array') {
  896. for (let key in _data) {
  897. if (_data[key].value !== undefined && _data[key].value !== '') {
  898. data[key] = _data[key].value
  899. }
  900. }
  901. } else if (type === 'string') {
  902. data = '';
  903. for (let key in _data) {
  904. if (![undefined, null, ''].includes(_data[key].value)) {
  905. let value = _data[key].value;
  906. if (controlJsType(value, 'string')) {
  907. data = data + "&" + key + "=" + value;
  908. } else if (controlJsType(value, 'array')) {
  909. value.forEach(function (val) {
  910. data = data + "&" + key + "=" + val;
  911. });
  912. }
  913. }
  914. }
  915. }
  916. return data;
  917. }
  918. // renderer input
  919. function rendererSearchFormOnInput(key, value, mold) {
  920. if ([undefined, null, ''].includes(value)) {
  921. $(_form).find("input[name='" + key + "']").val('');
  922. $(_form).find("input[name='" + key + "']").removeClass('bg-warning');
  923. } else {
  924. $(_form).find("input[name='" + key + "']").val(value);
  925. $(_form).find("input[name='" + key + "']").addClass('bg-warning');
  926. }
  927. }
  928. // renderer select
  929. function rendererSearchFormOnSelect(key, value, mold) {
  930. if ([undefined, null, ''].includes(value)) {
  931. $(_form).find("select[name='" + key + "']").val('').removeClass('bg-warning');
  932. if (key === 'paginate') {
  933. $(_form).find("select[name='" + key + "']").val(_this.paginations[0]).removeClass('bg-warning');
  934. }
  935. } else {
  936. $(_form).find("select[name='" + key + "']").val(value);
  937. if (key === 'paginate' && value == _this.paginations[0]) {
  938. return;
  939. }
  940. $(_form).find("select[name='" + key + "']").addClass('bg-warning');
  941. }
  942. }
  943. // renderer check
  944. function rendererSearchFormOnCheck(key, value, mold) {
  945. if ([undefined, null, ''].includes(value)) {
  946. $(_form).find("input[name='" + key + "']").prop("checked", false).removeClass('bg-warning');
  947. } else {
  948. if (controlJsType(value, 'array')) {
  949. value.forEach(function (_value) {
  950. $(_form).find("input[name='" + key + "'][value='" + _value + "']").prop("checked", true).addClass('bg-warning');
  951. })
  952. } else if (controlJsType(value, 'string')) {
  953. $(_form).find("input[name='" + key + "'][value='" + value + "']").prop("checked", true).addClass('bg-warning');
  954. }
  955. }
  956. }
  957. // redener multiple select input
  958. function rendererSearchFormOnMultipleSelect(key, value, mold) {
  959. let ul = $(_form).find("ul[name='" + key + "']");
  960. if ([undefined, null, ''].includes(value) || (controlJsType(value, 'array') && value.length === 0)) {
  961. $(_form).find("input[name='" + key + "_tip']").removeClass('bg-warning');
  962. ul.addClass('bg-warning');
  963. } else if (!!value) {
  964. $(_form).find("input[name='" + key + "_tip']").addClass('bg-warning');
  965. ul.addClass('bg-warning');
  966. } else if (controlJsType(value, 'array') && value.length >= 1) {
  967. $(_form).find("input[name='" + key + "_tip']").addClass('bg-warning');
  968. ul.addClass('bg-warning');
  969. }
  970. redenerUl(ul);
  971. }
  972. // redener multiple select ul
  973. function redenerUl(ul) {
  974. let name = ul.attr('name');
  975. let select = _data[name].select;
  976. if ([null, undefined].includes(select)) {
  977. _data[name].select = [];
  978. select = [];
  979. }
  980. let lis = ul.find('li');
  981. if ([null, undefined, ''].includes(lis)) {
  982. return;
  983. } else {
  984. lis.get().forEach(function (dom) {
  985. if (select.includes($(dom).attr('value'))) {
  986. dom.style.backgroundColor = "#4AA0E6";
  987. $(dom).addClass('active');
  988. } else {
  989. dom.style.backgroundColor = "#FFFFFF";
  990. $(dom).removeClass('active');
  991. }
  992. });
  993. if (lis.length === select.length) {
  994. $("#multipleCheckBox").prop("checked", true);
  995. }
  996. isMultiple(ul.attr('name'));
  997. }
  998. }
  999. function rendererSearchFormOnTime(key, value, mold) {
  1000. if (![undefined, null, ''].includes(value)) {
  1001. $(_form).find("input[name="+key+"]").val(value.substring(0, 10)).addClass('bg-warning');
  1002. if (value.length > 11 + 5) {
  1003. $(_form).find("input[name='" + key + "_tim']").val(value.substring(11, 5));
  1004. }
  1005. } else {
  1006. $(_form).find("input[name="+key+"]").val("").removeClass('bg-warning');
  1007. $(_form).find("input[name='" + key + "_tim']").val("");
  1008. }
  1009. }
  1010. function visibleClearBtn() {
  1011. let bool = false;
  1012. for (const key in _data) {
  1013. if (key === 'paginate') {
  1014. if ((key === 'paginate' && _data[key].value !== _this.paginations[0])) {
  1015. bool = true;
  1016. }
  1017. } else {
  1018. if (![undefined, null, ''].includes(_data[key].value)) {
  1019. if(controlJsType(_data[key].value,'array') && _data[key].value.length === 0 ){
  1020. continue;
  1021. }
  1022. bool = true;
  1023. }
  1024. }
  1025. }
  1026. if (bool) {
  1027. _clearTr.show();
  1028. } else {
  1029. _clearTr.hide();
  1030. }
  1031. }
  1032. function inputKeyDown() {
  1033. $(_form).find('input').keydown(function (e) {
  1034. if (e.keyCode === 13) {
  1035. setTimeout(function () {
  1036. if (_this.keydownSearch) _this.onsubmit();
  1037. if (controlJsType(_this.keydownfun, 'fun')) _this.keydownfun();
  1038. },100);
  1039. }
  1040. });
  1041. $(_form).find("input[type='date']").change(function (e) {
  1042. setTimeout(function () {
  1043. if (_this.keydownSearch)_this.onsubmit();
  1044. if (controlJsType(_this.keydownfun, 'fun')) _this.keydownfun();
  1045. },100);
  1046. });
  1047. }
  1048. function selectChange() {
  1049. $(_form).find('select').change(function () {
  1050. _this.onsubmit();
  1051. })
  1052. }
  1053. function redenerSearchFormOnData(key, value, mold) {
  1054. if (mold === 'input' ||mold === 'dateTime' || mold === 'dateTimeLocal' || mold === 'dateMonth') {
  1055. rendererSearchFormOnInput(key, value, mold);
  1056. } else if (mold === 'select') {
  1057. rendererSearchFormOnSelect(key, value, mold);
  1058. } else if (mold === 'check') {
  1059. rendererSearchFormOnCheck(key, value, mold);
  1060. } else if (mold === 'select_multiple_select') {
  1061. rendererSearchFormOnMultipleSelect(key, value, mold);
  1062. } else if (mold === 'time') {
  1063. rendererSearchFormOnTime(key, value, mold);
  1064. }
  1065. }
  1066. // 数据转换
  1067. function switchData() {
  1068. _this.condition.forEach(function (conditions) {
  1069. conditions.forEach(function (condition) {
  1070. if (['input', 'select', 'dateTime','dateTimeLocal', 'dateMonth', 'search_select', 'time', 'select_multiple_select','checkbox'].includes(condition.type)) {
  1071. if (!condition.select) {
  1072. condition.select = '';
  1073. }
  1074. let data = {
  1075. name: condition.name,
  1076. type: condition.type,
  1077. value: condition.value,
  1078. select: condition.select,
  1079. mold: ['input', 'dateTime'].includes(condition.type) ? 'input' : condition.type,
  1080. data:condition.data,
  1081. killings:condition.killings,
  1082. rules:condition.rules,
  1083. }
  1084. if (condition.type === 'search_select') {
  1085. data.mold = 'select';
  1086. }
  1087. if(condition.type === 'select_multiple_select'){
  1088. data.value = [];
  1089. }
  1090. _data[condition.name] = data;
  1091. } else if (['dateTime_dateTime', 'input_input', 'input_select', 'dateTime_select'].includes(condition.type)) {
  1092. let types = condition.type.split("_");
  1093. if (!condition.select) {
  1094. condition.select = ['', ''];
  1095. }
  1096. if (!condition.value) {
  1097. condition.value = ['', ''];
  1098. }
  1099. condition.name.forEach(function (_name, index) {
  1100. let data = {
  1101. name: _name,
  1102. type: types[index],
  1103. value: condition.value[index],
  1104. select: condition.select[index],
  1105. mold: ['input', 'dateTime'].includes(types[index]) ? 'input' : types[index],
  1106. killings:condition.killings,
  1107. rules:condition.rules,
  1108. }
  1109. if(condition.killings&&Array.isArray(condition.killings)){
  1110. data.killings=condition.killings[index];
  1111. }
  1112. if(condition.rules&&Array.isArray(condition.rules)){
  1113. data.rules=condition.rules[index];
  1114. }
  1115. _data[_name] = data;
  1116. })
  1117. }
  1118. })
  1119. })
  1120. }
  1121. // redererSearchForm on cookie
  1122. function rendererSearchForm() {
  1123. let url = document.referrer;
  1124. let partt = /(Edit\/)|(\/eidt)|(eidt\/)/i;
  1125. if (url.search(partt) > 0) {
  1126. rendererOptionOnCookie();
  1127. } else {
  1128. rendererOptionOnUrl();
  1129. }
  1130. }
  1131. function rendererOptionOnCookie() {
  1132. let data = fetchCookie();
  1133. if (data === undefined) {
  1134. return;
  1135. }
  1136. data = JSON.parse(data);
  1137. for (let key in data) {
  1138. _data[key].value = data[key].value;
  1139. _data[key].mold = data[key].mold;
  1140. _data[key].select = data[key].select;
  1141. }
  1142. rendererSearchFormOn_data();
  1143. }
  1144. function rendererOptionOnUrl() {
  1145. if (!window.location.search) {
  1146. return;
  1147. }
  1148. searchOptionToUrlsearch();
  1149. rendererSearchFormOn_data();
  1150. }
  1151. function rendererSearchFormOn_data() {
  1152. for (let key in _data) {
  1153. let value = _data[key].value, type = _data[key].type, mold = _data[key].mold;
  1154. // if(value){
  1155. // let string = new String(value);
  1156. // if(string.length > 1 ){
  1157. // string = string.replace(/\+/g,' ');
  1158. // value = string
  1159. // }
  1160. // }
  1161. _data[key].select = value;
  1162. if (['input', 'dateTime', 'dateTimeLocal', 'dateMonth'].includes(mold)) {
  1163. rendererSearchFormOnInput(key, value, mold);
  1164. } else if (['select'].includes(mold)) {
  1165. rendererSearchFormOnSelect(key, value, mold);
  1166. } else if (['checkbox'].includes(mold)) {
  1167. rendererSearchFormOnCheck(key, value, mold);
  1168. } else if (['select_multiple_select'].includes(mold)) {
  1169. if (controlJsType(value, ['array', 'string'])) {
  1170. let ul = $(_form).find("ul[name='" + key + "']");
  1171. redenerUl(ul);
  1172. rendererSearchFormOnMultipleSelect(key, value, mold);
  1173. }
  1174. } else if (['time'].includes(type) && value !== undefined) {
  1175. $(_form).find("input[name='" + key + "']").val(value.substr(0, 10)).addClass('bg-warning');
  1176. $(_form).find("input[id='" + key + "_min']").val(value.substr(11, value.length));
  1177. }
  1178. }
  1179. if ([undefined, null, ''].includes(data['paginate'])) {
  1180. return;
  1181. }
  1182. if ([undefined, null, ''].includes(data['paginate'].value)) {
  1183. $(_form).find("select[name='paginate']").val(_this.paginations[0]);
  1184. } else {
  1185. $(_form).find("select[name='paginate']").val(data['paginate'].value);
  1186. }
  1187. }
  1188. // url path on search form option renderer
  1189. function searchOptionToUrlsearch() {
  1190. let data = (window.location.search);
  1191. data = decodeURIComponent(decodeURIComponent(data));
  1192. //data = decodeURIComponent(data).replace('%20'," ");
  1193. if (!!data) {
  1194. data = (data.substr(1)).split('&');
  1195. data = convertArrayToObj(data);
  1196. for (const key in data) {
  1197. if(!data[key] || _this.param[key]){
  1198. continue;
  1199. }
  1200. if (key === 'paginate') {
  1201. _data[key] = {
  1202. name: key,
  1203. value: data[key],
  1204. type: 'select',
  1205. mold: 'select'
  1206. }
  1207. } else if (key === 'page') {
  1208. _page = data[key];
  1209. } else if(_data[key].type === 'select_multiple_select' ){
  1210. if(!_data[key]){
  1211. _data[key].value = [];
  1212. }else{
  1213. _data[key].value = data[key].split(',');
  1214. }
  1215. }else {
  1216. _data[key].value = data[key];
  1217. }
  1218. }
  1219. }
  1220. }
  1221. // cookie -> data
  1222. function set_dataOnCookie() {
  1223. let data = fetchCookie();
  1224. data = JSON.parse(data);
  1225. if (!data) {
  1226. return;
  1227. }
  1228. for (const key in data) {
  1229. if (!!data[key]) {
  1230. _data[key].value = data[key].value;
  1231. }
  1232. }
  1233. }
  1234. // array - > object
  1235. function convertArrayToObj(arr) {
  1236. if (!arr || !controlJsType(arr, 'array')) {
  1237. return {};
  1238. } else if (controlJsType(arr, 'array') && arr.length > 0) {
  1239. let data = {};
  1240. arr.forEach(function (map) {
  1241. let arr = map.split('='), key = arr[0], value = arr[1];
  1242. if (!data[key]) {
  1243. data[key] = value;
  1244. } else {
  1245. if (controlJsType(data[key], 'string')) {
  1246. data[key] = [data[key], value];
  1247. } else if (controlJsType(data[key], 'array')) {
  1248. data[key].push(value);
  1249. }
  1250. }
  1251. })
  1252. return data;
  1253. }
  1254. }
  1255. // paginate
  1256. function pervPage() {
  1257. let obj = getSearchData();
  1258. if(!!obj['page'] && obj['page']>1){
  1259. obj['page'] = obj['page']-1;
  1260. }
  1261. return getSearchUri(obj);
  1262. }
  1263. function nextPage() {
  1264. let obj = getSearchData();
  1265. if(!!obj['page'] ){
  1266. obj['page'] = obj['page']+1;
  1267. }else{
  1268. obj['page'] = 2;
  1269. }
  1270. if(!obj['paginate']){
  1271. obj['paginate'] = 50;
  1272. }
  1273. return getSearchUri(obj);
  1274. }
  1275. function getSearchObj() {
  1276. let data = window.location.search;
  1277. data = decodeURIComponent(data);
  1278. if(!data){
  1279. return {};
  1280. }
  1281. let saveData = {};
  1282. data = data.substr(1).split('&');
  1283. data.forEach(function (map) {
  1284. let arr = map.split('&'),key = arr[0],value = arr[1];
  1285. if(!!value){
  1286. if (!saveData[key]) {
  1287. saveData[key] = value;
  1288. } else {
  1289. if (controlJsType(data[key], 'string')) {
  1290. saveData[key] = [saveData[key] , value];
  1291. } else if (controlJsType(data[key], 'array')) {
  1292. saveData[key].push(value);
  1293. }
  1294. }
  1295. }
  1296. })
  1297. return saveData;
  1298. }
  1299. function getPage(){
  1300. return getSearchObj()['page'];
  1301. }
  1302. // paginate
  1303. function pervPage() {
  1304. let obj = getSearchData();
  1305. _page = getPage();
  1306. if(!obj['paginate']){
  1307. obj['paginate'] = 50;
  1308. }
  1309. if(Number(_page)>1){
  1310. _page= Number(_page)-1;
  1311. }else{
  1312. _page = 1;
  1313. }
  1314. return getSearchUri(obj);
  1315. }
  1316. function nextPage() {
  1317. let obj = getSearchData();
  1318. _page = getPage();
  1319. if(!_page){_page=1;}
  1320. if(_page){
  1321. _page = Number(_page)+ 1;
  1322. }
  1323. if(!obj['paginate']){
  1324. obj['paginate'] = 50;
  1325. }
  1326. return getSearchUri(obj);
  1327. }
  1328. function goPage(page){
  1329. let obj = getSearchData();
  1330. _page = page;
  1331. return getSearchUri(obj);
  1332. }
  1333. function getSearchUri(obj){
  1334. let string = "?";
  1335. for (const key in obj) {
  1336. if(controlJsType(obj[key],['string','number'])){
  1337. string+= key + "=" + obj[key]+'&';
  1338. }else if( controlJsType(obj[key],'array')&&obj[key].length > 0){
  1339. obj[key].forEach(function(value){
  1340. string+=key + "=" + value+'&';
  1341. })
  1342. }
  1343. }
  1344. return string;
  1345. }
  1346. };