queryForm.js 55 KB

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