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 =data.paginations ? data.paginations : [50, 100, 200, 500,1000] ;
this.isPaginations = data.isPaginations !== false;
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.goPage = goPage;
this.getSearchData = getSearchData;
this.autoSubmit = (data.autoSubmit !== false) ;
this.param = data.param || [];
this.appendDom = data.appendDom || undefined;
// 提交表单
this.onsubmit = function () {
if(!this.autoSubmit){
return;
}
saveSearchOnCookie();
let form = $("
");
for (const key in _data) {
let map = _data[key];
if(map['required'] == true && (map.value === '' || map.value===null || map.value === undefined))return;
if (["string", "number"].includes(fetchJsType(map.value)) && map.value !== '') {
// if(map.value){
// let string = new String(map.value);
// if(string.length > 1 ){
// string = string.replace(/\+/g,',');
// map.value = string
// }
// }
//form.append("");
form.append("")
} else if ('array' === fetchJsType(map.value)) {
let string = map.value.join(',');
//form.append("");
form.append("")
}
}
for (let key in this.param){
form.append("");
}
if (_this.method !== 'get') {
form.append("");
form.append("");
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 = $("");
_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();
let dom = _this.appendDom ? $("#"+_this.appendDom+"") : null;
$(window).scroll(function(){
let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
if (scrollTop > _parentNode_top) {
_form.addClass('fixed-top');
_parentNode.height(height);
if (dom){
dom.css('position','fixed');
dom.css('background','rgb(255, 255, 255)');
dom.css('top',height);
dom.css('z-index',1000);
}
} else {
_form.removeClass('fixed-top');
if (dom){
dom.attr('style','');
}
}
});
}
// add hide div (on submit form )
function addHiedDiv() {
_parentNode.append(_form.append(_table));
_hidden_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 = $("
");
conditions.forEach(function (condition, index, array) {
let td = $(" | ");
if ([null, '', undefined].includes(condition.type) && ![null, undefined].includes(condition.name)) {
condition.type = 'input';
}
if (['dateTime_dateTime','dateTimeLocal', 'input_input'].includes(condition.type)) {
td = $("")
} else if (['select_multiple_select', 'time', 'dateTime_select'].includes(condition.type)) {
td = $(" | ");
} else if (['time', 'dateTime_select'].includes(condition.type)) {
td = $(" | ");
}
td.append(createModule(condition));
tr.append(td);
})
_table.append(tr);
if (arrays.length === index + 1) {
let 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','dateMonth', 'select', 'dateTime', 'input_select'].includes(condition.type)) {
_width += 260;
} else if (['dateTime_dateTime','dateTimeLocal', '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', 'dateTime_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 = $("");
clearbtn.click(function () {
clearData();
visibleClearBtn();
})
let tr = $(" |
");
tr.find('td').append(clearbtn);
_clearTr = tr;
return tr;
}
// create SearchBtn
function createSearchBt() {
_searchBtn = $("按条件搜索");
let 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() {
let isPaginations = _this.isPaginations ? '' : 'hidden';
_pagination = $("");
let tr = $(" |
");
tr.find('div').append(_pagination);
_this.paginations.forEach(function (map) {
let 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 = $("")
let tipdiv = $("" + cookies['success_tip'].value + "
").append(tip);
_form.append(tipdiv);
cookies['success_tip'] = '';
saveCookie(cookies);
}
}
// 数据清空
function clearData() {
saveCookie({});
let url=_this.url;
if (_this.param !== []){
url += "?";
let i=0;
for (let key in _this.param){
if (i!==0) url += "&";
url += key+"="+_this.param[key];
i++;
}
}
window.location.href = 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 === "dateTime" && getdateTime(condition))
|| (type === "dateTimeLocal" && getDateTimeLocal(condition))
|| (type === "dateMonth" && getDateMonth(condition))
|| (type === "time" && getTime(condition))
|| (type === "input_input" && getInputInput(condition))
|| (type === "dateTime_dateTime" && getdateTimedateTime(condition))
|| (type === "dateTime_select" && getdateTimeSelect(condition))
|| (type === "select_multiple_select" && getSelectMultipleSelect(condition))
|| (type === "checkbox" && getCheckBox(condition));
}
// dateMonth
function getDateMonth(condition) {
let dateMonth = $("");
dateMonth.attr('title', condition.tip === undefined ? '' : condition.tip);
dateMonth.blur(function(){
modifyData({name: condition.name, type: 'dateMonth', value: this.value, mold: 'dateMonth'});
});
dateMonth.keypress(function(event){
if(event.which === 13){
modifyData({name: condition.name, type: 'input', value: this.value, mold: 'input'});
}
});
return dateMonth;
}
// input
function getInput(condition) {
let input = $("");
input.attr('title', condition.tip === undefined ? '' : condition.tip);
input.blur(function(){
modifyData({name: condition.name, type: 'input', value: this.value, mold: 'input'});
});
input.keypress(function(event){
if(event.which === 13)modifyData({name: condition.name, type: 'input', value: this.value, mold: 'input'});
});
return input;
}
// select
function getSelect(condition) {
let select = $("")
select.attr('title', condition.tip === undefined ? '' : condition.tip);
let tip = $("")
select.append(tip);
condition.data.forEach(function (map) {
let 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 = $("");
let input = $("");
input.attr('title', controlJsType(condition.tip[0], 'undefined') ? '输入关键词快速定位下拉列表,回车确定' : condition.tip[0]);
let 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.blur(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);
});
input.keypress(function(event){
if(event.which === 13){
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 () {
modifyData({name: condition.name, type: 'input_select', value: this.value, mold: 'select'});
})
return div.append(input).append(select);
}
// input_select
function getInputSelect(condition) {
let div = $("");
let input = $("");
input.attr('title', controlJsType(condition.tip[0], 'undefined') ? '' : condition.tip[0]);
let select = $("