I'm editing the order grid by adding custom columns like this
$this->addColumn('pagamenti', array(
'header' => 'Paymentsource',
'width'=>'50px',
'align' =>'left',
'type' => 'text',
'renderer' => 'Blablabla_Adminhtml_Block_Sales_Order_Renderer_lol'
));
but every column is positioned on the far right of the table, no matter where I call addColumns.
Is there a way to force the position?
Thanks
You can probably use the addColumnAfter
function.
$this->addColumnAfter('pagamenti', array(
'header' => 'Paymentsource',
'width'=>'50px',
'align' =>'left',
'type' => 'text',
'renderer' => 'Blablabla_Adminhtml_Block_Sales_Order_Renderer_lol'
), 'id_of_column_to_be_after');
If you've ever looked at the _prepareColumns
method that you're overriding you'll see that it calls sortColumnsByOrder
. So if you're adding a column after this point (perhaps with an event) you'll need to do something like:
$this->addColumnsOrder('pagamenti', 'real_order_id')
->sortColumnsByOrder();
If you called parent::_prepareColumns(); before $this->addColumn() or $this->addColumnAfter() then your new column always been display at last position. So you may adjust the place of parent::_prepareColumns(); statement as per your requirement.
$this->addColumnAfter('barcode', array(
'header' => Mage::helper('sales')->__('Barcode'),
'align' => 'left',
'index' => 'barcode',
'width' => '200px',
),'real_order_id');
parent::_prepareColumns();
Here, Barcode column will be shown just after Order Id # column. If you put parent::_prepareColumns(); before $this->addColumnAfter then it will not display just after Order ID # column. It will display at last only.
As the other answers have pointed out, you can utilize the Mage_Adminhtml_Block_Widget_Grid::addColumnAfter
method to accomplish this.
However what (most of) the other answers fail to mention is that you must explicitly call Mage_Adminhtml_Block_Widget_Grid::sortColumnsByOrder
in order for the columns to be reordered.
The Mage_Adminhtml_Block_Widget_Grid::sortColumnsByOrder
method is called exclusively by the Mage_Adminhtml_Block_Widget_Grid::_prepareColumns
method as follows:
/// app/code/core/Mage/Adminhtml/Block/Widget/Grid.php line 557
protected function _prepareColumns()
{
$this->sortColumnsByOrder();
return $this;
}
This means that your derived class will have to do the same in order to re-sort the columns based upon your new ordering. For example:
protected function _prepareColumns() {
/// Let the parent add some columns
/// Mage_Eav_Block_Adminhtml_Attribute_Grid_Abstract does add some
parent::_prepareColumns();
/// Add some new columns
$this->addColumnAfter('sort_order', array(
'header' => $this->__('Sort Order'),
'sortable' => true,
'index' => 'sort_order'
), 'frontend_label');
$this->addColumnAfter('attribute_group_name', array(
'header' => $this->__('Attribute Group'),
'sortable' => true,
'index' => 'attribute_group_name'), 'sort_order');
/// ! IMPORTANT ! Re-sort the columns with the new additions
$this->sortColumnsByOrder();
return $this;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With