Question

I have a multi select attribute in eav_attribute with attribute_code my_managers, its entity_type_code is customer_address, I want to get selected value,,I am trying to export it to csv,in my protected function _prepareColumns() {.....} I am doing this. If single value is selected, it is working fine, but if multiple value are selected, then it shows an empty column.Multi select case is not working properly, other two cases are fine.I am doing this.

class Abc_AddressManager_Block_Adminhtml_Stores_Csv extends Mage_Adminhtml_Block_Widget_Grid {

public function __construct() {
    parent::__construct();
    $this->setIsExport(true);
    $this->setId('customer_address_grid');
    $this->setUseAjax(false);
    $this->setDefaultSort('store_id');
    $this->setSaveParametersInSession(true);
}


 protected function _prepareCollection() {
        $customer_id = $this->getRequest()->getParam('id');
    $collection = Mage::getResourceModel('customer/address_collection');
    $collection->addAttributeToSelect('*');
    $select = $collection->getSelect();

    $select->joinLeft(
            array('customer' => $collection->getTable('customer/entity')), 'customer.entity_id=e.parent_id', array('email' => 'email')
    );

    $this->setCollection($collection);

    return parent::_prepareCollection();
}

protected function _prepareColumns() {

    $addressModel = Mage::getModel('customer/address');
    $addressForm = Mage::getModel('customer/form');
    $addressForm->setFormCode('adminhtml_customer_address')
            ->setEntity($addressModel)
            ->initDefaultValues();
    $attributes = $addressForm->getAttributes();

    $this->addColumn('email', array(
        'header' => 'email',
        'type' => 'text',
        'index' => 'email',
        'filter' => false,
        'sortable' => false,
    ));

    $this->addColumn('entity_id', array(
        'header' => 'address_id',
        'type' => 'text',
        'index' => 'entity_id',
        'filter' => false,
        'sortable' => false,
    ));

    foreach ($attributes as $attribute) {
        $code = $attribute->getData('attribute_code');
        if ($code == "additional_managers") {
            $x = 0;
        }
        switch ($attribute->getFrontendInput()) {
            case 'select' :
                $selectValues = array();
                $options = $attribute->getSource()->getAllOptions(false);
                foreach ($options as $option) {
                    $selectValues[$option['value']] = $option['label'];
                }
                $this->addColumn($code, array(
                    'header' => $code,
                    'type' => 'options',
                    'index' => $code,
                    'filter' => false,
                    'sortable' => false,
                    'options' => $selectValues
                ));

                break;
            case 'multiselect' :

                $selectValues = array();
                $options = $attribute->getSource()->getAllOptions(false);
                foreach ($options as $option) {
                    $selectValues[$option['value']] = $option['label'];
                }

                $this->addColumn($attribute->getAttributeCode(), array(
                    'header' => $attribute->getFrontendLabel(),
                    'type' => 'options',
                    'index' => $code,
                    'options' => $selectValues,
                    'renderer' => 'Abc_AddressManager_Block_Adminhtml_Stores_Render_Store',
                ));



                break;

            default :
                $this->addColumn($code, array(
                    'header' => $code,
                    'type' => 'text',
                    'index' => $code,
                    'filter' => false,
                    'sortable' => false
                ));
                break;
        }
    } // end

    return parent::_prepareColumns();
}

}

And my render is

 class Abc_sgAddressManager_Block_Adminhtml_Stores_Render_Store extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract{
    public function render(Varien_Object $row){

        $id = $row->getEntityId();//Here row is csutomer address entity objetct,
        //Code will go here
    }
}

and one thing more, in db,comma separated values (values of selected options) is saved as shown in pic. enter image description here There one more catch, if I remove case of "multiselect",just values of are shown for multi selected in csv.If selection is multiple, multiple values are show, With current code, it skips multiple selected options

Was it helpful?

Solution

$this->addColumn($code, array(
                'header' => $attribute->getFrontendLabel(),
                'type' => 'options',
                'index' => $code,
                'options' => $selectValues,
                'renderer'  => 'Namespace_Modulename_Block_Adminhtml_Code',
            ));

render block

class Namespace_Modulename_Block_Adminhtml_Code extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{

    public function render(Varien_Object $row)
    {


         $status=$row->getCode();
         //you code here 


         return $value;
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top