queryForm.js 62 KB

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