queryForm.js 59 KB

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