queryForm.js 59 KB

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