| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177 |
- const query = function getQueryForm(data) {
- {
- this.el = data.el || '#form_div';
- this.form = data.form;
- this.method = data.method || 'get';
- this.url = data.url || getPathname();
- this.condition = data.condition;
- this.paginations = [50, 100, 200, 500] || data.paginations;
- this.keydownfun = data.keydownfun || undefined;
- this.selectChange = data.selectChange || undefined;
- this.searchClick = data.searchClick || undefined;
- this.selectfun = data.selectfun || undefined;
- this._onsubmit = data._onsubmit || undefined;
- this.keydownSearch = data.keydownSearch || true;
- this.selectChangeSearch = data.selectChangeSearch || true;
- this.tip = data.tip || '';
- this.nextPage = nextPage;
- this.pervPage = pervPage;
- this.getSearchData = getSearchData;
- this.autoSubmit = (data.autoSubmit === false) ? false : true ;
- // 提交表单
- this.onsubmit = function () {
- if(!this.autoSubmit){
- return;
- }
- saveSearchOnCookie();
- let form = $("<form method=" + _this.method + " ></form>");
- for (const key in _data) {
- let map = _data[key];
- if (["string", "number"].includes(fetchJsType(map.value)) && map.value !== '') {
- form.append("<input name='" + key + "' value='" + map.value + "'>")
- } else if ('array' === fetchJsType(map.value)) {
- map.value.forEach(function (value) {
- form.append("<input type='checkbox' name='" + key + "' value='" + value + "' checked='checked' >");
- })
- }
- }
- if (_this.method !== 'get') {
- form.append("<input type='hidden' name='_token' value='" + $('meta[name="csrf-token"]').attr('content') + "'>");
- form.append("<input type='hidden' name='_method' value='" + _this.method + "'>");
- form.attr('action', _this.url);
- }
- _hidden_div.html('');
- _hidden_div.append(form);
- form.submit();
- }
- }
- let _this = this;
- let _parentNode = null;
- let _data = {};
- let _form = [];
- let _table = null;
- let _clearTr = null;
- let _pagination = null;
- let _baseData = data;
- let _searchBtn = null;
- let _hidden_div = null;
- let _parentNode_top = null;
- let _page = '';
- this.init = function () {
- _parentNode = $(_this.el);
- _form = $("<div ></div>");
- _table = $("<table class='table table-sm table-bordered m-0 position-static' style='background: rgb(255, 255, 255);'></table>");
- fillTable();
- switchData();
- addHiedDiv();
- rendererSearchForm();
- visibleClearBtn();
- parentNodeFloat();
- inputKeyDown();
- selectChange();
- createTip();
- $('[data-toggle="tooltip"]').tooltip({'trigger': 'hover'})
- }
- // form fixed
- function parentNodeFloat() {
- _parentNode_top = _parentNode.offset().top;
- let height = _parentNode.height();
- window.onscroll = function () {
- let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
- if (scrollTop > _parentNode_top) {
- _form.addClass('fixed-top');
- _parentNode.height(height);
- } else {
- _form.removeClass('fixed-top');
- }
- }
- }
- // add hide div (on submit form )
- function addHiedDiv() {
- _parentNode.append(_form.append(_table));
- _hidden_div = $('<div></div>');
- _hidden_div.hide();
- $("body").append(_hidden_div);
- }
- // fill table
- function fillTable() {
- _table.html('');
- _table.append(crateClearBtn());
- _table.append(createPagination());
- _baseData.condition.forEach(function (conditions, index, arrays) {
- let tr = $("<tr></tr>");
- conditions.forEach(function (condition, index, array) {
- let td = $("<td style='width: 260px'></td>");
- if ([null, '', undefined].includes(condition.type) && ![null, undefined].includes(condition.name)) {
- condition.type = 'input';
- }
- if (['dataTime_dataTime', 'input_input'].includes(condition.type)) {
- td = $("<td style='width: 330px'</td>")
- } else if (['select_multiple_select', 'time', 'dataTime_select'].includes(condition.type)) {
- td = $("<td style='width: 280px'</td>");
- } else if (['time', 'dataTime_select'].includes(condition.type)) {
- td = $("<td style='width: 320px'</td>");
- }
- td.append(createModule(condition));
- tr.append(td);
- })
- _table.append(tr);
- if (arrays.length === index + 1) {
- let td = $("<td ></td>");
- tr.append(td.append(createSearchBt()));
- }
- })
- $.cookie.raw = true;
- $.cookie.json = true;
- setTableWidth();
- }
- function setTableWidth() {
- let max_width = 0;
- _baseData.condition.forEach(function (conditions, xindex, xarrays) {
- let _width = 0;
- conditions.forEach(function (condition, index, array) {
- if (['input', 'select', 'dataTime', 'input_select'].includes(condition.type)) {
- _width += 260;
- } else if (['dataTime_dataTime', 'input_input'].includes(condition.type)) {
- _width += 330;
- } else if (['select_multiple_select','search_select'].includes(condition.type)) {
- _width += 280;
- } else if (['checkbox'].includes(condition.type)) {
- _width += 260;
- } else if (['time', 'dataTime_select'].includes(condition.type)) {
- _width += 320;
- } else if ([undefined, null, ''].includes(condition)) {
- _width += 260;
- }
- if (index === array.length - 1 && xindex === xarrays.length - 1) {
- _width += 260;
- }
- if (max_width < _width) {
- max_width = _width;
- }
- })
- })
- _parentNode.css('min-width', 1920 - 30)
- _parentNode.css('max-width', (max_width || window.screen.availWidth) - 30);
- _table.css('width', max_width + 'px');
- }
- // 清空过滤条件按钮
- function crateClearBtn() {
- let clearbtn = $("<button type='button' class='btn btn-warning text-dark '>清除过滤条件</button>");
- clearbtn.click(function () {
- clearData();
- visibleClearBtn();
- })
- let tr = $("<tr ><td colspan='10'></td></tr>");
- tr.find('td').append(clearbtn);
- _clearTr = tr;
- return tr;
- }
- // create SearchBtn
- function createSearchBt() {
- _searchBtn = $("<span class='btn btn-sm btn-outline-dark ml-1' >按条件搜索</span>");
- let div = $("<div class='form-inline'></div>");
- div.append(_searchBtn);
- _searchBtn.click(function () {
- if (controlJsType(_this.searchClick, 'fun')) {
- _this.searchClick();
- }
- if (controlJsType(_this._onsubmit, 'fun')) {
- _this._onsubmit();
- }else{
- _this.onsubmit();
- }
- })
- return div;
- }
- // create _pagination
- function createPagination() {
- _pagination = $("<select id='paginate' name='paginate' class='form-control form-control-sm' style='vertical-align: middle; max-width: 150px;'></select>");
- let tr = $("<tr ><td colspan='10'><div class='col p-0' style='height: 100%'></div></td></tr>");
- tr.find('div').append(_pagination);
- _this.paginations.forEach(function (map) {
- let option = $("<option value='" + map + "'>每页显示" + map + "</option>");
- _pagination.append(option);
- })
- _pagination.change(function () {
- let dom = {name: 'paginate', type: 'select', value: _pagination.val(), mold: 'select'};
- _data['paginate'] = dom;
- modifyData(dom);
- _data['paginate'] = dom;
- _this.onsubmit();
- })
- return tr;
- }
- // create Tip
- function createTip() {
- let data = fetchCookie();
- let search = window.location.search;
- if (!data || !search) {
- return;
- }
- let cookies = JSON.parse(data);
- if (!cookies['success_tip']) {
- return;
- }
- if (!cookies['success_tip'] && !!_this.tip) {
- cookies['success_tip'].value = _this.tip;
- cookies['success_tip'].count = 1;
- _data['page'].value = _page;
- saveCookie(cookies);
- set_dataOnCookie();
- _this.onsubmit();
- // 重定向
- } else if (!cookies['success_tip'] && cookies['success_tip'].count === 1) {
- let tip = $("<div class='alert alert-success h1'></div>")
- let tipdiv = $("<div style='max-width: " + window.screen.availWidth + "px'>" + cookies['success_tip'].value + "</div>").append(tip);
- _form.append(tipdiv);
- cookies['success_tip'] = '';
- saveCookie(cookies);
- }
- }
- // 数据清空
- function clearData() {
- saveCookie({});
- window.location.href = _this.url;
- }
- // 存cookie
- function saveCookie(data) {
- $.cookie(getPathname(), data, {expires: 1});
- }
- // 取cookie
- function fetchCookie() {
- return $.cookie(getPathname());
- }
- // 获取路径
- function getPathname() {
- return window.location.pathname;
- }
- // 获取js对象类型types.call
- function fetchJsType(JsObj) {
- let types = Object.prototype.toString;
- let obj = types.call(JsObj);
- return (obj === "[object Number]" && 'number')
- || (obj === "[object Boolean]" && 'boolean')
- || (obj === "[object String]" && 'string')
- || (obj === "[object Array]" && 'array')
- || (obj === "[object Function]" && 'fun')
- || (obj === "[object Object]" && 'object')
- || (obj === "[object Null]" && 'null')
- || (obj === "[object Undefined]" && 'undefined');
- }
- // 判断js类型
- function controlJsType(JsObj, type) {
- if (fetchJsType(type) === 'array') {
- return type.includes(fetchJsType(JsObj));
- } else if (fetchJsType(type) === 'string') {
- return fetchJsType(JsObj) === type;
- } else {
- return JsObj === type;
- }
- }
- // create Module
- function createModule(condition) {
- let type = condition.type;
- return (['input', '', null].includes(type) && getInput(condition))
- || (type === "select" && getSelect(condition))
- || (type === "search_select" && getSearchSelect(condition))
- || (type === "input_select" && getInputSelect(condition))
- || (type === "dataTime" && getDataTime(condition))
- || (type === "time" && getTime(condition))
- || (type === "input_input" && getInputInput(condition))
- || (type === "dataTime_dataTime" && getDataTimeDataTime(condition))
- || (type === "dataTime_select" && getDataTimeSelect(condition))
- || (type === "select_multiple_select" && getSelectMultipleSelect(condition))
- || (type === "checkbox" && getCheckBox(condition));
- }
- // input
- function getInput(condition) {
- 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' >");
- input.attr('title', condition.tip === undefined ? '' : condition.tip);
- input.on('input', function () {
- let dom = {name: condition.name, type: 'input', value: this.value, mold: 'input'}
- modifyData(dom)
- });
- return input;
- }
- // select
- function getSelect(condition) {
- let select = $("<select name='" + condition.name + "' class='form-control form-control-sm' style='max-width: 200px;' data-toggle='tooltip' data-placement='top'></select>")
- select.attr('title', condition.tip === undefined ? '' : condition.tip);
- let tip = $("<option value class='text-secondary'>" + condition.placeholder + "</option>")
- select.append(tip);
- condition.data.forEach(function (map) {
- let option = $("<option value='" + map.name + "' class='text-secondary'>" + map.value + "</option>")
- select.append(option);
- })
- select.on('change', function () {
- let dom = {name: condition.name, type: 'select', value: this.value, mold: 'select'}
- modifyData(dom)
- })
- return select;
- }
- // search_select
- function getSearchSelect(condition) {
- let div = $("<div class='form-inline'></div>");
- let input = $("<input class='form-control form-control-sm' placeholder='" + condition.placeholder[0] + "' style='width: 70px;' autocomplete='off' data-toggle='tooltip' data-placement='top'>");
- input.attr('title', controlJsType(condition.tip[0], 'undefined') ? '输入关键词快速定位下拉列表,回车确定' : condition.tip[0]);
- 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>");
- select.attr('title', controlJsType(condition.tip[1], 'undefined') ? '' : condition.tip[1]);
- let placeholder = '';
- if (controlJsType(condition.placeholder, 'array') && !['', undefined, null].includes(condition.placeholder[1])) {
- placeholder = condition.placeholder[1];
- }
- fillSelectOption(select, condition.data, placeholder);
- input.bind('input propertychange', function () {
- let value = this.value;
- let data = condition.data.filter(function (map) {
- return map.value.includes(value);
- })
- fillSelectOption(select, data || condition.data);
- select.val(_data[condition.name].select);
- })
- select.change(function () {
- let dom = {name: condition.name, type: 'input_select', value: this.value, mold: 'select'};
- modifyData(dom);
- })
- return div.append(input).append(select);
- }
- // input_select
- function getInputSelect(condition) {
- let div = $("<div class='form-inline'></div>");
- 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' >");
- input.attr('title', controlJsType(condition.tip[0], 'undefined') ? '' : condition.tip[0]);
- let select = $("<select class='form-control form-control-sm ml-2' name='" + condition.name[1] + "' style='max-width: 200px;' title='" + condition.tip[1] + "''>");
- fillSelectOption(select, condition.data);
- input.bind('input propertychange', function () {
- let dom = {name: this.name, type: 'input_select_longtext', value: this.value, mold: 'input'};
- modifyData(dom);
- })
- select.change(function () {
- let dom = {name: this.name, type: 'input_select_longtext', value: this.value, mold: 'select'};
- modifyData(dom);
- })
- return div.append(input).append(select);
- }
- // datTime
- function getDataTime(condition) {
- let dataTime = $("<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'>");
- dataTime.attr('title', controlJsType(condition.tip, 'undefined') ? '' : condition.tip);
- dataTime.bind('input propertychange', function () {
- let dom = {name: condition.name, type: 'dataTime', value: this.value, mold: 'dataTime'};
- modifyData(dom);
- })
- return dataTime;
- }
- // time
- function getTime(condition) {
- let div = $("<div class='form-inline'></div>");
- let dataTime = $("<input type='date' class='form-control form-control-sm' name='" + condition.name + "' style='width:150px' data-toggle='tooltip' data-placement='top' >");
- dataTime.attr('title', controlJsType(condition.tip[0], 'undefined') ? '' : condition.tip[0]);
- 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' >");
- min.attr('title', controlJsType(condition.tip[1], 'undefined') ? '' : condition.tip[1]);
- dataTime.bind('input propertychange', function () {
- let value = this.value !== '' ? this.value + ' ' + ([null, undefined, ''].includes(min.value) ? '00:00' : min.value) : '';
- let dom = {name: condition.name, type: 'time', value: value, mold: 'time'};
- modifyData(dom);
- })
- min.bind('input propertychange', function () {
- verifyTime(this);
- })
- min.bind('input propertychange', function () {
- if (['null', 'undefined', ''].includes(dataTime.val())) {
- return;
- }
- let value = ['null', 'undefined', ''].includes(dataTime.val()) ? '' : dataTime.val() + ' ' + min.val();
- //let value = dataTime.value !== '' ? dataTime.value + ' ' + (min.value === '' ? '00:00' : min. value) : '';
- let dom = {name: condition.name, type: 'time', value: value, mold: 'time'};
- modifyData(dom);
- })
- return div.append(dataTime).append(min);
- }
- // input_input
- function getInputInput(condition) {
- let div = $("<div class='form-inline'></div>");
- 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] + "'>");
- input1.attr('title', controlJsType(condition.tip[0], 'undefined') ? '' : condition.tip[0]);
- 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] + "'>");
- input2.attr('title', controlJsType(condition.tip[1], 'undefined') ? '' : condition.tip[1]);
- input1.bind('input propertychange', function () {
- let dom = {name: this.name, type: 'input_input', value: this.value, mold: 'input'};
- modifyData(dom);
- })
- input2.bind('input propertychange', function () {
- let dom = {name: this.name, type: 'input_input', value: this.value, mold: 'input'};
- modifyData(dom);
- })
- return div.append(input1).append(input2);
- }
- // dataTime_dataTime
- function getDataTimeDataTime(condition) {
- let div = $("<div class='form-inline'></div>");
- let dataTime1 = $("<input type='date' class='form-control form-control-sm' name='" + condition.name[0] + "' style='width: 150px' data-toggle='tooltip' data-placement='top' >");
- dataTime1.attr('title', controlJsType(condition.tip[0], 'undefined') ? '' : condition.tip[0]);
- let dataTime2 = $("<input type='date' class='form-control form-control-sm ml-2' name='" + condition.name[1] + "' style='width: 150px;' data-toggle='tooltip' data-placement='top' >");
- dataTime2.attr('title', controlJsType(condition.tip[1], 'undefined') ? '' : condition.tip[1]);
- dataTime1.bind('input propertychange', function () {
- let dom = {name: this.name, type: 'dataTime_dataTime', value: this.value, mold: 'dataTime'};
- modifyData(dom);
- })
- dataTime2.bind('input propertychange', function () {
- let dom = {name: this.name, type: 'dataTime_dataTime', value: this.value, mold: 'dataTime'};
- modifyData(dom);
- })
- return div.append(dataTime1).append(dataTime2);
- }
- // dataTime_select
- function getDataTimeSelect(condition) {
- let div = $("<div class='form-inline'></div>");
- let dataTime = $("<input type='date' class='form-control form-control-sm' name='" + condition.name[0] + "' style='max-width: 150px;' data-toggle='tooltip' data-placement='top' >");
- dataTime.attr('title', controlJsType(condition.tip[0], 'undefined') ? '' : condition.tip[0]);
- 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>");
- select.attr('title', controlJsType(condition.tip[1], 'undefined') ? '' : condition.tip[1]);
- fillSelectOption(select, condition.data)
- dataTime.bind('input propertychange', function () {
- let dom = {name: this.name, type: 'dataTime_select', value: this.value, mold: 'dataTime'};
- modifyData(dom);
- })
- select.change(function () {
- let dom = {name: this.name, type: 'dataTime_select', value: this.value, mold: 'select'};
- modifyData(dom);
- })
- return div.append(dataTime).append(select);
- }
- // select_multiple_select
- function getSelectMultipleSelect(condition) {
- let div = $("<div class='form-inline' style='position: relative'></div>");
- 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>");
- let label = $("<label id='" + condition.name + "_lab' style='vertical-align: middle; width: 100px' >(多选)</label>").hide();
- let select_div = $("<div ></div>");
- select.attr('title', controlJsType(condition.tip[0], 'undefined') ? '' : condition.tip[0]);
- let placeholder = controlJsType(condition.placeholder[0], 'undefined') ? '' : condition.placeholder[0];
- fillSelectOption(select, condition.data, placeholder);
- 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'>");
- input.attr('title', controlJsType(condition.tip[1], 'undefined') ? '' : condition.tip[1]);
- input.attr('placeholder', controlJsType(condition.placeholder[1], 'undefined') ? '' : condition.placeholder[1])
- let ul_div = $("<div class='pl-0' 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>");
- 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>");
- select_div.append(input).append(ul_div.append(ul));
- div.append(select).append(label).append(select_div);
- fillMultipleSelectOption(ul, condition.data, condition.name);
- input.bind('input propertychange', function () {
- let value = this.value;
- if (value === '') {
- fillMultipleSelectOption(ul, _data[condition.name].data, condition.name);
- redenerUl(ul);
- return;
- }
- let select = _data[condition.name].select;
- if ([undefined, null, ''].includes(select)) {
- select = [];
- }
- let select_data = _data[condition.name].data.filter(function (map) {
- return map.value.includes(value) || select.includes(map.name);
- })
- fillMultipleSelectOption(ul, select_data, condition.name);
- redenerUl(ul);
- })
- select.change(function () {
- let dom = {
- name: condition.name,
- type: 'select_multiple_select',
- value: [this.value]
- }
- modifyData(dom);
- redenerUl(ul);
- })
- input.focus(function () {
- ul_div.show();
- })
- input.blur(function () {
- setTimeout(function () {
- if (!select_div.is(':focus')) {
- ul_div.hide();
- }
- }, 1000);
- })
- select_div.focus(function () {
- ul_div.show();
- })
- select_div.blur(function () {
- ul_div.hide();
- })
- ul_div.blur(function () {
- ul_div.hide();
- })
- ul_div.hide();
- return div;
- }
- // checkbox
- function getCheckBox(condition) {
- let div1 = $("<div class='form-inline'></div>")
- condition.data.forEach(function (map, index) {
- let div = $('<div class="custom-control custom-control-inline custom-checkbox"></div>');
- let checkbox = $("<input type='checkbox' name='" + condition.name + "' class='custom-control-input' id='" + condition.name + index + "' value='" + map.name + "' >");
- let label = $("<label class='custom-control-label text-left font-weight-norma' for='" + condition.name + index + "'>" + map.value + "</label>")
- div1.append(div.append(checkbox).append(label));
- checkbox.click(function () {
- let value = [];
- $.each($("input:checkbox[name='" + condition.name + "']:checked"), function () {
- value.push($(this).val());
- })
- let dom = {name: condition.name, type: 'checkbox', value: value, mold: 'check'};
- modifyData(dom);
- });
- })
- return div1;
- }
- // fill select
- function fillSelectOption(select, data, placeholder = '') {
- select.html('');
- if (![undefined, null].includes(placeholder)) {
- let option = $("<option value></option>");
- if ('' === placeholder) {
- option = $("<option value>" + '全部' + placeholder + "</option>");
- } else {
- option = $("<option value>" + placeholder + "</option>");
- }
- select.append(option);
- }
- data.forEach(function (map) {
- let option = $("<option value='" + map.value + "'>" + map.name + "</optio>");
- select.append(option);
- })
- if (data !== undefined) {
- }
- }
- // fill multiple select ul
- function fillMultipleSelectOption(ul, data, name) {
- ul.html('');
- data.forEach(function (map) {
- let span = $("<span style='cursor:default'>" + map.value + "</span>");
- let li = $("<li class='list-group-item pt-0 pb-0' value='" + map.name + "'></li>");
- li.append(span);
- ul.append(li);
- li.click(function () {
- let value = li.attr('value');
- let dom_data = _data[name].value;
- if (controlJsType(dom_data, ['undefined', 'null', 'string'])) {
- dom_data = [dom_data];
- }
- if (dom_data.includes(value)) {
- dom_data.splice(dom_data.indexOf(value), 1);
- } else {
- dom_data.push(value);
- dom_data = arrDuplicate(dom_data);
- }
- let dom = {
- name: ul.attr('name'),
- type: 'select_multiple_select',
- value: dom_data,
- select: dom_data,
- mold: 'select_multiple_select'
- };
- modifyData(dom);
- redenerUl(ul);
- isMultiple(name);
- _this.onsubmit();
- })
- })
- }
- function isMultiple(name) {
- let label = $('#' + name + '_lab');
- let select = $('#' + name + '_sel');
- let dom_data = _data[name].value;
- if (dom_data.length === 1 ) {
- select.show();
- select.val(dom_data[0]);
- label.hide();
- }else if(controlJsType(dom_data,'string')){
- select.show();
- select.val(dom_data);
- label.hide();
- } else if (dom_data.length >= 2) {
- select.hide();
- label.show();
- }
- }
- // modify _data
- function modifyData(dom) {
- _data[dom.name].mold = dom.mold;
- _data[dom.name].value = dom.value;
- _data[dom.name].select = dom.value;
- redenerSearchFormOnData(dom.name, dom.value, dom.mold);
- }
- // save search on cookie
- function saveSearchOnCookie() {
- let saveData = {};
- for (const key in _data) {
- if (!['', null, undefined].includes(_data[key].value)) {
- if (controlJsType(_data[key].value, 'array') && _data[key].value.length === 0) {
- //console.log(key);
- continue;
- }
- saveData[key] = {
- value: _data[key].value,
- mold: _data[key].mold,
- }
- }
- }
- if (!_this.page) {
- saveData['page'] = _this.page;
- }
- saveCookie(saveData);
- }
- // hour_minute
- function verifyTime(time) {
- setTimeout(function () {
- time.value = time.value.replace(':', ':');
- time.value = time.value.replace(/[a-zA-Z]/, '');
- time.value = time.value.replace(/[^\d^\:]/, '');
- time.value = time.value.replace(/\:\:/, ':');
- time.value = time.value.replace(/^([\d]{1})([\s]{1})/, "$1:");
- time.value = time.value.replace(/^([\d]{1})([\d]{1})([\s]{1})/, "$1$2:");
- time.value = time.value.replace(/^([\d]{1})([\d]{1})([\d]{1})/, "$1$2:$3");
- time.value = time.value.replace(/^([\d]{1})([\d]{1})([\d]{1})([\d]{1})(.*?)/, "$1$2$3$4");
- time.value = time.value.replace(/^([\d]{1})([\d]{1})([\d]{1})([\d]{1})/, "$1$2:$3$4");
- time.value = time.value.replace(/^([\d]{1})([\d]{1}):([\d]{1})([\d]{1})([\s\S]{1})/, "$1$2:$3$4");
- time.value = time.value.replace(/^([\d]{1})([\d]{1}):([\D]{1,99})/, "$1$2:");
- time.value = time.value.replace(/^([\d]{1})([\d]{1})([\d]{1})\./, "$1:$2$3");
- time.value = time.value.replace(/^([\d]{1})\.([\d]{1})([\d]{1})/, "$1:$2$3");
- time.value = time.value.replace(/^([\d]{1})([\d]{2})([\S\s]{1,99})/, "$1$2:$3");
- time.value = time.value.replace(/^([\d]{1}):([\d]{2})([\S\s]{1,99})/, "$1:$2");
- time.value = time.value.replace(/^([\d])/, "$1");
- time.value = time.value.replace(/^([1])([\d]{1})/, "$1$2");
- time.value = time.value.replace(/^([3-9])([\d]{1})/, "2$2");
- time.value = time.value.replace(/^([2-9]{1})([4-9]{1})/, "$13");
- time.value = time.value.replace(/^([\d]{1})([\d]{1}):([6-9]{1})/, "$1$2:5");
- time.value = time.value.replace(/^([\d]{1})([\d]{1})$/, "$1$2:");
- }, 10);
- }
- // array duplicate
- function arrDuplicate(arr) {
- let res = [];
- let obj = {};
- for (let i = 0; i < arr.length; i++) {
- if ([undefined, null, ''].includes(arr[i])) {
- continue;
- }
- if (!obj[arr[i]]) {
- obj[arr[i]] = 1;
- res.push(arr[i]);
- }
- }
- return res;
- }
- // get search data
- function getSearchData(type = 'array') {
- let data = {};
- if (type === 'array') {
- for (let key in _data) {
- if (_data[key].value !== undefined && _data[key].value !== '') {
- data[key] = _data[key].value
- }
- }
- } else if (type === 'string') {
- data = '';
- for (let key in _data) {
- if (![undefined, null, ''].includes(_data[key].value)) {
- let value = _data[key].value;
- if (controlJsType(value, 'string')) {
- data = data + "&" + key + "=" + value;
- } else if (controlJsType(value, 'array')) {
- value.forEach(function (val) {
- data = data + "&" + key + "=" + val;
- });
- }
- }
- }
- }
- return data;
- }
- // renderer input
- function rendererSearchFormOnInput(key, value, mold) {
- if ([undefined, null, ''].includes(value)) {
- $(_form).find("input[name='" + key + "']").val('');
- $(_form).find("input[name='" + key + "']").removeClass('bg-warning');
- } else {
- $(_form).find("input[name='" + key + "']").val(value);
- $(_form).find("input[name='" + key + "']").addClass('bg-warning');
- }
- }
- // renderer select
- function rendererSearchFormOnSelect(key, value, mold) {
- if ([undefined, null, ''].includes(value)) {
- $(_form).find("select[name='" + key + "']").val('').removeClass('bg-warning');
- if (key === 'paginate') {
- $(_form).find("select[name='" + key + "']").val(_this.paginations[0]).removeClass('bg-warning');
- }
- } else {
- $(_form).find("select[name='" + key + "']").val(value);
- //console.log(value === _this.paginations[0]);
- if (key === 'paginate' && value == _this.paginations[0]) {
- return;
- }
- $(_form).find("select[name='" + key + "']").addClass('bg-warning');
- }
- }
- // renderer check
- function rendererSearchFormOnCheck(key, value, mold) {
- if ([undefined, null, ''].includes(value)) {
- $(_form).find("input[name='" + key + "']").prop("checked", false).removeClass('bg-warning');
- } else {
- if (controlJsType(value, 'array')) {
- value.forEach(function (_value) {
- $(_form).find("input[name='" + key + "'][value='" + _value + "']").prop("checked", true).addClass('bg-warning');
- })
- } else if (controlJsType(value, 'string')) {
- $(_form).find("input[name='" + key + "'][value='" + value + "']").prop("checked", true).addClass('bg-warning');
- }
- }
- }
- // redener multiple select input
- function rendererSearchFormOnMultipleSelect(key, value, mold) {
- let ul = $(_form).find("ul[name='" + key + "']");
- if ([undefined, null, ''].includes(value) || (controlJsType(value, 'array') && value.length === 0)) {
- $(_form).find("input[name='" + key + "_tip']").removeClass('bg-warning');
- ul.addClass('bg-warning');
- } else if (!!value) {
- $(_form).find("input[name='" + key + "_tip']").addClass('bg-warning');
- ul.addClass('bg-warning');
- } else if (controlJsType(value, 'array') && value.length >= 1) {
- $(_form).find("input[name='" + key + "_tip']").addClass('bg-warning');
- ul.addClass('bg-warning');
- }
- redenerUl(ul);
- }
- // redener multiple select ul
- function redenerUl(ul) {
- let name = ul.attr('name');
- let select = _data[name].select;
- if ([null, undefined].includes(select)) {
- _data[name].select = [];
- select = [];
- }
- let lis = ul.find('li');
- if ([null, undefined, ''].includes(lis)) {
- return;
- } else {
- lis.get().forEach(function (dom) {
- if (select.includes($(dom).attr('value'))) {
- dom.style.backgroundColor = "#4AA0E6";
- $(dom).addClass('active');
- } else {
- dom.style.backgroundColor = "#FFFFFF";
- $(dom).removeClass('active');
- }
- })
- isMultiple(name);
- }
- }
- function rendererSearchFormOnTime(key, value, mold) {
- if ([undefined, null, ''].includes(value)) {
- $(_form).find("input[name='+key+']").val(value.substring(0, 10)).addClass('bg-warning');
- if (value.length > 11 + 5) {
- $(_form).find("input[name='" + key + "_tim']").val(value.substring(11, 5));
- }
- } else {
- $(_form).find("input[name='+key+']").val("").removeClass('bg-warning');
- $(_form).find("input[name='" + key + "_tim']").val("");
- }
- }
- function visibleClearBtn() {
- let bool = false;
- for (const key in _data) {
- if (key === 'paginate') {
- if ((key === 'paginate' && _data[key].value !== _this.paginations[0])) {
- bool = true;
- }
- } else {
- if (![undefined, null, ''].includes(_data[key].value)) {
- bool = true;
- }
- }
- }
- if (bool) {
- _clearTr.show();
- } else {
- _clearTr.hide();
- }
- }
- function inputKeyDown() {
- $(_form).find('input').keydown(function (e) {
- if (e.keyCode === 13) {
- if (_this.keydownSearch) {
- _this.onsubmit()
- }
- if (controlJsType(_this.keydownfun, 'fun')) {
- _this.keydownfun();
- }
- }
- });
- $(_form).find("input[type='date']").change(function (e) {
- if (_this.keydownSearch) {
- _this.onsubmit()
- }
- if (controlJsType(_this.keydownfun, 'fun')) {
- _this.keydownfun();
- }
- });
- }
- function selectChange() {
- $(_form).find('select').change(function () {
- _this.onsubmit();
- })
- }
- function redenerSearchFormOnData(key, value, mold) {
- if (mold === 'input') {
- rendererSearchFormOnInput(key, value, mold);
- } else if (mold === 'select') {
- rendererSearchFormOnSelect(key, value, mold);
- } else if (mold === 'check') {
- rendererSearchFormOnCheck(key, value, mold);
- } else if (mold === 'select_multiple_select') {
- rendererSearchFormOnMultipleSelect(key, value, mold);
- } else if (mold === 'time') {
- rendererSearchFormOnTime(key, value, mold);
- }
- }
- // 数据转换
- function switchData() {
- _this.condition.forEach(function (conditions) {
- conditions.forEach(function (condition) {
- if (['input', 'select', 'dataTime', 'search_select', 'time', 'select_multiple_select','checkbox'].includes(condition.type)) {
- if (!condition.select) {
- condition.select = '';
- }
- let data = {
- name: condition.name,
- type: condition.type,
- value: condition.value,
- select: condition.select,
- mold: ['input', 'dataTime'].includes(condition.type) ? 'input' : condition.type
- }
- if (condition.type === 'search_select') {
- data.mold = 'select';
- }
- _data[condition.name] = data;
- } else if (['dataTime_dataTime', 'input_input', 'input_select', 'dataTime_select'].includes(condition.type)) {
- let types = condition.type.split("_");
- if (!condition.select) {
- condition.select = ['', ''];
- }
- if (!condition.value) {
- condition.value = ['', ''];
- }
- condition.name.forEach(function (_name, index) {
- let data = {
- name: _name,
- type: types[index],
- value: condition.value[index],
- select: condition.select[index],
- mold: ['input', 'dataTime'].includes(types[index]) ? 'input' : types[index]
- }
- _data[_name] = data;
- })
- }
- })
- })
- }
- // redererSearchForm on cookie
- function rendererSearchForm() {
- let url = document.referrer;
- let partt = /(Edit\/)|(\/eidt)|(eidt\/)/i;
- if (url.search(partt) > 0) {
- rendererOptionOnCookie();
- } else {
- rendererOptionOnUrl();
- }
- }
- function rendererOptionOnCookie() {
- //console.log('rendererSearchFromOnCookie');
- let data = fetchCookie();
- if (data === undefined) {
- return;
- }
- data = JSON.parse(data);
- for (let key in data) {
- _data[key].value = data[key].value;
- _data[key].mold = data[key].mold;
- _data[key].select = data[key].select;
- }
- rendererSearchFormOn_data();
- }
- function rendererOptionOnUrl() {
- if (!window.location.search) {
- return;
- }
- searchOptionToUrlsearch();
- rendererSearchFormOn_data();
- }
- function rendererSearchFormOn_data() {
- // console.log('rendererSearchFormOn_data', _data);
- for (let key in _data) {
- let value = _data[key].value, type = _data[key].type, mold = _data[key].mold;
- _data[key].select = value;
- if (['input', 'dataTime'].includes(mold)) {
- rendererSearchFormOnInput(key, value, mold);
- } else if (['select'].includes(mold)) {
- rendererSearchFormOnSelect(key, value, mold);
- } else if (['checkbox'].includes(mold)) {
- rendererSearchFormOnCheck(key, value, mold);
- } else if (['select_multiple_select'].includes(mold)) {
- if (controlJsType(value, ['array', 'string'])) {
- let ul = $(_form).find("ul[name='" + key + "']");
- redenerUl(ul);
- rendererSearchFormOnMultipleSelect(key, value, mold);
- }
- } else if (['time'].includes(type) && value !== undefined) {
- $(_form).find("input[name='" + key + "']").val(value.substr(0, 10));
- $(_form).find("input[id='" + key + "_min']").val(value.substr(11, value.length));
- }
- /* if (['input', 'dataTime', 'input_input', 'dataTime_dataTime'].includes(type)) {
- rendererSearchFormOnInput(key,value,mold);
- } else if (['select', 'input_select', 'dataTime_select'].includes(type)) {
- if (mold === 'select') {
- rendererSearchFormOnSelect(key,value,mold)
- } else if (mold === 'input') {
- rendererSearchFormOnInput(key,value,mold);
- } else if (mold === 'dataTime') {
- rendererSearchFormOnInput(key,value,mold);
- }
- } else if (['input_select_text'].includes(type)) {
- if (mold === 'input') {
- rendererSearchFormOnInput(key,value,mold);
- } else if (mold === 'select') {
- rendererSearchFormOnSelect(key,value,mold)
- }
- } else if (['checkbox'].includes(type)) {
- if (controlJsType(value, 'array')) {
- value.forEach(function (map) {
- $(_form).find("input[type='checkbox'][name='" + key + "'][value='" + map + "']").attr("checked", true);
- })
- }
- } else if (['select_multiple_select'].includes(type)) {
- if (controlJsType(value, ['array','string']) ) {
- let ul = $(_form).find("ul[name='" + key + "']");
- redenerUl(ul);
- rendererSearchFormOnMultipleSelect(key,value,mold);
- }
- } else if (['time'].includes(type) && value !== undefined) {
- $(_form).find("input[name='"+key+"']").val(value.substr(0,10));
- $(_form).find("input[id='"+key+"_min']").val(value.substr(11,value.length));
- }*/
- }
- if ([undefined, null, ''].includes(data['paginate'])) {
- return;
- }
- if ([undefined, null, ''].includes(data['paginate'].value)) {
- $(_form).find("select[name='paginate']").val(_this.paginations[0]);
- } else {
- $(_form).find("select[name='paginate']").val(data['paginate'].value);
- }
- }
- // url path on search form option renderer
- function searchOptionToUrlsearch() {
- let data = window.location.search;
- data = decodeURIComponent(data);
- if (!!data) {
- data = (data.substr(1)).split('&');
- data = convertArrayToObj(data);
- for (const key in data) {
- if(!data[key]){
- continue;
- }
- if (key === 'paginate') {
- _data[key] = {
- name: key,
- value: data[key],
- type: 'select',
- mold: 'select'
- }
- } else if (key === 'page') {
- _page = data[key];
- } else {
- _data[key].value = data[key];
- }
- // console.log('key', key, typeof key);
- }
- }
- }
- // cookie -> data
- function set_dataOnCookie() {
- let data = fetchCookie();
- data = JSON.parse(data);
- if (!data) {
- return;
- }
- for (const key in data) {
- if (!!data[key]) {
- _data[key].value = data[key].value;
- }
- }
- }
- // array - > object
- function convertArrayToObj(arr) {
- if (!arr || !controlJsType(arr, 'array')) {
- return {};
- } else if (controlJsType(arr, 'array') && arr.length > 0) {
- let data = {};
- arr.forEach(function (map) {
- let arr = map.split('='), key = arr[0], value = arr[1];
- if (!data[key]) {
- data[key] = value;
- } else {
- if (controlJsType(data[key], 'string')) {
- data[key] = [data[key], value];
- } else if (controlJsType(data[key], 'array')) {
- data[key].push(value);
- }
- }
- })
- return data;
- }
- }
- // paginate
- function pervPage() {
- let obj = getSearchData();
- if(!!obj['page'] && obj['page']>1){
- obj['page'] = obj['page']-1;
- }
- return getSearchUri(obj);
- }
- function nextPage() {
- let obj = getSearchData();
- if(!!obj['page'] ){
- obj['page'] = obj['page']+1;
- }else{
- obj['page'] = 2;
- }
- if(!obj['paginate']){
- obj['paginate'] = 50;
- }
- return getSearchUri(obj);
- }
- function getSearchObj() {
- let data = window.location.search;
- data = decodeURIComponent(data);
- if(!data){
- return {};
- }
- let saveData = {};
- data = data.substr(1).split('&');
- data.forEach(function (map) {
- let arr = map.split('&'),key = arr[0],value = arr[1];
- if(!!value){
- if (!saveData[key]) {
- saveData[key] = value;
- } else {
- if (controlJsType(data[key], 'string')) {
- saveData[key] = [saveData[key] , value];
- } else if (controlJsType(data[key], 'array')) {
- saveData[key].push(value);
- }
- }
- }
- })
- return saveData;
- }
- function getSearchUri(obj){
- let string = "?";
- for (const key in obj) {
- if(controlJsType(obj[key],['string','number'])){
- string+= key + "=" + obj[key]+'&';
- }else if( controlJsType(obj[key],'array')&&obj[key].length > 0){
- obj[key].forEach(function(value){
- string+=key + "=" + value+'&';
- })
- }
- }
- return string;
- }
- };
|