Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento grid column position

Tags:

php

magento

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

like image 791
15 revs, 2 users 97% Avatar asked Jan 20 '12 12:01

15 revs, 2 users 97%


4 Answers

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');
like image 150
Peter O'Callaghan Avatar answered Nov 06 '22 11:11

Peter O'Callaghan


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();
like image 41
clockworkgeek Avatar answered Nov 06 '22 12:11

clockworkgeek


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.

like image 4
Praful Rajput Avatar answered Nov 06 '22 12:11

Praful Rajput


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;
}
like image 2
Luke A. Leber Avatar answered Nov 06 '22 12:11

Luke A. Leber