固定宽度表格布局设定宽度问题

近日做  GridLite 轻量级的Ext Grid 的列宽拖放调整时遇到了一点小问题,记一下呵

 

首先看下面一段代码:

 

动态生成一个 table-layout:fixed 的表格 (某一列内容很多不换行,并设置该列宽度),然后待table渲染后再设置整个 table的css宽度。

 

table.gridLite {
	border-collapse:collapse;
	table-layout:fixed;
	text-align:left;
	border:1px solid #D0D0D0;
	font-size:12px;
}


.x-grid3-hd-inner {
       overflow:hidden;
       white-space:nowrap;

}
.x-grid3-cell {
       overflow:hidden;
}


var table2 = Ext.DomHelper.append(document.body, {
    tag: 'table',
    cls: 'gridLite',
    cellspacing: "0",
    cellpadding: "0",
    //放其后设置 固定表格布局在 firefox 和 chrome 中失效 !!!
    //style:'width:800px',
    cn: [{
        tag: 'caption',
        html: 'test'
    },
    {
        tag: 'colgroup',
        cn: [{
            tag: 'col'
        },
        {
            tag: 'col'
        }]
    },
    {
        tag: 'thead',
        cn: [{
            tag: 'tr',
            cn: [{
                tag: 'th',
                cls: 'x-grid3-cell',
                style: 'width:21px;',
                cn: [{
                    tag: 'div',
                    cls: "x-grid3-hd-inner",
                    html: ' '
                }]
            },
            {
                tag: 'th',
                cls: 'x-grid3-cell',
                style: 'width:50px;',
                cn: [{
                    tag: 'div',
                    cls: 'x-grid3-hd-inner',
                    html: '我们 我们 我们 我们 我们 我们 我们 我们 我们 我们 我们 我们 我们 我们'
                }]
            },
            {
                tag: 'th',
                cls: 'x-grid3-cell',
                cn: [{
                    tag: 'div',
                    cls: 'x-grid3-hd-inner',
                    html: '1'
                }]
            }]
        }]
    }]
},
true);
//放其后设置 固定表格布局在 firefox 和 chrome 中失效 !!!ie opera 可行
table2.setStyle({
    width: '800px'
});
 

 

效果:

 

IE.Opera :

 

Firefox .Chrome

 

若把css width 创建表格时初始设置,则各个浏览器没有区别。

 

确实这方面标准没有规定,其实 firefox ,chrome 在初始没有设定css width时,即使设了 固定宽度表格布局,它们仍是按照自由宽度表格布局来处理的,后期设了表格宽度也没用了。(关于 自由宽度表格布局列宽度计算,有兴趣可以查阅 css权威指南 表的自动布局,很复杂 )


所以最好 table-layout:fixed 和 width 成对出现,可以设个默认值 100%  (后期可以再更改):

 

table.gridLite {
	border-collapse:collapse;
	table-layout:fixed;
	text-align:left;
	border:1px solid #D0D0D0;
	font-size:12px;
	/*fixed width 成对设置,否则部分浏览器会当做 自动宽度布局模型*/
	width:100%;
}
 

 

以下引自:css权威指南


注意:使用固定宽度布局模型时,没有必要非得为表指定一个显示宽度,不过如果指定一个宽度确实有帮助。不过这不是必要的。 用户代理也可以使用自动宽度布局模型对width值为auto的表完成布局。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值