2012-09-26 61 views
0

我已经写了一个自定义模块,Magento的后台,我想添加过滤器为每一列如何添加列过滤器为每列在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 
    )); 
+1

请澄清你的问题。你问在哪里把这段代码?如果不是,请更具体地说明您想要找到的内容。 – shaune

+0

你正在学习一个教程来指导你吗?如果不是,为什么不呢? – clockworkgeek

回答

1

首先搜索项目的“延伸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,名称和ENTITY_ID。

+0

请给我关于如何使用过滤器编写_prepareCollection()的想法 – chaitu

+0

它给我错误,调用未定义的函数addAttributeToSelect() – chaitu

+0

请编辑你的帖子并添加代码示例。 – xyz

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/

+0

你能帮我解答吗? http://stackoverflow.com/questions/31269519/grid-column-filter-not-working-with-multiple-values-in-magento-custom-module – Sathish

相关问题