我创建了另一种解决方案,当您的 html 不同时,它不仅是网格,而且它上面的其他内容也不同。 JSFiddle 位于此处。
HTML
<div class="container">
<div class="test">
hey there
</div>
<div id="grid"></div>
</div>
CSS
html,
body {
margin: 0;
padding: 0;
height: 100%;
}
.container{
height:100%;
}
.test{
height:100px;
}
html {
overflow: hidden;
}
JS
function resizeGrid() {
var gridElement = $("#grid");
var dataArea = gridElement.find(".k-grid-content");
var newHeight = gridElement.parent().innerHeight() - 2 -calcHeightsOfParentChildren(gridElement);
var diff = gridElement.innerHeight() - dataArea.innerHeight();
if((newHeight-diff)>0){
gridElement.height(newHeight);
dataArea.height(newHeight - diff);
}
}
function calcHeightsOfParentChildren(element){
var children = $(element).parent().children();
var height = 0;
$.each(children, function( index, value ) {
if($(value).attr("id") != $(element).attr("id")){
height = height + $(value).height();
}
});
return height;
}
$(window).resize(function() {
resizeGrid();
});
$("#grid").kendoGrid({
dataSource: {
type: "odata",
transport: {
read: "http://demos.kendoui.com/service/Northwind.svc/Orders"
},
schema: {
model: {
fields: {
OrderID: {
type: "number"
},
ShipName: {
type: "string"
},
ShipCity: {
type: "string"
}
}
}
},
pageSize: 10,
serverPaging: true,
serverFiltering: true,
serverSorting: true
},
height: 250,
filterable: true,
sortable: true,
pageable: true,
dataBound: resizeGrid,
columns: [{
field: "OrderID",
filterable: false
},
"ShipName",
"ShipCity"
]
});
我的解决方案中的关键是修改后的 resizeGrid 函数。没有这种修改会发生什么,如果用户向上滚动足够远,底部寻呼机就会丢失!通过检查以确保 newHeight-diff 大于 0,它可以防止此错误。
第二个 calcHeightsOfParentChildren 在传递有问题的网格元素时将计算除其自身之外的所有其他高度,以帮助计算放置寻呼机控件和网格的正确差异,使其真正占据 100% 并保持其 100% 的比例.