PDA

View Full Version : docked toolbar and button positioning. align attribute is ignored.



profunctional
30 Jun 2010, 12:54 PM
I have the following code below. Setting aligned to both left and right does nothing. Both buttons are left aligned. I also tried setting the alignment to centered. The buttons are still left aligned. Is there any reason why these are ignored?

dockedItems:
[
{
dock: 'top',
title: 'Dashboard',
xtype: 'toolbar',
items:
[
{
xtype: 'button',
ui: 'mask',
iconCls: 'refresh',
stretch: false,
align: 'left',
handler: refresh
},
{
xtype: 'button',
ui: 'mask',
iconCls: 'add',
stretch: false,
align: 'right',
handler: refresh
}
]
}

TommyMaintz
30 Jun 2010, 1:00 PM
There is no align property available on buttons inside a Sencha Touch toolbar.

To align your button on the right you can put in a spacer in between the buttons. In your case:



items: [{
xtype: 'button',
ui: 'mask',
iconCls: 'refresh',
handler: refresh
}, {xtype: 'spacer'}, {
xtype: 'button',
ui: 'mask',
iconCls: 'add',
handler: refresh
}]


Just to explain the reason behind this. In Sencha Touch, toolbars are nothing more than containers with an hbox layout and custom styling. So in order to alter the alignment, the pack and other hbox layout configuration you could do:



layout: {
pack: 'justify',
align: 'center' // align center is the default
}


This also means that you can set the property "flex" on every item inside a toolbar to have them take up the remaining space.

Finally I just wanted to paste the code for the Spacer class to show you what it does.



Ext.Spacer = Ext.extend(Ext.Component, {
initComponent : function() {
if (!this.width) {
this.flex = 1;
}

Ext.Spacer.superclass.initComponent.call(this);
}
});


As you can see you can give a spacer a fixed width, and if you dont do that it will use a flex of one. This means that if you use the following order of items in your toolbar [button, spacer, button, spacer, button], you will get a button on the left, one in the center and one on the right with the spacers both taking up an equal amount of space.

I hope this answered your question.

profunctional
30 Jun 2010, 4:32 PM
Thanks for the detailed explanation. The spacer did the trick.

mbritton
7 Feb 2012, 8:53 AM
I had to explicitly set flex:1 to get the spacer to scale properly in Ext JS 4.



dockedItems:[{
xtype:'toolbar',
dock:'top',
items:[
{
xtype:'tbspacer',
flex:1
},
{
id:'toggleSearchButton',
enableToggle:true,
type:'button',
text:'Search',
pressed:false
}]
}],

jagadale11
12 Jun 2012, 3:16 AM
=D>
I had to explicitly set flex:1 to get the spacer to scale properly in Ext JS 4.



dockedItems:[{
xtype:'toolbar',
dock:'top',
items:[
{
xtype:'tbspacer',
flex:1
},
{
id:'toggleSearchButton',
enableToggle:true,
type:'button',
text:'Search',
pressed:false
}]
}],