layui.define(['layer'],function(exports){
|
/**
|
* 参照的列表框
|
* weidy@2018-03-08
|
*/
|
var ReferGrid = function(){
|
this.defaultConfig = {
|
gridUrl:'/webReferController/referDataGrid',//表格的参照路径
|
tableDefinedUrl:'/webReferController/getTableDefine',
|
valueField:'oid',//值的字段
|
textField:'name'//显示的字段
|
};
|
this.backPath= configData.compatibility ? path : configData.objectServicePath
|
};
|
ReferGrid.prototype.init = function(){
|
|
};
|
ReferGrid.prototype.showReferDialog = function (referConfig){
|
var filter = referConfig.filter,
|
name = referConfig.name,
|
title = referConfig.title,
|
valueInfo = ($webUtil.isNotNull(referConfig.text)?("已设置的值为[" +referConfig.text +"]"):'未设置值'),
|
isMuti = referConfig.isMuti,
|
target = referConfig.target,
|
value = referConfig.value,
|
text = referConfig.text;
|
if(filter.indexOf('.')>-1){
|
filter=filter.split('.')[0]+'-'+filter.split('.')[1];
|
}
|
if(name.indexOf('.')>-1){
|
name=name.split('.')[0]+'-'+name.split('.')[1];
|
}
|
var options = referConfig.options;
|
var me = this;
|
var referFilter = filter+"_" + name;
|
var referIndex = $webUtil.dialog({
|
title:title,
|
btn:['确定','取消'],
|
content:'<div layui-filter="referGrid_toolbar_' + referFilter + '" class="layui-table-toolbar " style="margin: 5px 0 5px 0"></div><div style="margin: 0 5px"><table class="layui-table layui-referGrid" lay-size="sm" lay-even layui-filter="referGrid_' + referFilter + '" id="referGrid_' + referFilter + '" ></table></div>',
|
area:[(($webUtil.isNotNull(options.width) && options.width*1>1) ?options.width*1 : 1000) +'px',
|
(($webUtil.isNotNull(options.height) && options.height*1>1) ?options.height*1 : 625) + 'px'],
|
btnBefore:valueInfo,
|
resizing:function(layero){
|
me.doResize(filter,name);
|
},
|
yes:function(index,layero){
|
var selectValue = me.getSelectValue(filter,name,isMuti);
|
if(selectValue){
|
target.setValue(filter,name,selectValue.value,selectValue.rawValue,true,selectValue.allValues);
|
layer.close(referIndex);
|
}
|
},
|
success:function(layero){
|
me.showGrid(filter,name,options,value,text);
|
}
|
});
|
};
|
//显示
|
ReferGrid.prototype.showGrid=function(filter,name,options,value,rawValue){
|
var that = this;
|
that.setConfig(filter,name,options);
|
|
if($('#referGrid_' + filter + '_' + name).children().length>0){
|
$('#referGrid_' + filter + '_' + name).show();
|
}else{
|
var tableId = 'referGrid_' + filter + '_' + name;
|
layui.use('table',function(){//因为table里引用了form ,form里引用了这里
|
var table = layui.table;
|
var isMuti = $webUtil.getBoolean($webUtil.getValueFromObj(options,['isMuti'],false));
|
var requestData = {};
|
if(options.extraParams ){
|
for(var key in options.extraParams){
|
requestData[key] = options.extraParams[key];
|
}
|
}
|
if(options.useFormKey && options.formValues){
|
//使用表单上的字段来过滤
|
requestData['conditionMap["' + (options.paramForFormKey?options.paramForFormKey:options.useFormKey) + '"]'] = options.formValues[options.useFormKey];
|
}
|
if(options.where ){
|
for(var key in options.where){
|
requestData['conditionMap["' + key + '"]'] = options.where[key];
|
}
|
}
|
var copyParam = ['referBo','textField','valueField','displayTable','whereSql','isMuti','queryScheme'];
|
for(var i= 0 ; i < copyParam.length ; i ++){
|
requestData[copyParam[i]] = options[copyParam[i]];
|
}
|
if(!requestData['referBo']){
|
requestData['referBo'] = options.referType;
|
}
|
if($webUtil.isNotNull(options.displayTable)){
|
//说明是使用平台的表格
|
$webUtil.post(that.defaultConfig.tableDefinedUrl,{btmType:(options.referBo||options.referType),code:options.displayTable,isMuti:isMuti},function(result){
|
if(result.success){
|
var queryScheme = result.querySchema;
|
if($webUtil.isNotNull(queryScheme) && $webUtil.isNull(options.queryScheme)){
|
options.queryScheme = queryScheme;
|
}
|
requestData['queryScheme'] = options.queryScheme;
|
var cols = [];
|
if(result.showIndex){
|
cols.push({field:'LAY_TABLE_INDEX',type:'numbers',title:'序号'});
|
}
|
if(result.showCheckbox){
|
cols.push({field:'LAY_CHECKED',type:'checkbox'});
|
}
|
var referColumn = "";
|
if(result.columns){
|
layui.each(result.columns,function(_index,record){
|
var col = record;
|
if(col.field.indexOf(".")>-1){
|
referColumn += col.field + ",";
|
col.field = col.field.replace(".","_");
|
}
|
if($webUtil.isNotNull(col.enumCode)){
|
//是枚举的内容
|
var vciWebComboxStore = layui.vciWebComboxStore;
|
vciWebComboxStore.newCombox(col.enumCode,{
|
data:col.enumMap
|
});
|
col.templet=function(d,thisItem){
|
var vciWebComboxStore = layui.vciWebComboxStore;
|
return vciWebComboxStore.getComboxText(thisItem.enumCode,d[thisItem.field]);
|
};
|
}
|
cols.push(col);
|
});
|
}
|
requestData['referColumn'] = referColumn;
|
var pageObject = false;
|
if(result.limit>0){
|
pageObject = {
|
limit:result.limit,
|
page:1
|
};
|
}
|
//可以定义表格了
|
table.render({
|
elem:'#referGrid_' + filter + '_' + name,
|
id:tableId,
|
backPath:options.backPath?options.backPath:that.backPath,
|
url:options.url,
|
page:pageObject,
|
height:options.height?options.height:475,
|
where:requestData,
|
selectMode:isMuti?table.selectMode.muti:table.selectMode.single,
|
cols:[cols],
|
initSort:options.sortKey,
|
remoteSort:options.remoteSort
|
});
|
that.showSearchHtml(tableConfig.queryColumns,tableId,filter,name,requestData);
|
}else{
|
$webUtil.showErrorMsg(result.msg);
|
}
|
});
|
}else{
|
//说明是自定义的表格
|
var tableConfig = options.tableConfig;
|
if(!tableConfig){
|
$webUtil.showErrorMsg("没有定义参照的表格配置");
|
}
|
if(!requestData){
|
requestData = {
|
currentUserReferModelKey:options.currentUserReferModelKey,
|
currentUserReferMethodKey:options.currentUserReferMethodKey
|
};
|
}else{
|
requestData['currentUserReferModelKey'] = options.currentUserReferModelKey;
|
requestData['currentUserReferMethodKey'] = options.currentUserReferMethodKey;
|
}
|
if(options.tableConfig && options.tableConfig.cols && options.tableConfig.cols.length>0){
|
//说明传递了的
|
var hasIndex = false;
|
var hasCheck = false;
|
layui.each(options.tableConfig.cols,function (_index,_item){
|
if(_item.field == layui.table.config.indexName){
|
hasIndex = true;
|
}
|
if(_item.field == layui.table.config.checkName){
|
hasCheck = true;
|
}
|
});
|
if(!hasIndex){
|
options.tableConfig.cols.unshift(layui.table.getIndexColumn());
|
}
|
if(!hasCheck){
|
options.tableConfig.cols.unshift(layui.table.getCheckColumn());
|
}
|
}
|
//可以定义表格了
|
table.render({
|
elem:'#referGrid_' + filter + '_' + name,
|
id:tableId,
|
backPath:options.backPath?options.backPath:that.backPath,
|
url:options.url,
|
page:tableConfig.page,
|
height:options.height?options.height:475,
|
where:requestData,
|
method:options.method,
|
selectMode:isMuti?table.selectMode.muti:table.selectMode.single,
|
cols:[tableConfig.cols],
|
initSort:options.initSort,
|
remoteSort:options.remoteSort
|
});
|
that.showSearchHtml(tableConfig.queryColumns,tableId,filter,name,requestData);
|
}
|
});
|
}
|
};
|
ReferGrid.prototype.showSearchHtml =function(queryColumns,tableId,filter,name,requestData){
|
//设置查询区域
|
var that = this;
|
var table = layui.table;
|
var toolbarElem = $('[layui-filter="referGrid_toolbar_' + filter + '_' + name +'"]');
|
if(queryColumns){
|
var toolbarHtml = '<div class="layui-form-item" style="height:24px;float:left;display:inline-block"> '
|
+ '<div class="layui-input-inline" style="height:24px;line-height:24px;width:120px;min-width: 120px"><select name="referGrid_search_'+ filter + '_' + name +'" style="height:24px;width:120px;padding-right: 10px;">';
|
layui.each(queryColumns,function(_index,queryRecord){
|
toolbarHtml += '<option value="' + queryRecord.field + '" >' + queryRecord.title + '</option>';
|
});
|
toolbarHtml += '</select></div>';
|
toolbarHtml += '<div class="layui-input-inline"><input type="text" style="height:24px" name="referGrid_searchvalue_'+ filter + '_' + name +'" placeholder="输入值后进行模糊查询" class="layui-input" ></div>';
|
toolbarHtml += '<div class="layui-input-inline" ><button class="layui-btn" style="height:24px;line-height:24px" name="referGrid_searchbtn_'+ filter + '_' + name +'" >查询</button></div>';
|
toolbarHtml += '</div>';
|
toolbarElem.html(toolbarHtml);
|
layui.form.render('select');
|
$('[name="referGrid_searchbtn_'+ filter + '_' + name +'"]').click(function(){
|
var searchField = $(this).parent().parent().find('select').val();
|
var searchValue = $(this).parent().parent().find('[name="referGrid_searchvalue_' + filter + '_' + name + '"]').val();
|
that.queryByCondition(tableId,requestData,searchField,searchValue);
|
});
|
$('[name="referGrid_searchvalue_'+ filter + '_' + name +'"]').keydown(function(e){
|
if(e.keyCode == 13) {
|
var searchField = $(this).parent().parent().find('select').val();
|
var searchValue = $(this).val();
|
that.queryByCondition(tableId, requestData, searchField,searchValue);
|
}
|
});
|
$('[name="referGrid_searchvalue_'+ filter + '_' + name +'"]').focus()
|
}
|
};
|
ReferGrid.prototype.queryByCondition = function(tableId,requestData,searchField,searchValue){
|
if($webUtil.isNotNull(searchValue)){
|
searchValue = "*" + searchValue + "*";
|
}
|
requestData['conditionMap["' + searchField + '"]'] = searchValue;
|
requestData['page']= 1;
|
var table = layui.table;
|
table.reload(
|
tableId, {
|
where: requestData
|
}
|
);
|
}
|
ReferGrid.prototype.setConfig=function(filter,name,options){
|
var that = this;
|
if(!that.referConfig){
|
that.referConfig = {};
|
}
|
that.referConfig[filter + "_" + name] = options;
|
};
|
ReferGrid.prototype.getConfig=function(filter,name){
|
var that = this;
|
if(!that.referConfig){
|
that.referConfig = {};
|
}
|
if((filter + "_" + name) in that.referConfig){
|
return that.referConfig[filter + "_" + name];
|
}
|
};
|
ReferGrid.prototype.getSelectValue = function(filter,name,isMuti){
|
var that = this;
|
var table = layui.table;
|
var config = that.getConfig(filter,name);
|
var selectValue = {};
|
var checkStatus = table.checkStatus('referGrid_' + filter + "_" + name);
|
if(checkStatus.data.length>0){
|
if(!isMuti && checkStatus.data.length>1){
|
$webUtil.showErrorMsg("每次只能选择一条数据");
|
return false;
|
}
|
var value = "";
|
var rawValue = "";
|
config.valueField = config.valueField?config.valueField:that.defaultConfig.valueField;
|
config.textField = config.textField?config.textField:that.defaultConfig.textField;
|
var isMutiValue = (config.valueField.indexOf(",")>-1);
|
var isMutiRaw = (config.textField.indexOf(",")>-1);
|
layui.each(checkStatus.data,function(_index,item){
|
if(isMutiValue){
|
var valueFieldArray = config.valueField.split(",");
|
layui.each(valueFieldArray,function(_indexField,_itemField){
|
value += item[_itemField] + (config.valueSep?config.valueSep:' ');
|
});
|
value+= ",";
|
}else {
|
value += item[config.valueField] + ",";
|
}
|
if(isMutiRaw) {
|
var rawFieldArray = config.textField.split(",");
|
layui.each(rawFieldArray,function(_indexField,_itemField){
|
rawValue += item[_itemField] + (config.textSep?config.textSep:' ');
|
});
|
rawValue+= ",";
|
}else{
|
rawValue += item[config.textField] + ",";
|
}
|
});
|
if($webUtil.endWith(value,",")){
|
value = value.substring(0,value.length - 1);
|
}
|
if($webUtil.endWith(rawValue,",")){
|
rawValue = rawValue.substring(0,rawValue.length -1);
|
}
|
selectValue = {
|
value : value,
|
rawValue : rawValue,
|
allValues: checkStatus.data
|
};
|
return selectValue;
|
}else{
|
return false;
|
}
|
};
|
ReferGrid.prototype.doResize = function(filter,name){
|
|
};
|
|
ReferGrid.prototype.destory = function(filter,name){
|
//销毁
|
var that = this;
|
if($('#referGrid_' + filter + '_' + name)){
|
$('#referGrid_' + filter + '_' + name).remove();
|
}
|
if(!that.referConfig){
|
that.referConfig = {};
|
}
|
if((filter + "_" + name) in that.referConfig){
|
delete that.referConfig[filter + "_" + name];
|
}
|
};
|
var rg = new ReferGrid();
|
exports('vciWebReferGrid',rg);
|
});
|