chaitu
chaitu

Reputation: 599

How to add column filter for each column in magento grid for custom module

I've been writing a custom module for the magento backend,i want to add filter for each column

Can anyone point me in the right direction as to where the code is that handles this function? I'm going to assume its part of the controller

Thanks for any help you can provide!

i have prepared columns like this

     public function __construct()
{
    parent::__construct();
    $this->setId('main_grid');
$this->setDefaultSort('entity_id');
    $this->setDefaultDir('DESC');
    $this->setSaveParametersInSession(true);
    $this->setUseAjax(true);

}

protected function _prepareCollection()
{

    $model = Mage::getModel('CartAbandoned/tip');
            $collection = $model->getCollection();
        $this->setCollection($collection);
            return parent::_prepareCollection();
}


    $this->addColumn('id', array(
        'header'        => Mage::helper('CartAbandoned')->__('Id'),
        'align'         => 'right',
        'width'         => '50px',
        'type'          => 'number',
        'index'         => 'entity_id',
    ));

    $this->addColumn('E-Mail', array(
        'header'        => Mage::helper('CartAbandoned')->__('EMail'),
        'align'         => 'left',
        'width'         => '150px',
        'index'         => 'customer_email',
        'type'          => 'text',
        'truncate'      => 50,
        'escape'        => true,
   ));

    $this->addColumn('firstName', array(
        'header'        => Mage::helper('CartAbandoned')->__('firstName'),
        'align'         => 'left',
        'index'         => 'customer_firstname',
        'type'          => 'text',
        'escape'        => true,
   ));

    $this->addColumn('lastName', array(
        'header'        => Mage::helper('CartAbandoned')->__('lastName'),
        'align'         => 'left',
        'index'         => 'customer_lastname',
        'type'          => 'text',
        'escape'        => true,
      ));

$this->addColumn('total', array(
        'header'        => Mage::helper('CartAbandoned')->__('Total'),
        'align'         => 'left',
        'index'         => 'base_grand_total',
        'type'          => 'price',
        'escape'        => true,
 ));

$this->addColumn('quan', array(
        'header'        => Mage::helper('CartAbandoned')->__('Quantity'),
        'align'         => 'left',
        'index'         => 'items_qty',
        'type'          => 'number',
        'escape'        => true,
 ));

    $this->addColumn('cartcreatedtime', array(
        'header'        => Mage::helper('CartAbandoned')->__('cartcreatedtime'),
        'align'         => 'left',
        'index'         => 'created_at',
        'type'          => 'datetime',
        'escape'        => true,
 ));

   $this->addColumn('cartabandonedtime', array(
        'header'        => Mage::helper('CartAbandoned')->__('cartabandonedtime'),
        'align'         => 'left',
        'index'         => 'updated_at',
        'type'          => 'datetime',
        'escape'        => true,
));


     $this->addColumn('action',array(
            'header'    => Mage::helper('CartAbandoned')->__('Action'),
            'type'      => 'action',
            'getter'    => 'getId',
            'actions'   => array(
                array(
                    'caption' => Mage::helper('CartAbandoned')->__('View Products'),
                    'url'     => array('base'=>'*/*/edit'),
                    'field'   => 'id'
               )
           ),
           'filter'    => false,
           'sortable'  => false
    ));

Upvotes: 0

Views: 9324

Answers (2)

zhartaunik
zhartaunik

Reputation: 950

There is one solution, which I found on atwix.com

$this->addColumn('address', array(
            'header'=> Mage::helper('sales')->__('Address'),
            'type'  => 'text',
            'renderer' => 'Atwix_Ordersgrid_Block_Adminhtml_Sales_Order_Renderer_Address',
            'filter_condition_callback' => array($this, '_addressFilter'),
));

as you can see we have added one more value filter_condition_callback

and the only thing we need is to add this protected method, which allow us add filtration:

protected function _addressFilter($collection, $column)
{
    if (!$value = $column->getFilter()->getValue()) {
        return $this;
    }

    $this->getCollection()->getSelect()->where(
        "sales_flat_order_address.city like ?
        OR sales_flat_order_address.street like ?
        OR sales_flat_order_address.postcode like ?"
    , "%$value%");


    return $this;
}

Full article you can find here: http://www.atwix.com/magento/grid-filter-for-columns/

Upvotes: 1

xyz
xyz

Reputation: 2282

First of all search project by this term "extends Mage_Adminhtml_Block_Widget_Grid", you should find for example this class Mage_Adminhtml_Block_Catalog_Category_Tab_Product.

Basically what you need to focus on are two methods:

  • _prepareCollection()
  • _prepareColumns()

_prepareCollection prepares collection which is used by your grid and on which Magento applies filters which are represented by index key in each column that you add in _prepareColumns() method.

Example

Below example comes from class that i've pasted above ;)

$this->addColumn('E-Mail', array(
    'header'        => Mage::helper('CartAbandoned')->__('EMail'),
    'align'         => 'left',
    'width'         => '150px',
    'index'         => 'customer_email',
    'type'          => 'text',
    'truncate'      => 50,
    'escape'        => true,

));

In your collection there should be field/column which is called customer_email and as you have index set to same name Magento should handle rest.

EDIT

protected function _prepareCollection()
{
    $collection = Mage::getModel('catalog/product')->getCollection()
        ->addAttributeToSelect('name')
        ->addAttributeToSelect('sku');

    $this->setCollection($collection);

    return parent::_prepareCollection();
}

protected function _prepareColumns()
{
    $this->addColumn('entity_id', array(
        'header'    => Mage::helper('catalog')->__('ID'),
        'sortable'  => true,
        'width'     => '60',
        'index'     => 'entity_id'
    ));
    $this->addColumn('name', array(
        'header'    => Mage::helper('catalog')->__('Name'),
        'index'     => 'name'
    ));
    $this->addColumn('sku', array(
        'header'    => Mage::helper('catalog')->__('SKU'),
        'width'     => '80',
        'index'     => 'sku'
    ));

    return parent::_prepareColumns();
}

This example show how to prepare filterable collection for 3 columns: sku, name and entity_id.

Upvotes: 1

Related Questions