过滤magento grid

时间:2015-06-30 06:49:54

标签: magento filter grid render complex-data-types

您好我在货运网格中添加了一个新列,如下所示

    $this->addColumn('telephone', array(
    'header' => Mage::helper('sales')->__('Billing Phone'),
    'index' => 'telephone',

    'renderer'=> new OSP_Adminhtml_Block_Customer_Renderer_BillingPhone()
    ));

在此我使用渲染器显示自定义值,如下所示

public function render(Varien_Object $row) {
        $customer_id = $row->getData('customer_id');

    if( $customer_id > 0 ) {
        // get member_id (club canon)
        $customer = Mage::getModel('customer/customer')->load($customer_id);
        if( is_object( $customer ) ) {
            $value = $customer->getData('mobile');
        }
    }else{
    $id = $row->getData('order_increment_id');
    $order = Mage::getModel('sales/order')->loadByIncrementId($id);
    $value = $order->getBillingAddress()->getTelephone();
}
    return $value;
}
  

工作正常,并在此基础上正确显示数据   渲染器中的条件。

     

但问题是现在我需要过滤数据,而不是   工作,因为它只在一列中查找电话或移动数据   我读过有关filter_condition_callback但无法完成工作的内容。你能告诉我怎样才能做到这一点。

提前致谢

1 个答案:

答案 0 :(得分:0)

1.将行filter_condition_callback添加到列中,如下所示:

 $this->addColumn('telephone', array(
    'header' => Mage::helper('sales')->__('Billing Phone'),
    'index' => 'telephone',
    'filter_condition_callback' => array($this, '_someCallBackFunction'),
    'renderer'=> new OSP_Adminhtml_Block_Customer_Renderer_BillingPhone()
    ));

在您的专栏中添加此行后,Magento将调用此回调函数,在468行中查看app/code/core/Mage/Adminhtml/Block/Widget/Grid.php

2.在您的块网格文件中创建_someCallBackFunction函数,该函数将采用两个参数$collection - 未加载集合对象,以及$column Mage_Adminhtml_Block_Widget_Grid_Column

protected function _someCallBackFunction($collection, $column)
{
    $value = $column->getFilter()->getValue();
    if ($value === null) { //here check if filter is not null
        return $this;
    }

    /**
     * Here you can add filter to collection
     * or do other manipulations with collection.
     * As example you can check filter value and filter collection.
     */
    if ($value != 0) { 
        $collection->addFieldToFilter('telephone', array('neq' => 0));
    }

    return $this;
}