【问题标题】:can't Filter or search product in grid with custom renderer无法使用自定义渲染器过滤或搜索网格中的产品
【发布时间】:2015-03-08 21:41:43
【问题描述】:

我的管理网格模块中的过滤器有问题。 我还在自定义表中使用了“order_item_id”字段,并使用渲染器根据“(sales/order_item)”获取产品名称。 我也使用“ 'filter_condition_callback' => array($this, '_productFilter') ”但它不能工作 我的问题是:无法过滤自定义渲染器不起作用的列。 当我在列中搜索产品名称时,它返回零记录。 我的代码有什么问题?

public function _prepareColumns()
{
   $this->addColumn('entity_id', array(
      'header'    => Mage::helper('adminhtml')->__('ID'),
      'align'     =>'right',
      'width'     => '50px',
      'index'     => 'entity_id',
    ));         

    $this->addColumn('order_item_id', array(
      'header'    => Mage::helper('adminhtml')->__('Product Name'),
      'align'     =>'right',
      'index'     => 'order_item_id',
      'renderer' => 'Test_Module1_Block_Adminhtml_Renderer_Product',
      'filter_condition_callback' => array($this, '_productFilter'),
    ));
    return parent::_prepareColumns();
}
protected function _productFilter($collection, $column)
{
    if (!$value = $column->getFilter()->getValue()) {
        return $this;
    }

    $this->getCollection()->getSelect()->where(
        "order_item_id like ?
        "
    , "%$value%");


    return $this;
}

我的渲染器是

 class Test_Module1_Block_Adminhtml_Renderer_Product extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    public function render(Varien_Object $row)
    {
        $order = Mage::getModel('sales/order_item')->load($row->getData('order_item_id'));

        return $order->getName();
    }
}

【问题讨论】:

    标签: magento grid admin renderer


    【解决方案1】:

    这是因为您已呈现 id 并且您正在使用产品名称进行搜索。因此,您需要更改您的 productfilter 函数并根据产品名称在表中放置用于搜索 id 的代码。 你需要改变你的功能:

    protected function _productFilter($collection, $column)
    {
      if (!$value = $column->getFilter()->getValue()) {
        return $this;
    }
     $orderitem = Mage::getModel('sales/order_item')->getCollection();
     $orderitem->addFieldToFilter('name',array('like'=>'%'.$value.'%'));  
       $ids =array();
      foreach($orderitem as $item){
          $ids[] = $item->getId();
     }
    
    $this->getCollection()->addFieldToFilter("id",array("in",$ids));
    return $this;
    }
    

    【讨论】:

      【解决方案2】:

      我对 saumik 代码及其对我的工作进行了一些更改。

      protected function _productFilter($collection, $column)
          {
              if (!$value = $column->getFilter()->getValue()) {
                  return $this;
              }
              $orderitem = Mage::getModel('sales/order_item')->getCollection();
              $orderitem->addFieldToFilter('name',array('like'=>'%'.$value.'%'));  
              $ids =array();
              foreach($orderitem as $item){
                $ids[] = $item->getOrderId(); //  sales_flat_order_item.order_id = sales_flat_order.entity_id
              }
              $this->getCollection()->addFieldToFilter("entity_id",array("in",$ids));
              return $this;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-15
        • 2012-03-02
        • 2017-11-13
        • 2019-06-06
        • 2020-08-29
        相关资源
        最近更新 更多