0

我一直在为 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
    ));
4

2 回答 2

1

首先通过“extends Mage_Adminhtml_Block_Widget_Grid”这个词搜索项目,你应该找到例如这个类Mage_Adminhtml_Block_Catalog_Category_Tab_Product

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

  • _prepareCollection()
  • _prepareColumns()

_prepareCollection准备网格使用的集合,Magento 在其上应用过滤器,这些过滤器由您在方法index中添加的每一列中的键表示。_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并且您已经index设置为相同的名称,Magento 应该处理休息。

编辑

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

这个例子展示了如何为 3 列准备可过滤的集合:sku、name 和 entity_id。

于 2012-09-26T19:54:39.663 回答
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/

于 2015-07-07T11:46:29.397 回答