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.hide();
  636. ul_div.mouseleave(function () {
  637. if (_data[condition.name].value.length > 0) {
  638. // _this.onsubmit();
  639. ul_div.hide();
  640. return;
  641. }
  642. if (!isMouseenterCheckBox) {
  643. ul_div.hide();
  644. }
  645. });
  646. return div;
  647. }
  648. // checkbox
  649. function getCheckBox(condition) {
  650. let div1 = $("<div class='form-inline'></div>")
  651. condition.data.forEach(function (map, index) {
  652. let div = $('<div class="custom-control custom-control-inline custom-checkbox"></div>');
  653. let checkbox = $("<input type='checkbox' name='" + condition.name + "' class='custom-control-input' id='" + condition.name + index + "' value='" + map.name + "' >");
  654. let label = $("<label class='custom-control-label text-left font-weight-norma' for='" + condition.name + index + "'>" + map.value + "</label>")
  655. div1.append(div.append(checkbox).append(label));
  656. checkbox.click(function () {
  657. let value = [];
  658. $.each($("input:checkbox[name='" + condition.name + "']:checked"), function () {
  659. value.push($(this).val());
  660. })
  661. let dom = {name: condition.name, type: 'checkbox', value: value, mold: 'check'};
  662. modifyData(dom);
  663. _this.onsubmit();
  664. });
  665. })
  666. return div1;
  667. }
  668. // fill select
  669. function fillSelectOption(select, data, placeholder = '') {
  670. select.html('');
  671. if (![undefined, null].includes(placeholder)) {
  672. let option = $("<option value></option>");
  673. if ('' === placeholder) {
  674. option = $("<option value>" + '全部' + placeholder + "</option>");
  675. } else {
  676. option = $("<option value>" + placeholder + "</option>");
  677. }
  678. select.append(option);
  679. }
  680. data.forEach(function (map) {
  681. let option = $("<option value='" + map.name + "'>" + map.value + "</optio>");
  682. select.append(option);
  683. })
  684. if (data !== undefined) {
  685. }
  686. }
  687. // fill multiple select ul
  688. function fillMultipleSelectOption(ul, data, name) {
  689. ul.html('');
  690. data.forEach(function (map) {
  691. let span = $("<span style='cursor:default'>" + map.value + "</span>");
  692. let li = $("<li class='list-group-item pt-0 pb-0' style='z-index:95' value='" + map.name + "'></li>");
  693. li.append(span);
  694. ul.append(li);
  695. li.click(function () {
  696. let value = li.attr('value');
  697. let dom_data = _data[name].value;
  698. if (!dom_data) {
  699. dom_data = [];
  700. }
  701. if (controlJsType(dom_data, 'string')) {
  702. dom_data = [dom_data];
  703. }
  704. if (dom_data.includes(value)) {
  705. dom_data.splice(dom_data.indexOf(value), 1);
  706. } else {
  707. dom_data.push(value);
  708. }
  709. dom_data = arrDuplicate(dom_data);
  710. let dom = {
  711. name: ul.attr('name'),
  712. type: 'select_multiple_select',
  713. value: dom_data,
  714. select: dom_data,
  715. mold: 'select_multiple_select'
  716. };
  717. modifyData(dom);
  718. redenerUl(ul);
  719. isMultiple(ul.attr('name'));
  720. })
  721. })
  722. }
  723. function isMultiple(name) {
  724. let label = $('#' + name + '_lab');
  725. let select = $('#' + name + '_sel');
  726. let dom_data = _data[name].value;
  727. if (dom_data && dom_data.length === 1) {
  728. select.show();
  729. select.val(dom_data[0]);
  730. label.hide();
  731. } else if (controlJsType(dom_data, 'string')) {
  732. select.show();
  733. select.val(dom_data);
  734. label.hide();
  735. } else if (dom_data.length >= 2) {
  736. select.hide();
  737. label.show();
  738. }
  739. }
  740. // modify _data
  741. function modifyData(dom) {
  742. _data[dom.name].mold = dom.mold;
  743. _data[dom.name].value = dom.value;
  744. _data[dom.name].select = dom.value;
  745. redenerSearchFormOnData(dom.name, dom.value, dom.mold);
  746. let killings = _data[dom.name].killings;
  747. if (killings) {
  748. killings.forEach(function (targetName) {
  749. _data[targetName].value = '';
  750. redenerSearchFormOnData(_data[targetName].name, '', _data[targetName].mold);
  751. })
  752. }
  753. /* rules 规则
  754. * son 子级元素 对象形式多传递 子级元素不存在时且 required_without_all_if 忽略列表内元素皆无值时使子级元素设置默认值
  755. * {son:{key:{required_without_all_if:[],default:''}}}
  756. * date_relevance 时间关联 对象形式传递 date为起终时间 relevance为关联元素name
  757. * killing为标记干掉哪种类型元素(enum('date','relevance')) default为relevance列表值,时间差存在于该值中才会去选中
  758. * {date_relevance:{date:['',''],relevance:'',killing:'',default:[]}}
  759. * */
  760. let rules = _data[dom.name].rules;
  761. if (rules && JSON.stringify(rules) !== '{}') {
  762. if (rules['son']) {
  763. for (let key in rules['son']) {
  764. if (!dom.value) {
  765. redenerSearchFormOnData(_data[key].name, '', _data[key].mold);
  766. continue;
  767. }
  768. if (!_data[key].value) {
  769. if (rules['son'][key].required_without_all_if && Array.isArray(rules['son'][key].required_without_all_if)) {
  770. let sign = true;
  771. rules['son'][key].required_without_all_if.some(function (name) {
  772. if (_data[name].value) {
  773. sign = false;
  774. return true;
  775. }
  776. });
  777. if (sign) {
  778. _data[_data[key].name].mold = _data[key].mold;
  779. _data[_data[key].name].value = rules['son'][key].default;
  780. _data[_data[key].name].select = rules['son'][key].default;
  781. redenerSearchFormOnData(_data[key].name, rules['son'][key].default, _data[key].mold);
  782. }
  783. }
  784. }
  785. }
  786. }
  787. if (rules['date_relevance']) {
  788. if (!rules['date_relevance']['date']
  789. || !Array.isArray(rules['date_relevance']['date'])
  790. || rules['date_relevance']['date'].length !== 2
  791. || !rules['date_relevance']['relevance']) return;
  792. let start = _data[rules['date_relevance']['date'][0]];
  793. let end = _data[rules['date_relevance']['date'][1]];
  794. let relevance = _data[rules['date_relevance']['relevance']];
  795. if (dom.name === rules['date_relevance']['relevance']) {
  796. if (relevance.value) {
  797. let today = getToday();
  798. let tarDate = getSubDate(getToday(), relevance.value);
  799. _data[end.name].value = today + " 23:59";
  800. _data[start.name].value = tarDate + " 00:00";
  801. /*redenerSearchFormOnData(end.name, tarDate+" 23:59", end.mold);
  802. redenerSearchFormOnData(start.name, tarDate+" 00:00", start.mold);*/
  803. }
  804. return;
  805. }
  806. if (!start.value || !end.value) {
  807. if (rules['date_relevance']['killing'] === 'date') {
  808. _data[start.name].value = '';
  809. redenerSearchFormOnData(start.name, '', start.mold);
  810. _data[start.name].value = '';
  811. redenerSearchFormOnData(end.name, '', end.mold);
  812. } else {
  813. _data[relevance.name].value = '';
  814. redenerSearchFormOnData(relevance.name, '', relevance.mold);
  815. }
  816. }
  817. if (start.value && end.value) {
  818. start.value = start.value.replace('+', ' ');
  819. end.value = end.value.replace('+', ' ');
  820. let diff = getDiffDay(start.value, end.value);
  821. if (rules['date_relevance']['default'] && rules['date_relevance']['default'].includes(diff)) {
  822. _data[relevance.name].value = diff;
  823. redenerSearchFormOnData(relevance.name, diff, relevance.mold);
  824. } else {
  825. _data[relevance.name].value = '';
  826. redenerSearchFormOnData(relevance.name, '', relevance.mold);
  827. }
  828. }
  829. }
  830. }
  831. }
  832. function getDiffDay(startDate, endDate) {
  833. startDate = new Date(startDate);
  834. endDate = new Date(endDate);
  835. return Math.floor((endDate.getTime() - startDate.getTime()) / (24 * 3600 * 1000));
  836. }
  837. function getSubDate(date, value) {
  838. date = new Date(date);
  839. date.setDate(date.getDate() - value);
  840. return dateFormat(date);
  841. }
  842. function getToday() {
  843. let myDate = new Date();
  844. return dateFormat(myDate);
  845. }
  846. function dateFormat(date) {
  847. let year = date.getFullYear();
  848. let month = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
  849. let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  850. return year + "-" + month + "-" + day;
  851. }
  852. // save search on cookie
  853. function saveSearchOnCookie() {
  854. let saveData = {};
  855. for (const key in _data) {
  856. if (!['', null, undefined].includes(_data[key].value)) {
  857. if (controlJsType(_data[key].value, 'array') && _data[key].value.length === 0) {
  858. continue;
  859. }
  860. saveData[key] = {
  861. value: _data[key].value,
  862. mold: _data[key].mold,
  863. }
  864. }
  865. }
  866. if (!_this.page) {
  867. saveData['page'] = _this.page;
  868. }
  869. saveCookie(saveData);
  870. }
  871. // hour_minute
  872. function verifyTime(time) {
  873. setTimeout(function () {
  874. time.value = time.value.replace(':', ':');
  875. time.value = time.value.replace(/[a-zA-Z]/, '');
  876. time.value = time.value.replace(/[^\d^\:]/, '');
  877. time.value = time.value.replace(/\:\:/, ':');
  878. time.value = time.value.replace(/^([\d]{1})([\s]{1})/, "$1:");
  879. time.value = time.value.replace(/^([\d]{1})([\d]{1})([\s]{1})/, "$1$2:");
  880. time.value = time.value.replace(/^([\d]{1})([\d]{1})([\d]{1})/, "$1$2:$3");
  881. time.value = time.value.replace(/^([\d]{1})([\d]{1})([\d]{1})([\d]{1})(.*?)/, "$1$2$3$4");
  882. time.value = time.value.replace(/^([\d]{1})([\d]{1})([\d]{1})([\d]{1})/, "$1$2:$3$4");
  883. time.value = time.value.replace(/^([\d]{1})([\d]{1}):([\d]{1})([\d]{1})([\s\S]{1})/, "$1$2:$3$4");
  884. time.value = time.value.replace(/^([\d]{1})([\d]{1}):([\D]{1,99})/, "$1$2:");
  885. time.value = time.value.replace(/^([\d]{1})([\d]{1})([\d]{1})\./, "$1:$2$3");
  886. time.value = time.value.replace(/^([\d]{1})\.([\d]{1})([\d]{1})/, "$1:$2$3");
  887. time.value = time.value.replace(/^([\d]{1})([\d]{2})([\S\s]{1,99})/, "$1$2:$3");
  888. time.value = time.value.replace(/^([\d]{1}):([\d]{2})([\S\s]{1,99})/, "$1:$2");
  889. time.value = time.value.replace(/^([\d])/, "$1");
  890. time.value = time.value.replace(/^([1])([\d]{1})/, "$1$2");
  891. time.value = time.value.replace(/^([3-9])([\d]{1})/, "2$2");
  892. time.value = time.value.replace(/^([2-9]{1})([4-9]{1})/, "$13");
  893. time.value = time.value.replace(/^([\d]{1})([\d]{1}):([6-9]{1})/, "$1$2:5");
  894. time.value = time.value.replace(/^([\d]{1})([\d]{1})$/, "$1$2:");
  895. }, 10);
  896. }
  897. // array duplicate
  898. function arrDuplicate(arr) {
  899. let res = [];
  900. let obj = {};
  901. for (let i = 0; i < arr.length; i++) {
  902. if ([undefined, null, ''].includes(arr[i])) {
  903. continue;
  904. }
  905. if (!obj[arr[i]]) {
  906. obj[arr[i]] = 1;
  907. res.push(arr[i]);
  908. }
  909. }
  910. return res;
  911. }
  912. // get search data
  913. function getSearchData(type = 'array') {
  914. let data = {};
  915. if (type === 'array') {
  916. for (let key in _data) {
  917. if (_data[key].value !== undefined && _data[key].value !== '') {
  918. data[key] = _data[key].value
  919. }
  920. }
  921. } else if (type === 'string') {
  922. data = '';
  923. for (let key in _data) {
  924. if (![undefined, null, ''].includes(_data[key].value)) {
  925. let value = _data[key].value;
  926. if (controlJsType(value, 'string')) {
  927. data = data + "&" + key + "=" + value;
  928. } else if (controlJsType(value, 'array')) {
  929. value.forEach(function (val) {
  930. data = data + "&" + key + "=" + val;
  931. });
  932. }
  933. }
  934. }
  935. }
  936. return data;
  937. }
  938. // renderer input
  939. function rendererSearchFormOnInput(key, value, mold) {
  940. if ([undefined, null, ''].includes(value)) {
  941. $(_form).find("input[name='" + key + "']").val('');
  942. $(_form).find("input[name='" + key + "']").removeClass('bg-warning');
  943. } else {
  944. $(_form).find("input[name='" + key + "']").val(value);
  945. $(_form).find("input[name='" + key + "']").addClass('bg-warning');
  946. }
  947. }
  948. // renderer select
  949. function rendererSearchFormOnSelect(key, value, mold) {
  950. if ([undefined, null, ''].includes(value)) {
  951. $(_form).find("select[name='" + key + "']").val('').removeClass('bg-warning');
  952. if (key === 'paginate') {
  953. $(_form).find("select[name='" + key + "']").val(_this.paginations[0]).removeClass('bg-warning');
  954. }
  955. } else {
  956. $(_form).find("select[name='" + key + "']").val(value);
  957. if (key === 'paginate' && value == _this.paginations[0]) {
  958. return;
  959. }
  960. $(_form).find("select[name='" + key + "']").addClass('bg-warning');
  961. }
  962. }
  963. // renderer check
  964. function rendererSearchFormOnCheck(key, value, mold) {
  965. if ([undefined, null, ''].includes(value)) {
  966. $(_form).find("input[name='" + key + "']").prop("checked", false).removeClass('bg-warning');
  967. } else {
  968. if (controlJsType(value, 'array')) {
  969. value.forEach(function (_value) {
  970. $(_form).find("input[name='" + key + "'][value='" + _value + "']").prop("checked", true).addClass('bg-warning');
  971. })
  972. } else if (controlJsType(value, 'string')) {
  973. $(_form).find("input[name='" + key + "'][value='" + value + "']").prop("checked", true).addClass('bg-warning');
  974. }
  975. }
  976. }
  977. // redener multiple select input
  978. function rendererSearchFormOnMultipleSelect(key, value, mold) {
  979. let ul = $(_form).find("ul[name='" + key + "']");
  980. if ([undefined, null, ''].includes(value) || (controlJsType(value, 'array') && value.length === 0)) {
  981. $(_form).find("input[name='" + key + "_tip']").removeClass('bg-warning');
  982. ul.addClass('bg-warning');
  983. } else if (!!value) {
  984. $(_form).find("input[name='" + key + "_tip']").addClass('bg-warning');
  985. ul.addClass('bg-warning');
  986. } else if (controlJsType(value, 'array') && value.length >= 1) {
  987. $(_form).find("input[name='" + key + "_tip']").addClass('bg-warning');
  988. ul.addClass('bg-warning');
  989. }
  990. redenerUl(ul);
  991. }
  992. // redener multiple select ul
  993. function redenerUl(ul) {
  994. let name = ul.attr('name');
  995. let select = _data[name].select;
  996. if ([null, undefined].includes(select)) {
  997. _data[name].select = [];
  998. select = [];
  999. }
  1000. let lis = ul.find('li');
  1001. if ([null, undefined, ''].includes(lis)) {
  1002. return;
  1003. }
  1004. lis.get().forEach(dom=>{
  1005. let value = $(dom).attr('value');
  1006. if (select.includes(value)){
  1007. dom.style.backgroundColor = "#4AA0E6";
  1008. $(dom).addClass('active');
  1009. } else {
  1010. dom.style.backgroundColor = "#FFFFFF";
  1011. $(dom).removeClass('active');
  1012. }
  1013. })
  1014. if (lis.length === select.length) {
  1015. $("#multipleCheckBox").prop("checked", true);
  1016. }
  1017. isMultiple(ul.attr('name'));
  1018. }
  1019. function rendererSearchFormOnTime(key, value, mold) {
  1020. if (![undefined, null, ''].includes(value)) {
  1021. $(_form).find("input[name=" + key + "]").val(value.substring(0, 10)).addClass('bg-warning');
  1022. if (value.length > 11 + 5) {
  1023. $(_form).find("input[name='" + key + "_tim']").val(value.substring(11, 5));
  1024. }
  1025. } else {
  1026. $(_form).find("input[name=" + key + "]").val("").removeClass('bg-warning');
  1027. $(_form).find("input[name='" + key + "_tim']").val("");
  1028. }
  1029. }
  1030. function visibleClearBtn() {
  1031. let bool = false;
  1032. for (const key in _data) {
  1033. if (key === 'paginate') {
  1034. if ((key === 'paginate' && _data[key].value !== _this.paginations[0])) {
  1035. bool = true;
  1036. }
  1037. } else {
  1038. if (![undefined, null, ''].includes(_data[key].value)) {
  1039. if (controlJsType(_data[key].value, 'array') && _data[key].value.length === 0) {
  1040. continue;
  1041. }
  1042. bool = true;
  1043. }
  1044. }
  1045. }
  1046. if (bool) {
  1047. _clearTr.show();
  1048. } else {
  1049. _clearTr.hide();
  1050. }
  1051. }
  1052. function inputKeyDown() {
  1053. $(_form).find('input').keydown(function (e) {
  1054. if (e.keyCode === 13) {
  1055. setTimeout(function () {
  1056. if (_this.keydownSearch) _this.onsubmit();
  1057. if (controlJsType(_this.keydownfun, 'fun')) _this.keydownfun();
  1058. }, 100);
  1059. }
  1060. });
  1061. $(_form).find("input[type='date']").change(function (e) {
  1062. setTimeout(function () {
  1063. // if (_this.keydownSearch)_this.onsubmit();
  1064. if (controlJsType(_this.keydownfun, 'fun')) _this.keydownfun();
  1065. }, 100);
  1066. });
  1067. }
  1068. function selectChange() {
  1069. // $(_form).find('select').change(function () {
  1070. // _this.onsubmit();
  1071. // })
  1072. }
  1073. function redenerSearchFormOnData(key, value, mold) {
  1074. if (mold === 'input' || mold === 'dateTime' || mold === 'dateTimeLocal' || mold === 'dateMonth') {
  1075. rendererSearchFormOnInput(key, value, mold);
  1076. } else if (mold === 'select') {
  1077. rendererSearchFormOnSelect(key, value, mold);
  1078. } else if (mold === 'check') {
  1079. rendererSearchFormOnCheck(key, value, mold);
  1080. } else if (mold === 'select_multiple_select') {
  1081. rendererSearchFormOnMultipleSelect(key, value, mold);
  1082. } else if (mold === 'time') {
  1083. rendererSearchFormOnTime(key, value, mold);
  1084. }
  1085. }
  1086. // 数据转换
  1087. function switchData() {
  1088. _this.condition.forEach(function (conditions) {
  1089. conditions.forEach(function (condition) {
  1090. if (['input', 'select', 'dateTime', 'dateTimeLocal', 'dateMonth', 'search_select', 'time', 'select_multiple_select', 'checkbox'].includes(condition.type)) {
  1091. if (!condition.select) {
  1092. condition.select = '';
  1093. }
  1094. let data = {
  1095. name: condition.name,
  1096. type: condition.type,
  1097. value: condition.value,
  1098. select: condition.select,
  1099. mold: ['input', 'dateTime'].includes(condition.type) ? 'input' : condition.type,
  1100. data: condition.data,
  1101. killings: condition.killings,
  1102. rules: condition.rules,
  1103. required: condition.required,
  1104. }
  1105. if (condition.type === 'search_select') {
  1106. data.mold = 'select';
  1107. }
  1108. if (condition.type === 'select_multiple_select') {
  1109. data.value = [];
  1110. }
  1111. _data[condition.name] = data;
  1112. } else if (['dateTime_dateTime', 'input_input', 'input_select', 'dateTime_select'].includes(condition.type)) {
  1113. let types = condition.type.split("_");
  1114. if (!condition.select) {
  1115. condition.select = ['', ''];
  1116. }
  1117. if (!condition.value) {
  1118. condition.value = ['', ''];
  1119. }
  1120. condition.name.forEach(function (_name, index) {
  1121. let data = {
  1122. name: _name,
  1123. type: types[index],
  1124. value: condition.value[index],
  1125. select: condition.select[index],
  1126. mold: ['input', 'dateTime'].includes(types[index]) ? 'input' : types[index],
  1127. killings: condition.killings,
  1128. rules: condition.rules,
  1129. required: condition.required,
  1130. }
  1131. if (condition.killings && Array.isArray(condition.killings)) {
  1132. data.killings = condition.killings[index];
  1133. }
  1134. if (condition.rules && Array.isArray(condition.rules)) {
  1135. data.rules = condition.rules[index];
  1136. }
  1137. _data[_name] = data;
  1138. })
  1139. }
  1140. })
  1141. })
  1142. }
  1143. // redererSearchForm on cookie
  1144. function rendererSearchForm() {
  1145. let url = document.referrer;
  1146. let partt = /(Edit\/)|(\/eidt)|(eidt\/)/i;
  1147. if (url.search(partt) > 0) {
  1148. rendererOptionOnCookie();
  1149. } else {
  1150. rendererOptionOnUrl();
  1151. }
  1152. }
  1153. function rendererOptionOnCookie() {
  1154. let data = fetchCookie();
  1155. if (data === undefined) {
  1156. return;
  1157. }
  1158. data = JSON.parse(data);
  1159. for (let key in data) {
  1160. _data[key].value = data[key].value;
  1161. _data[key].mold = data[key].mold;
  1162. _data[key].select = data[key].select;
  1163. }
  1164. rendererSearchFormOn_data();
  1165. }
  1166. function rendererOptionOnUrl() {
  1167. if (!window.location.search) {
  1168. return;
  1169. }
  1170. searchOptionToUrlsearch();
  1171. rendererSearchFormOn_data();
  1172. }
  1173. this.isRender = function isRender(){
  1174. let bool = false;
  1175. for (let key in _data) {
  1176. let {value,name} = _data[key];
  1177. if(name === 'paginate') continue;
  1178. if(controlJsType(value,'array') && value.length > 0){
  1179. bool = true;
  1180. }
  1181. if(controlJsType(value,'string') && value !== ""){
  1182. bool = true;
  1183. }
  1184. }
  1185. return bool;
  1186. }
  1187. function rendererSearchFormOn_data() {
  1188. for (let key in _data) {
  1189. let value = _data[key].value, type = _data[key].type, mold = _data[key].mold;
  1190. // if(value){
  1191. // let string = new String(value);
  1192. // if(string.length > 1 ){
  1193. // string = string.replace(/\+/g,' ');
  1194. // value = string
  1195. // }
  1196. // }
  1197. _data[key].select = value;
  1198. if (['input', 'dateTime', 'dateTimeLocal', 'dateMonth'].includes(mold)) {
  1199. rendererSearchFormOnInput(key, value, mold);
  1200. } else if (['select'].includes(mold)) {
  1201. rendererSearchFormOnSelect(key, value, mold);
  1202. } else if (['checkbox'].includes(mold)) {
  1203. rendererSearchFormOnCheck(key, value, mold);
  1204. } else if (['select_multiple_select'].includes(mold)) {
  1205. if (controlJsType(value, ['array', 'string'])) {
  1206. let ul = $(_form).find("ul[name='" + key + "']");
  1207. redenerUl(ul);
  1208. rendererSearchFormOnMultipleSelect(key, value, mold);
  1209. }
  1210. } else if (['time'].includes(type) && value !== undefined) {
  1211. $(_form).find("input[name='" + key + "']").val(value.substr(0, 10)).addClass('bg-warning');
  1212. $(_form).find("input[id='" + key + "_min']").val(value.substr(11, value.length));
  1213. }
  1214. }
  1215. if ([undefined, null, ''].includes(data['paginate'])) {
  1216. return;
  1217. }
  1218. if ([undefined, null, ''].includes(data['paginate'].value)) {
  1219. $(_form).find("select[name='paginate']").val(_this.paginations[0]);
  1220. } else {
  1221. $(_form).find("select[name='paginate']").val(data['paginate'].value);
  1222. }
  1223. }
  1224. // url path on search form option renderer
  1225. function searchOptionToUrlsearch() {
  1226. let data = (window.location.search);
  1227. data = decodeURIComponent(decodeURIComponent(data));
  1228. //data = decodeURIComponent(data).replace('%20'," ");
  1229. if (!!data) {
  1230. data = (data.substr(1)).split('&');
  1231. data = convertArrayToObj(data);
  1232. for (const key in data) {
  1233. if (!data[key] || _this.param[key]) {
  1234. continue;
  1235. }
  1236. if (key === 'paginate') {
  1237. _data[key] = {
  1238. name: key,
  1239. value: data[key],
  1240. type: 'select',
  1241. mold: 'select'
  1242. }
  1243. } else if (key === 'page') {
  1244. _page = data[key];
  1245. } else if (_data[key].type === 'select_multiple_select') {
  1246. if (!_data[key]) {
  1247. _data[key].value = [];
  1248. } else {
  1249. _data[key].value = data[key].split(',');
  1250. }
  1251. } else {
  1252. _data[key].value = data[key];
  1253. }
  1254. }
  1255. }
  1256. }
  1257. // cookie -> data
  1258. function set_dataOnCookie() {
  1259. let data = fetchCookie();
  1260. data = JSON.parse(data);
  1261. if (!data) {
  1262. return;
  1263. }
  1264. for (const key in data) {
  1265. if (!!data[key]) {
  1266. _data[key].value = data[key].value;
  1267. }
  1268. }
  1269. }
  1270. // array - > object
  1271. function convertArrayToObj(arr) {
  1272. if (!arr || !controlJsType(arr, 'array')) {
  1273. return {};
  1274. } else if (controlJsType(arr, 'array') && arr.length > 0) {
  1275. let data = {};
  1276. arr.forEach(function (map) {
  1277. let arr = map.split('='), key = arr[0], value = arr[1];
  1278. if (!data[key]) {
  1279. data[key] = value;
  1280. } else {
  1281. if (controlJsType(data[key], 'string')) {
  1282. data[key] = [data[key], value];
  1283. } else if (controlJsType(data[key], 'array')) {
  1284. data[key].push(value);
  1285. }
  1286. }
  1287. })
  1288. return data;
  1289. }
  1290. }
  1291. // paginate
  1292. function pervPage() {
  1293. let obj = getSearchData();
  1294. if (!!obj['page'] && obj['page'] > 1) {
  1295. obj['page'] = obj['page'] - 1;
  1296. }
  1297. return getSearchUri(obj);
  1298. }
  1299. function nextPage() {
  1300. let obj = getSearchData();
  1301. if (!!obj['page']) {
  1302. obj['page'] = obj['page'] + 1;
  1303. } else {
  1304. obj['page'] = 2;
  1305. }
  1306. if (!obj['paginate']) {
  1307. obj['paginate'] = 50;
  1308. }
  1309. return getSearchUri(obj);
  1310. }
  1311. function getSearchObj() {
  1312. let data = window.location.search;
  1313. data = decodeURIComponent(data);
  1314. if (!data) {
  1315. return {};
  1316. }
  1317. let saveData = {};
  1318. data = data.substr(1).split('&');
  1319. data.forEach(function (map) {
  1320. let arr = map.split('&'), key = arr[0], value = arr[1];
  1321. if (!!value) {
  1322. if (!saveData[key]) {
  1323. saveData[key] = value;
  1324. } else {
  1325. if (controlJsType(data[key], 'string')) {
  1326. saveData[key] = [saveData[key], value];
  1327. } else if (controlJsType(data[key], 'array')) {
  1328. saveData[key].push(value);
  1329. }
  1330. }
  1331. }
  1332. })
  1333. return saveData;
  1334. }
  1335. function getPage() {
  1336. return getSearchObj()['page'];
  1337. }
  1338. // paginate
  1339. function pervPage() {
  1340. let obj = getSearchData();
  1341. _page = getPage();
  1342. if (!obj['paginate']) {
  1343. obj['paginate'] = 50;
  1344. }
  1345. if (Number(_page) > 1) {
  1346. _page = Number(_page) - 1;
  1347. } else {
  1348. _page = 1;
  1349. }
  1350. return getSearchUri(obj);
  1351. }
  1352. function nextPage() {
  1353. let obj = getSearchData();
  1354. _page = getPage();
  1355. if (!_page) {
  1356. _page = 1;
  1357. }
  1358. if (_page) {
  1359. _page = Number(_page) + 1;
  1360. }
  1361. if (!obj['paginate']) {
  1362. obj['paginate'] = 50;
  1363. }
  1364. return getSearchUri(obj);
  1365. }
  1366. function goPage(page) {
  1367. let obj = getSearchData();
  1368. _page = page;
  1369. return getSearchUri(obj);
  1370. }
  1371. function getSearchUri(obj) {
  1372. let string = "?";
  1373. for (const key in obj) {
  1374. if (controlJsType(obj[key], ['string', 'number'])) {
  1375. string += key + "=" + obj[key] + '&';
  1376. } else if (controlJsType(obj[key], 'array') && obj[key].length > 0) {
  1377. obj[key].forEach(function (value) {
  1378. string += key + "=" + value + '&';
  1379. })
  1380. }
  1381. }
  1382. return string;
  1383. }
  1384. };