queryForm.js 56 KB

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