2012-09-26 136 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

相關問題