0

I want to display only those record that is selected or match with db record on load. Untill i reset filter when reset filter it should show all record.

This is my grid.php code. I tried with _addColumnFilterToCollection function but not working.

    <?php
class Shipping_Shippinggroup_Block_Adminhtml_Shippinggroup_Edit_Tab_Formcustomer extends  Mage_Adminhtml_Block_Widget_Grid
{
    public function __construct()
    {
        parent::__construct();
        $this->setId('customerGrid');
        $this->setUseAjax(true); // Using ajax grid is important
        $this->setDefaultSort('id');
        $this->setSaveParametersInSession(false);  //Dont save paramters in session or else it creates problems
        $this->setAdditionalJavaScript("
        // added click on selectbox support
        serializerController.prototype.rowClick = serializerController.prototype.rowClick.wrap(function(o, grid, event) {
            var tagName = Event.element(event).tagName
                isSelect = (tagName == 'SELECT' || tagName == 'OPTION');
            if (!isSelect) {
                o(grid, event);
            }
        });
        ");

    }



    protected function _getStore()
    {
        $storeId = (int) $this->getRequest()->getParam('store', 0);
        return Mage::app()->getStore($storeId);
    }

    protected function _prepareCollection()
{

    $store = $this->_getStore();
    $groupId = $this->getRequest()->getParam('id');

    $collection = Mage::getResourceModel('customer/customer_collection')
    ->addNameToSelect()
    ->addAttributeToSelect('entity_id')
    ->addAttributeToSelect('email')
    ->addAttributeToSelect('created_at')
    ->addAttributeToSelect('group_id')
    ->addFieldToFilter('group_id',array('in' => array(5)));
     $collection->getSelect()->joinLeft( array('shippinggroupcustomer'=> 'shippinggroupcustomer'), 'shippinggroupcustomer.customer_ids = e.entity_id and shippinggroupcustomer.shippinggroup_id='.$groupId,array('shippinggroup_id','customer_ids','shouldsendwarrantymail','shouldsendfactorymail','shouldsendordermail'));
    $this->setCollection($collection);

    return parent::_prepareCollection();
}



    protected function _prepareColumns()
    {
        $this->addColumn('customer_ids', array(
            'header_css_class'  => 'a-center',
            'type'              => 'checkbox',
            'name'              => 'customer_ids',
            'values'            => array_keys($this->_getSelectedCustomers()),
            'align'             => 'center',
            'index'             => 'entity_id'
        ));

         $this->addColumn('name', array(
            'header'    => Mage::helper('customer')->__('Name'),
            'index'     => 'name'
        ));
        $this->addColumn('email', array(
            'header'    => Mage::helper('customer')->__('Email'),
            'width'     => '150',
            'index'     => 'email'
        ));
        $this->addColumn('shouldsendwarrantymail', array(
            'name'              => 'shouldsendwarrantymail',
            'index'             => 'shouldsendwarrantymail',
            'header'    => Mage::helper('customer')->__('Should Send Warranty Mail'),
            'type'              => 'select',
            'options'   => array('0'=>'No','1'=>'Yes'),
            'filter'    => false,
            'sortable'  => false,


        ));

        $this->addColumn('shouldsendfactorymail', array(
            'name'              => 'shouldsendfactorymail',
            'index'             => 'shouldsendfactorymail',
            'header'    => Mage::helper('customer')->__('Should Send Factory Mail'),
            'type'              => 'select',
            'options'   => array('0'=>'No','1'=>'Yes'),
            'filter'    => false,
            'sortable'  => false


        ));
        $this->addColumn('shouldsendordermail', array(
            'name'              => 'shouldsendordermail',
            'index'             => 'shouldsendordermail',
            'header'    => Mage::helper('customer')->__('Should Send Order Mail'),
            'type'              => 'select',
            'options'   => array('0'=>'No','1'=>'Yes'),
            'filter'    => false,
            'sortable'  => false


        ));





        return parent::_prepareColumns();
    }

    public function getRowUrl($row) {
        return false;
    }

    protected function _getSelectedCustomers()   // Used in grid to return selected customers values.
    {
        $customers = $this->getSelectedCustomers();
        return $customers;
    }

    public function getSelectedCustomers()
    {
        // Customer Data

        $tm_id = $this->getRequest()->getParam('id');

        if(!isset($tm_id)) {
            $tm_id = 0;
        }

            $custIds = array();
            $customerProductIds = Mage::getModel('shippinggroup/shippinggroupcustomer')->getCollection()->addFieldToFilter('shippinggroup_id', array(
                     array('finset' => $tm_id)));
                    // print_r($customerProductIds->getData());
            foreach($customerProductIds as $id){
                $custIds[$id->getCustomerIds()] = array(
                                                        'shouldsendwarrantymail' => $id->getShouldsendwarrantymail(),
                                                        'shouldsendfactorymail' => $id->getShouldsendfactorymail(),
                                                        'shouldsendordermail' => $id->getshouldsendordermail(),
                                                        );
            }

        //var_dump($custIds);exit;
        return $custIds;
    }

    /**
     * Rerieve grid URL
     *
     * @return string
     */
    public function getGridUrl()
    {
        return $this->getUrl('*/*/customergrid', array('_current'=>true));
    }

}
-1

Ok got the solution myself.

Grid file changes

public function __construct()
{
    parent::__construct();
    $this->setId('customerGrid');
    $this->setUseAjax(true); // Using ajax grid is important
    $this->setDefaultSort('id');
    $this->setDefaultFilter(array('customer_ids'=>1)); /* added this line to call column filter functino */
    $this->setSaveParametersInSession(false);  //Dont save paramters in session or else it creates problems
    $this->setAdditionalJavaScript("
    // added click on selectbox support
    serializerController.prototype.rowClick = serializerController.prototype.rowClick.wrap(function(o, grid, event) {
        var tagName = Event.element(event).tagName
            isSelect = (tagName == 'SELECT' || tagName == 'OPTION');
        if (!isSelect) {
            o(grid, event);
        }
    });
    ");

}


 protected function _addColumnFilterToCollection($column)
{
    // Set custom filter for in product flag
    if ($column->getId() == 'customer_ids') {
        $customersIds = array_keys($this->_getSelectedCustomers()); /* have multidimentional array so just need to pass customer_ids array */
        if (empty($customersIds)) {
            $customersIds = 0;
        }
        if ($column->getFilter()->getValue()) {
            $this->getCollection()->addFieldToFilter('entity_id', array('in'=>$customersIds));
        } else {
            if($customersIds) {
                $this->getCollection()->addFieldToFilter('entity_id', array('nin'=>$customersIds));
            }
        }
    } else {
        parent::_addColumnFilterToCollection($column);
    }
    return $this;

}
  • very unclear what you did – OZZIE Jun 27 at 11:23
  • Just added $this->setDefaultFilter(array('customer_ids'=>1)); and it will call _addColumnFilterToCollection function to make selected – MageLerner Jun 27 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.