queryForm.js 63 KB

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