Magento Grid Column Position

I am editing the order grid by adding custom columns like

$this->addColumn('pagamenti', array( 'header' => 'Paymentsource', 'width'=>'50px', 'align' =>'left', 'type' => 'text', 'renderer' => 'Blablabla_Adminhtml_Block_Sales_Order_Renderer_lol' )); 

but each column is located in the far right corner of the table, no matter where I call addColumns.

Is there a way to force a position?

thanks

+7
source share
4 answers

Perhaps you can 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'); 
+27
source

If you have ever considered the _prepareColumns method that you override, you will see that it calls sortColumnsByOrder . Therefore, if you add a column after this point (possibly with an event), you need to do something like:

 $this->addColumnsOrder('pagamenti', 'real_order_id') ->sortColumnsByOrder(); 
+10
source

If you called parent :: _ prepareColumns (); until $ this-> addColumn () or $ this-> addColumnAfter () then your new column is always displayed at the last position. Thus, you can adjust the location of the operator parent :: _ prepareColumns (); according to your requirement.

 $this->addColumnAfter('barcode', array( 'header' => Mage::helper('sales')->__('Barcode'), 'align' => 'left', 'index' => 'barcode', 'width' => '200px', ),'real_order_id'); parent::_prepareColumns(); 

Here, the barcode column will be shown immediately after the Order Id # column. If you put parent :: _ prepareColumns (); until $ this-> addColumnAfter , then it will not be displayed immediately after the column Order ID #. It will only be displayed last.

+3
source

As the other answers pointed out, you can use the Mage_Adminhtml_Block_Widget_Grid::addColumnAfter to accomplish this.

However, what (most) does not mention other answers is that you must explicitly call Mage_Adminhtml_Block_Widget_Grid::sortColumnsByOrder so that the columns are reordered .

The Mage_Adminhtml_Block_Widget_Grid::sortColumnsByOrder method is called exclusively by the Mage_Adminhtml_Block_Widget_Grid::_prepareColumns 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 on your new order. 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; } 
+1
source

All Articles