【问题标题】:How add custom filter to colum grid in Magento?如何在 Magento 的列网格中添加自定义过滤器?
【发布时间】:2012-10-03 21:14:03
【问题描述】:

我在 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)...

【问题讨论】:

  • 其实我有兴趣学习如何创建一个自定义过滤器(如果可能的话)如果你有任何发现请分享信息

标签: magento magento-1.7


【解决方案1】:

我认为您不需要任何自定义过滤器。
只需尝试将index设置为与字段名称一致即可:

'index'     => 'category_id',

【讨论】:

  • nnnn,如果我使用 'index' => 'category_id' 那么我只有 1 个类别。产品属于多个类别...例如 3,7,13...
【解决方案2】:

看看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));
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-22
    • 1970-01-01
    • 1970-01-01
    • 2015-09-10
    • 1970-01-01
    • 2015-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多