【问题标题】:How to create filter_condition_callback on custom renderer in Magento admin grid?如何在 Magento 管理网格中的自定义渲染器上创建 filter_condition_callback?
【发布时间】:2023-03-14 11:14:01
【问题描述】:

我已将自定义列添加到客户网格:

    $this->addColumn('shipping_name', array(
        'header'    => Mage::helper('customer')->__('Shipping name'),
        'index'     => 'entity_id',
        'renderer'  => new My_Unique_Block_Customer_Renderer_Shippingname(),
        'filter_condition_callback' => array($this, '_shippingNameFilter')
    ));

渲染器 Shippingname.php 看起来像:

class My_Unique_Block_Customer_Renderer_Shippingname extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract {

public function render(Varien_Object $row)
{

    $id = $row->getData($this->getColumn()->getIndex());

    $customer = Mage::getModel('customer/customer')->load($id); //customer id

    $data = "";

    if ( $customer->getDefaultShippingAddress() != null ) {
        $shipping_address = $customer->getDefaultShippingAddress();

        if ( $shipping_address->getFirstname() != null ) {
            $data .= $shipping_address->getFirstname();
        }

        if ( $shipping_address->getLastname() != null ) {

            if ( $shipping_address->getFirstname() != null ) {
                $data .= " ";
            }

            $data .= $shipping_address->getLastname();
        }

    }

    return $data;
}}

我应该用什么来代替函数 _shippingNameFilter() 而不是“shipping_name like ”?让此列中的过滤器起作用?

    protected function _shippingNameFilter($collection, $column)
{
    if (!$value = $column->getFilter()->getValue()) {
        return $this;
    }
    $this->getCollection()->getSelect()->where("shipping_name like ?", "%$value%");

    return $this;
}

谢谢!

【问题讨论】:

    标签: php magento filter callback renderer


    【解决方案1】:

    如果我理解您的问题是正确的,那么对于自定义过滤器,您需要按照以下步骤操作。

    这是您的客户网格:

    $this->addColumn('shipping_name', array(
        'header'    => Mage::helper('customer')->__('Shipping name'),
        'index'     => 'entity_id',
        'renderer'  => new My_Unique_Block_Customer_Renderer_Shippingname(),
        'filter_condition_callback' => array($this, '_customShippingFilterCallBack')
    ));
    

    并添加这样的方法,

    protected function _customShippingFilterCallBack($collection, $column)
    {
       //Put your logic here..!!
    if (!$value = $column->getFilter()->getValue()) 
    {
        return $this;
    }
    $this->getCollection()->getSelect()->where("shipping_name like ?", "%$value%");
    
    return $this;
    }
    

    注意:这是创建自定义过滤器的基本结构。您需要根据需要进行一些更改。

    【讨论】:

    • 是的,结构和我的一样,但我不知道在过滤器回调中使用什么值,因为列是从 2 个值合并(concat)的:运送名字 + 运送姓氏。我需要一个具体的例子:-(
    【解决方案2】:

    是的。 @Vishwas Soni 是对的。此外,如果想将 eav_attribute_option_value 表加入过滤选项值,可以使用以下代码。

    $this->addColumn(
            'product_manufacturer',
            [
                'header' => __('Manufacturer'),
                'index' => 'manufacturer',
                'filter_condition_callback' => [$this, 'filterCustomManufacturer'],
                'header_css_class' => 'col-manufacturer',
                'column_css_class' => 'col-manufacturer'
            ]
        );
    

    下面添加过滤条件回调函数,包括join like,

    protected function filterCustomManufacturer($collection, $column)
    {
        if (!$value = $column->getFilter()->getValue()) {
            return $this;
        }
    
        $collection->getSelect()->joinLeft(array('c_p_e_i'=>'catalog_product_entity_int'),
        'e.row_id = c_p_e_i.row_id',array('value'));
    
        $collection->getSelect()->joinLeft(array('attribute_option'=>'eav_attribute_option_value'),
        'c_p_e_i.value = attribute_option.option_id and attribute_option.store_id = 0',
         array('eaov_val'=>'value' , 'eaov_option'=>'option_id'));
    
         $collection->getSelect()->where("`attribute_option`.`value` like ?", "%$value%");
    
        return $this;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多