日期:2014-05-16  浏览次数:20402 次

ExtJS GridPanel根据窗口大小自动变化插件

?

Ext.ns("Ext.grid.plugins");

Ext.grid.plugins.AutoResize = Ext.extend(Ext.util.Observable,{
	init:function(grid){
		grid.applyToMarkup = function(el){
			grid.render(el);
		}
		var containerId = Ext.get(grid.renderTo || grid.applyTo).id;
		if(Ext.isIE){
			Ext.get(containerId).on("resize",function(){
				grid.setWidth.defer(100,grid,[Ext.get(containerId).getWidth()]);
			});
		}else{
			window.onresize = function(){
				grid.setWidth(Ext.get(containerId).getWidth());
			}
		}
	}
});

?

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>autoresize</title>
	<link rel="stylesheet" type="text/css" href="http://dev.sencha.com/deploy/dev/resources/css/ext-all.css" />
	<link rel="stylesheet" type="text/css" href="http://dev.sencha.com/deploy/dev/resources/css/xtheme-gray.css" />
 	<script type="text/javascript" src="http://dev.sencha.com/deploy/dev/adapter/ext/ext-base-debug.js"></script>
    <script type="text/javascript" src="http://dev.sencha.com/deploy/dev/ext-all-debug.js"></script>
	<script type="text/javascript" src="http://dev.sencha.com/deploy/dev/src/locale/ext-lang-zh_CN.js"></script>

	<link rel="stylesheet" type="text/css" href="ext-patch.css" />
    <script type="text/javascript" src="AutoResize.js"></script>
    <script type="text/javascript">
		Ext.onReady(function(){
			var data = new Array();
			for(var i=1;i<=100;i++){
				data.push({id:i,name:"name"+i,sex:i%2 == 0?"man":"woman",age:i});
			}
			var grid = new Ext.grid.GridPanel({
				height:600,
				plugins:new Ext.grid.plugins.AutoResize(),
				frame:true,
				title:"Grid",
				//renderTo:"gridPanel",
				applyTo:"gridPanel",
				viewConfig:{
					forceFit:true
				},
				columns:[
					{dataIndex:"id",header:"id",sortable:true},
					{dataIndex:"name",header:"name",sortable:true},
					{dataIndex:"sex",header:"sex",sortable:true},
					{dataIndex:"age",header:"age",sortable:true}
				],
				store:new Ext.data.JsonStore({
					autoLoad:true,
					data:data,
					fields:["id","name","sex","age"]
				})
			});
		});    
    </script>
</head>
<body style="margin:0px;padding:0px;">
	<div id="gridPanel" style="width: 100%;"></div>
</body>
</html>
1 楼 gepangtsai 2011-12-06  
Ext.ns("Ext.grid.plugins");
Ext.grid.plugins.AutoResize = Ext.extend(Ext.util.Observable, {
	init : function(grid) {
		var id = grid.getId();
		grid.applyToMarkup = function(el) {
			grid.render(el);
		};
		// || Ext.get(grid.renderTo || grid.applyTo).id;
		var containerId = 'center';
		obj = Ext.getCmp(containerId);
		PMS.ctrl.autosize = obj;
		
		var width = obj.getWidth();
		var height = obj.getHeight();
		grid.setWidth(width - 20);
		grid.setHeight(height - 90);

		obj.on("resize", function(panel, adjWidth, adjHeight, rawWidth,
				rawHeight) {
			//通过ID获取对象本身,如果是对象传递当对象关闭后就会出现著名的:this.dom is undefined
			var grid = Ext.getCmp(id);
			grid.setWidth(adjWidth - 20);
			grid.setHeight(adjHeight - 90);
		});
	}
});
2 楼 gepangtsai 2011-12-06  
再判断下:
if (grid) {
				grid.setWidth(adjWidth - 20);
				grid.setHeight(adjHeight - 90);
			}