EasyUI Datagrid表格在页面自适应高度处理
页面的结构
Datagrid表格插件,我使用的js控制表格的生成,页面的接口为:
<div id="cou_table"></div><!--表格数据-->
<div class="pagelists" id="cou_tablePage" ></div>
这里,第一个div用于装载表格,第二个div用于生成分页(因为我没有使用Datagrid表格自带的分页)
js装载表格部分
$("#cou_table").datagrid({
url: urp,
menthod: "get",
loadFilter: function (data) { //数据过滤
var arrTablerecord = data.tablerecord;
if (data.recordcount === '0'||data.result == 'fail') {
layer.msg('未获取到符合条件的数据',{time:2600});
$("#cou_tablePage").whjPaging('setPage', {currPage:0,totalPage: 0,totalSize:0});
return [];
}else{
for(var i = 0;i< arrTablerecord.length;i++){
if(arrTablerecord[i].BusinessContactInfos != '' && arrTablerecord[i].BusinessContactInfos != null){
arrTablerecord[i].ConnectPerson = arrTablerecord[i].BusinessContactInfos[0].TrueName
}
//注册地区
if(arrTablerecord[i].ProvinceInfo != null && arrTablerecord[i].ProvinceInfo != ''){
arrTablerecord[i].RegAddress = arrTablerecord[i].ProvinceInfo.ProvinceName;
}else{
arrTablerecord[i].RegAddress = '/';
}
if(arrTablerecord[i].CityInfo != null && arrTablerecord[i].CityInfo != ''){
arrTablerecord[i].RegAddress = arrTablerecord[i].ProvinceInfo.ProvinceName +'-'+ arrTablerecord[i].CityInfo.CityName;
}
if(arrTablerecord[i].CountyInfo != null && arrTablerecord[i].CountyInfo != ''){
arrTablerecord[i].RegAddress = arrTablerecord[i].ProvinceInfo.ProvinceName +'-'+ arrTablerecord[i].CityInfo.CityName +'-'+ arrTablerecord[i].CountyInfo.CountyName;
}
}
}
var recordcount = data.recordcount/pageSizeA <= 1 ? 1 : Math.ceil(data.recordcount / pageSizeA);
$("#cou_tablePage").whjPaging('setPage', {currPage:pageNumberA,totalPage: recordcount,totalSize:data.recordcount});
return arrTablerecord;
},
columns: [[
{ field: "", title: "",checkbox: true },
// { field: "CompanysID", title: "编号", width: 30, resizable: true, align: 'center' },
{ field: "CompanyName", title: "客户名称", width: 150,resizable: true, align: 'center' },
{ field: "CustomerPropertyName", title: "客户属性", width: 100, resizable: true, align: 'center' },
{ field: "CustomerGradeName", title: "客户等级", width: 100, resizable: true, align: 'center' },
{ field: "RegAddress", title: "注册地区", width: 100, resizable: true, align: 'center' },
{ field: "Operator", title: "操作员", width: 100, resizable: true, align: 'center' },
{ field: "ConnectPerson", title: "跟踪人员", width: 100, resizable: true, align: 'center' },
{ field: "StatusName", title: "审核状态", width: 100, resizable: true, align: 'center' },
{ field: "LastContactTime", title: "上次联系时间", width: 100, resizable: true, align: 'center' },
{ field: "LastContactUser", title: "上次联系人", width: 100, resizable: true, align: 'center' }
]],
onLoadSuccess:function(data){
if(parameterS.CustomerProperty == 1 || parameterS.CustomerProperty == 2){
$("#cou_table").datagrid("hideColumn", "StatusName"); // 设置隐藏列
}
},
striped: true,//斑马色
checkOnSelect: true,
multiSort: true,//
nowrap: false,//换行
fitColumns: true,//宽度自适应
});
根据Datagrid表格官方给的属性和方法,很容易装载出一个我们需要的表格,但是我们会发现,表格的高度完全是根据自己设定一页显示多少条数据来决定的,一页显示50条数据的表格肯定比显示30条更高,表格过长则会超出浏览器窗口高度,这时候就出现了滚动条,通过滚动可以看到隐藏在浏览器下方的表格数据,如果我们有搜索条件和其他按钮操作,就会被隐藏掉,所以自适应高度很重要;
控制表格高度
要控制表格高度,首先需要知道表格应该显示多少的高度,一般来说:
表格显示高度 = 浏览器窗口高度- 搜索条件高度 - 功能按钮高度 - 分页栏高度;
(因为我的分页栏是另外用的插件,并不是表格自带的,所以要减去高度)
var height=(Number($(window).height())-Number($("#cou_tablePage").height()) - 66); // 计算高度
$("#cou_table").datagrid({
height : height,
width: '100%'
});
这样子就可以让表格的高度自适应了!
但是随之又出现了一个新问题,调用的接口被加载了两次?!
这个问题困扰了我很久,但是我一直认为问题应该是出在:
一次装载表格,一次控制高度,用了两次 $("#cou_table").datagrid({}),这个的两次出现导致了表格的两次装载。
url加载两次的解决方法
第一次装载url肯定没有问题,应该在控制表格高度的代码上找原因,终于被我发现一个神奇的方法,把控制表格高度的代码修改一下:
$("#cou_table").datagrid(=='resize'==,{
height : height,
width: '100%'
});
是的,加上这个就完美了,其实这都是文档上有的,一开始还是对文档不够仔细啊。