Magento – Magento Custom Dropdown Column in Manage Product Grid Disappears the Colums when i apply filter

gridgrid-serlizationproduct

I wanted to add custom column called brands(dropdown) in magento product grid in backend.I achieved this task but the issue is when i start filter the brands the column brands disappers and when i refresh the page then comes back.can any one please help me.
Here is my code.

in _prepareCollection() function i added

$collection = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToSelect('sku')
            ->addAttributeToSelect('name')
            ->addAttributeToSelect('attribute_set_id')
            ->addAttributeToSelect('type_id')
        ->addAttributeToSelect('brands'); // added by me

in _prepareColumns()

$brands = Mage::getModel('eav/entity_attribute_option')->getCollection()->setStoreFilter()->join('attribute','attribute.attribute_id=main_table.attribute_id','attribute_code');
        foreach($brands as $brand):
            if($brand->getAttributeCode() == 'brands')
                $brand_options[$brand->getOptionId()] = $brand->getValue();
            endforeach;

        $this->addColumn('brands',
            array(
            'header'=> Mage::helper('catalog')->__('Brands'),
            'width' => '10px',
            'index' => 'brands',
            'type'  => 'options',
            'sortable' => false,
            'options' => $brand_options,
        ));

Best Answer

First add the add new function on grid.php getAttributeOptions

  protected function _getAttributeOptions($attribute_code)
        {
            $attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', $attribute_code);
            $options = array();
            foreach( $attribute->getSource()->getAllOptions(false, true) as $option ) {
                $options[$option['value']] = $option['label'];
            }
            return $options;
        }

then add modify

$this->addColumn('brands', array(
            'header' => Mage::helper('catalog')->__('Brand'),
            'align' => 'left',
            'width' => '80px',
            'index' => 'brands',

'final_index' =>'brands',

            'type' => 'options',
            'options' => $this->_getAttributeOptions('brands'),
        ));