queryForm.js 61 KB

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