queryForm.js 60 KB

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