无法使用自定义渲染器在网格中过滤或搜索产品
can't Filter or search product in grid with custom renderer
I have a problem with filter in my module in admin grid.
I also used 'order_item_id' field in my custom table and fetching product name based on '(sales/order_item)' using renderer.
I also using " 'filter_condition_callback' => array($this, '_productFilter') " but it cant' work
My problem is: can't Filter for columns with custom renderer not working.
When i search product name in column,it returns zero records.
What i wrong in My Code ?
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;
}
my renderer is
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();
}
}
因为你渲染了id,而且你是用产品名搜索的。所以你需要改变你的productfilter功能,并根据产品名称放入table中搜索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;
}
我对 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;
}
I have a problem with filter in my module in admin grid. I also used 'order_item_id' field in my custom table and fetching product name based on '(sales/order_item)' using renderer. I also using " 'filter_condition_callback' => array($this, '_productFilter') " but it cant' work My problem is: can't Filter for columns with custom renderer not working. When i search product name in column,it returns zero records. What i wrong in My Code ?
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;
}
my renderer is
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();
}
}
因为你渲染了id,而且你是用产品名搜索的。所以你需要改变你的productfilter功能,并根据产品名称放入table中搜索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;
}
我对 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;
}