queryForm.js 62 KB

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