如何为magento网格中的每个列添加列筛选器以用于自定义模块

时间:2012-09-26 13:28:43

标签: magento grid

我一直在为magento后端编写自定义模块,我想为每列添加过滤器

有人能指出正确的方向,处理此功能的代码在哪里?我将假设它是控制器的一部分

感谢您提供的任何帮助!

我准备了这样的专栏

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

2 个答案:

答案 0 :(得分:1)

首先通过这个术语“扩展Mage_Adminhtml_Block_Widget_Grid”搜索项目,你应该找到这个类Mage_Adminhtml_Block_Catalog_Category_Tab_Product

基本上你需要关注的是两种方法:

  • _prepareCollection()
  • _prepareColumns()

_prepareCollection 准备您的网格使用的集合,Magento在 {中添加的每列中应用index键表示的过滤器{1}} 方法。

示例

以下示例来自我上面粘贴的课程;)

_prepareColumns()

));

在你的收藏中,应该有一个名为$this->addColumn('E-Mail', array( 'header' => Mage::helper('CartAbandoned')->__('EMail'), 'align' => 'left', 'width' => '150px', 'index' => 'customer_email', 'type' => 'text', 'truncate' => 50, 'escape' => true, 的字段/列,并且你将customer_email设置为同名,Magento应该处理休息。

修改

index

此示例显示如何为3列准备可过滤的集合:sku,name和entity_id。

答案 1 :(得分:1)

有一个解决方案,我在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'),
));

正如您所看到的,我们又添加了一个值filter_condition_callback

我们唯一需要的是添加这个受保护的方法,它允许我们添加过滤:

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

您可以在此处找到完整文章:http://www.atwix.com/magento/grid-filter-for-columns/