queryForm.js 62 KB

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