queryForm.js 59 KB

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