如何在Magento中为列网格添加自定义过滤器?

时间:2012-10-03 21:14:04

标签: magento magento-1.7

我在Adminhtml中有自定义网格。

protected function _prepareCollection()
{
    /* @var $collection Mage_Catalog_Model_Resource_Product_Collection */
    $collection = Mage::getModel('catalog/product')->getCollection();
    $collection->addAttributeToSelect('*');
    $collection->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id=entity_id', null, 'left');
    $collection->groupByAttribute('entity_id');
    $collection->addStaticField('category_id');
    $collection->addExpressionAttributeToSelect('category_grp', 'GROUP_CONCAT(category_id)', 'category_id');

    $this->setCollection($collection);
    return parent::_prepareCollection();
}

protected function _prepareColumns()
{
    parent::_prepareColumns();

 $this->addColumn('category_id', array(
        'header'    => Mage::helper('newsletter')->__('Category'),
        'index'     => 'category_grp',
        'type'      => 'categories',
        'options'   => $options,
        'align'     => 'left',
//      'filter_index' => $this->_getFlatExpressionColumn('category'),
    return $this;
}

category_grp是带有int

的数组

我的问题是如何在标题栏中添加过滤器来过滤字段中的项目?

例如仅过滤category_id = 7的产品(category_grp为3,6,7,13)......

2 个答案:

答案 0 :(得分:0)

我认为您不需要任何自定义过滤器 只是尝试根据字段名称设置index

'index'     => 'category_id',

答案 1 :(得分:0)

看一下magento filter_condition_callback选项

$this->addColumn('categories', array(
                   ....                      
                   'filter_condition_callback' => array($this, '_applyMyFilter'),
                    ..
                 )
 );

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

     $this->getCollection()->addFieldToFilter('categories', array('finset' => $value));
 }