queryForm.js 60 KB

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