现在的位置: 首页 > 综合 > 正文

easyui datagrid 浏览器像素及改变表、列宽问题

2013年12月06日 ⁄ 综合 ⁄ 共 2936字 ⁄ 字号 评论关闭

title:easyui datagrid表格宽度,以及列宽随浏览器缩放改变

在使用easyui的datagrid时,需要考虑到浏览器不同的像素问题,所以,在使用时,我们需要自己写一个函数

code:

		function getWidth(percent){
		    return $(window).width() * percent;
		}

这样在初始化时:

$("#tt").datagrid({
    width: getWidth(0.6)
});	    

当然,有时候我们会考虑浏览器缩放,也要改变其中显示,这时,加上一个resize事件就可以了

			 $(window).resize(function(){
				//alert("change....");
				$("#tt").datagrid({
					width: getWidth(0.6)
				});	    		 
			});

甚至,我们需要考虑到列宽也需要改变(因情况,条件而定,这里举一例),我们需要在写一个函数:

		function fixWidthTable(percent){
	    	    return getWidth(0.6) * percent;
		}

下面是完整的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>DataGrid ContextMenu - jQuery EasyUI Demo</title>
	<link rel="stylesheet" type="text/css" href="../themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="../themes/icon.css">
	<link rel="stylesheet" type="text/css" href="demo.css">
	<script type="text/javascript" src="../jquery-1.7.1.min.js"></script>
	<script type="text/javascript" src="../jquery.easyui.min.js"></script>
	<script>

		function fixWidthTable(percent){
	    	    return getWidth(0.6) * percent;
		}
		
		function getWidth(percent){
		    return $(window).width() * percent;
		}

		$(function(){
			 $(window).resize(function(){
				//alert("change....");
				$("#tt").datagrid({
					width: getWidth(0.6)
				});	    		 
			});

			$('#tt').datagrid({
				url: 'datagrid_data2.json',
				title: 'DataGrid - ContextMenu',
				width: getWidth(0.6),
				height: 'auto',
				fitColumns: true,
				columns:[[
				{field:'itemid',title:'Item ID',width:fixWidthTable(0.12)},
				{field:'productid',title:'Product ID',width:fixWidthTable(0.15)},
				{field:'listprice',title:'List Price',width:fixWidthTable(0.12),align:'right'},
				{field:'unitcost',title:'Unit Cost',width:fixWidthTable(0.12),align:'right'},
				{field:'attr1',title:'Attribute',width:fixWidthTable(0.38)},
				{field:'status',title:'Status',width:fixWidthTable(0.11),align:'center'}
			     ]],
				onHeaderContextMenu: function(e, field){
					e.preventDefault();
					if (!$('#tmenu').length){
						createColumnMenu();
					}
					$('#tmenu').menu('show', {
						left:e.pageX,
						top:e.pageY
					});
				}
			});
		});
		
		function createColumnMenu(){
			var tmenu = $('<div id="tmenu" style="width:100px;"></div>').appendTo('body');
			var fields = $('#tt').datagrid('getColumnFields');
			for(var i=0; i<fields.length; i++){
				$('<div iconCls="icon-ok"/>').html(fields[i]).appendTo(tmenu);
			}
			tmenu.menu({
				onClick: function(item){
					if (item.iconCls=='icon-ok'){
						$('#tt').datagrid('hideColumn', item.text);
						tmenu.menu('setIcon', {
							target: item.target,
							iconCls: 'icon-empty'
						});
					} else {
						$('#tt').datagrid('showColumn', item.text);
						tmenu.menu('setIcon', {
							target: item.target,
							iconCls: 'icon-ok'
						});
					}
				}
			});
		}
	</script>
</head>
<body>
	<h2>DataGrid - ContextMenu</h2>
	<div class="demo-info" style="margin-bottom:10px">
		<div class="demo-tip icon-tip"></div>
		<div>Right click the header of datagrid to show context menu.</div>
	</div>
	
	<table id="tt"></table>
	
</body>
</html>

不过在做测试中,遇到了各种奇怪的问题,今天早上一看,发现没有昨天的问题了,暴汗················

也不知道是什么原因!

如果这文有幸有高手看到,并发现了问题,或者有解决方案,贴出来也让俺瞅瞅········先谢谢啦························

上面的窗口大小改变事件中用的是重新加载,这样速度会慢很多,而API中提供了resize方法,所以改为:

$("#tt").datagrid("resize",{width:getWidth(0.6)});

抱歉!评论已关闭.