queryForm.js 62 KB

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