How to set Extjs Toolbar items' width to expand the whole 工具栏项的宽度设为百分比
2012-03-07 15:23
281 查看
Extjs 的toolbar 是一个快速定制的工具栏组件,通过给items:[]赋值,可以添加需要的element对象。
例如给grid tbar建一个简单的URL浏览器的导航条
new Ext.Toolbar({
height : 30,
region : 'center',
width:'100%',
items : [
'URL',
new Ext.form.TextField({
id : 'txt_url',
width : '500',
value : this.workdir,
}),
new Ext.Button({
id : 'btn_url',
icon : "./images/file/go.png",
cls : "x-btn-text-icon",
handler : this.showUrl.createDelegate(this)
})]
});
设置好,预览发现,中间的TextField的宽度没有展开,当窗口变大后,就会出现多余的空白,很难看。
于是把width : '500'修改为width : '100%',预览发现,TextField并没有伸展。而其父节点toolbar的div的width已设为100%并自动展开,为什么?
查看dom发现,Extjs在实现Items时,其html表示为<table>
<div id="ext-comp-1089" class="x-toolbar
x-small-editor" style="width: 100%; height: 25px;">
<table cellspacing="0">
<tbody>
<tr>
<td>
<span id="ext-gen1091" class="ytb-text">URL:</span>
</td>
<td id="ext-gen1092" >
<input id="file-win2txt_url" class="
x-form-text x-form-field" type="text" name="file-win2txt_url" autocomplete="off" size="20" style="width:
100%;">
</td>
<td id="ext-gen1098">
<table id="file-win2btn_url" class="x-btn-wrap
x-btn x-btn-text-icon" cellspacing="0" cellpadding="0" border="0" style="width:
auto;">
</td>
</tr>
</tbody>
</table>
</div>
由于Ext不会把input父dom的td也设为100%,因此input的width最大就和td默认的width一样宽,这样就会造成一个100%无效的假象。其实,TextField配置的width : '100%'已经生效,它的width等于其父td的width乘以100%,父亲的width决定了儿子的width。现在只需要下面的html片段
<td id="ext-gen1092" >
<input id="txt_url" class="
x-form-text x-form-field" type="text" name="file-win2txt_url" autocomplete="off" size="20" style="width:
100%;">
</td>
变为
<td id="ext-gen1092" style="width:
100%;">
<input id="txt_url" class="
x-form-text x-form-field" type="text" name="file-win2txt_url" autocomplete="off" size="20" style="width:
100%;">
</td>
就正确了。
但是如何做呢?
经过我的尝试,发现一个workaround的方法,在toolbar重写afterRender,设置其parent的width就可以。
afterRender:function()
{
Ext.Toolbar.prototype.afterRender.apply(this, arguments);
var pn=Ext.getCmp( 'txt_url').getEl();
pn.dom.parentNode.style.width =
'100%';
}
附全码:
var tb3 = new Ext.Toolbar({
height : 30,
region : 'center',
width:'100%',
items : [‘URL’, new Ext.form.TextField({
id : 'txt_url',
width : '100%',
value : this.workdir,
listeners : {
specialkey : function(field, e) {
if (e.getKey() == Ext.EventObject.ENTER) {
// this.grid.getSelectionModel().clearSelections(true);
this.showUrl();
}
}.createDelegate(this)
}
}), new Ext.Button({
id : 'btn_url',
icon : "./images/file/go.png",
cls : "x-btn-text-icon",
handler : this.showUrl.createDelegate(this)
})]
,
afterRender:function()
{
Ext.Toolbar.prototype.afterRender.apply(this, arguments);
var pn=Ext.getCmp( 'txt_url').getEl();
pn.dom.parentNode.style.width = '100%';
}
});
例如给grid tbar建一个简单的URL浏览器的导航条
new Ext.Toolbar({
height : 30,
region : 'center',
width:'100%',
items : [
'URL',
new Ext.form.TextField({
id : 'txt_url',
width : '500',
value : this.workdir,
}),
new Ext.Button({
id : 'btn_url',
icon : "./images/file/go.png",
cls : "x-btn-text-icon",
handler : this.showUrl.createDelegate(this)
})]
});
设置好,预览发现,中间的TextField的宽度没有展开,当窗口变大后,就会出现多余的空白,很难看。
于是把width : '500'修改为width : '100%',预览发现,TextField并没有伸展。而其父节点toolbar的div的width已设为100%并自动展开,为什么?
查看dom发现,Extjs在实现Items时,其html表示为<table>
<div id="ext-comp-1089" class="x-toolbar
x-small-editor" style="width: 100%; height: 25px;">
<table cellspacing="0">
<tbody>
<tr>
<td>
<span id="ext-gen1091" class="ytb-text">URL:</span>
</td>
<td id="ext-gen1092" >
<input id="file-win2txt_url" class="
x-form-text x-form-field" type="text" name="file-win2txt_url" autocomplete="off" size="20" style="width:
100%;">
</td>
<td id="ext-gen1098">
<table id="file-win2btn_url" class="x-btn-wrap
x-btn x-btn-text-icon" cellspacing="0" cellpadding="0" border="0" style="width:
auto;">
</td>
</tr>
</tbody>
</table>
</div>
由于Ext不会把input父dom的td也设为100%,因此input的width最大就和td默认的width一样宽,这样就会造成一个100%无效的假象。其实,TextField配置的width : '100%'已经生效,它的width等于其父td的width乘以100%,父亲的width决定了儿子的width。现在只需要下面的html片段
<td id="ext-gen1092" >
<input id="txt_url" class="
x-form-text x-form-field" type="text" name="file-win2txt_url" autocomplete="off" size="20" style="width:
100%;">
</td>
变为
<td id="ext-gen1092" style="width:
100%;">
<input id="txt_url" class="
x-form-text x-form-field" type="text" name="file-win2txt_url" autocomplete="off" size="20" style="width:
100%;">
</td>
就正确了。
但是如何做呢?
经过我的尝试,发现一个workaround的方法,在toolbar重写afterRender,设置其parent的width就可以。
afterRender:function()
{
Ext.Toolbar.prototype.afterRender.apply(this, arguments);
var pn=Ext.getCmp( 'txt_url').getEl();
pn.dom.parentNode.style.width =
'100%';
}
附全码:
var tb3 = new Ext.Toolbar({
height : 30,
region : 'center',
width:'100%',
items : [‘URL’, new Ext.form.TextField({
id : 'txt_url',
width : '100%',
value : this.workdir,
listeners : {
specialkey : function(field, e) {
if (e.getKey() == Ext.EventObject.ENTER) {
// this.grid.getSelectionModel().clearSelections(true);
this.showUrl();
}
}.createDelegate(this)
}
}), new Ext.Button({
id : 'btn_url',
icon : "./images/file/go.png",
cls : "x-btn-text-icon",
handler : this.showUrl.createDelegate(this)
})]
,
afterRender:function()
{
Ext.Toolbar.prototype.afterRender.apply(this, arguments);
var pn=Ext.getCmp( 'txt_url').getEl();
pn.dom.parentNode.style.width = '100%';
}
});
相关文章推荐
- How to set the width of columns?about chart control for .net
- How to set the default input focus on a field in an HTML web form
- How to get and set the drawing order of layers in globe(获取并设置Globe图层的叠加次序:)
- How to expand the BOM using ABAP?
- How to get a notification from Linux when the set of network interfaces changes
- How to add a user and set environments for the us
- 学习:How To Use Filters in SharePoint to show items in the current Calendar Month(转)
- How to set the DefaultButton in a Page Based on ASP.NET Master Page
- How To List All The Named Events Set For A Database [ID 436036.1]
- 关于JAVA界面风格(How to Set the Look and Feel)
- How to make fundamental Winsock calls to set up communication using the internet Protocol
- How to expand the BOM using ABAP?
- 轮廓算法的结果验证工具/How to validate the outline output
- How to Set Up SQL Server 2008 FileStream In the Cluster
- svn : how to set the executable bit on a file?
- How to set the style in valuelist
- How to stop the gridspliter when the height or width reach the thresholds
- How to fix the issue that GEM_HOME and/or GEM_PATH not set issue for rvm in mac version 10.12
- How to Set Today theme to default theme.
- [eZ publish] How to set the SSL to a node?