网格列过滤器不适用于magento自定义模块中的两个模型
我正在使用magento后端的自定义模块,在使用filter calback时,此筛选器不起作用! 有人能推荐我吗? 谢谢 我试过这样的代码 Grid.php网格列过滤器不适用于magento自定义模块中的两个模型,magento,filter,grid,adminhtml,Magento,Filter,Grid,Adminhtml,我正在使用magento后端的自定义模块,在使用filter calback时,此筛选器不起作用! 有人能推荐我吗? 谢谢 我试过这样的代码 Grid.php protected function _prepareCollection() { $collection = Mage::getModel('listings/listings')->getCollection(); $this->setCollection($collection);
protected function _prepareCollection()
{
$collection = Mage::getModel('listings/listings')->getCollection();
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
$this->addColumn("item_id", array(
"header" => Mage::helper("linkmanagement")->__("ID"),
"align" => "center",
"type" => "number",
"index" => "item_id",
));
$this->addColumn("title", array(
"header" => Mage::helper("linkmanagement")->__("Title"),
"index" => "title"
));
$this->addColumn("cat_ids", array(
"header" => Mage::helper("linkmanagement")->__("Cat ID"),
"align" => "center",
"index" => "cat_ids",
"renderer" => 'Sathish_Linkmanagement_Block_Adminhtml_Linkmanagement_Renderer_Categories',
'filter_condition_callback' => array($this, '_categoriesFilter')
));
$this->addColumn("url_key", array(
"header" => Mage::helper("linkmanagement")->__("URL"),
"index" => "url_key",
"width" => "200px",
));
$this->addColumn('status',
array(
'header' => Mage::helper('linkmanagement')->__('Status'),
'index' => 'status',
'type' => 'options',
'options' => array('1' => Mage::helper('linkmanagement')->__('Active'),
'0' => Mage::helper('linkmanagement')->__('Inactive')),
)
);
return parent::_prepareColumns();
}
class Sathish_Linkmanagement_Block_Adminhtml_Linkmanagement_Renderer_Categories extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
public function render(Varien_Object $row)
{
$value = $row->getData($this->getColumn()->getIndex());
// $value = 38,92
$value = explode(',',$value);
$collection = Mage::getModel("categories/categories")->getCollection()->addFieldToFilter('category_id',$value)->getData();
foreach($collection as $col){
$result[] = $col['title'];
}
$result = implode(',', $result);// $result = schools,colleges
return $result;
}
}
回调函数
protected function _categoriesFilter($collection, $column)
{
if (!$value = $column->getFilter()->getValue()) {
return $this;
}
$this->getCollection()->getSelect()->where(
"cat_ids ?"
, "%$value%");
return $this;
}
Categories.php
protected function _prepareCollection()
{
$collection = Mage::getModel('listings/listings')->getCollection();
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
$this->addColumn("item_id", array(
"header" => Mage::helper("linkmanagement")->__("ID"),
"align" => "center",
"type" => "number",
"index" => "item_id",
));
$this->addColumn("title", array(
"header" => Mage::helper("linkmanagement")->__("Title"),
"index" => "title"
));
$this->addColumn("cat_ids", array(
"header" => Mage::helper("linkmanagement")->__("Cat ID"),
"align" => "center",
"index" => "cat_ids",
"renderer" => 'Sathish_Linkmanagement_Block_Adminhtml_Linkmanagement_Renderer_Categories',
'filter_condition_callback' => array($this, '_categoriesFilter')
));
$this->addColumn("url_key", array(
"header" => Mage::helper("linkmanagement")->__("URL"),
"index" => "url_key",
"width" => "200px",
));
$this->addColumn('status',
array(
'header' => Mage::helper('linkmanagement')->__('Status'),
'index' => 'status',
'type' => 'options',
'options' => array('1' => Mage::helper('linkmanagement')->__('Active'),
'0' => Mage::helper('linkmanagement')->__('Inactive')),
)
);
return parent::_prepareColumns();
}
class Sathish_Linkmanagement_Block_Adminhtml_Linkmanagement_Renderer_Categories extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
public function render(Varien_Object $row)
{
$value = $row->getData($this->getColumn()->getIndex());
// $value = 38,92
$value = explode(',',$value);
$collection = Mage::getModel("categories/categories")->getCollection()->addFieldToFilter('category_id',$value)->getData();
foreach($collection as $col){
$result[] = $col['title'];
}
$result = implode(',', $result);// $result = schools,colleges
return $result;
}
}
注意:
工作正常。,
只有筛选器回调函数不工作 我用自己的渲染器检查了您的示例,并尝试检查您的回调 在回调方法中替换
$this->getCollection()->getSelect()
与
以下是我的建议:
Magento筛选器假定不是100%相似。我的意思是,如果你想应用过滤器,你应该使用构造:
where("cat_ids LIKE ?", "%$value%");
我仍然不明白以下问题的答案:
定义不工作。有人调用过回调吗?是回拨吗 代码对结果没有影响?-李·萨弗里特1小时前 放置
Mage::log('blabla',false,'grid.log',true)代码>位于回调方法的开头。然后检查这个日志文件。如果它不是空的-您的方法调用成功
如果您的方法调用-请尝试
Mage::log($collection->getSelectSQL(1), false, 'grid.log', true);
在应用过滤器之前和之后。并尝试在phpmyadmin中运行这些查询。检查结果
尝试在Mage\u Adminhtml\u Block\u Sales\u Order\u Grid类中应用这些更改
以下是我所做的:
$this->addColumn("cat_ids", array(
"header" => Mage::helper('sales')->__('Ship to Name'),
"align" => "center",
"index" => "grand_total",
"renderer" => "My_Class_.....",
"filter_condition_callback" => array($this, '_categoriesFilter')
));
定义不工作。有人调用过回调吗?回调代码对结果没有影响吗?“不工作”当我尝试筛选该列时,它不会返回筛选行使用回调时生成的SQL是什么?如果在应用回调过滤器时没有得到任何结果,那么您的查询可能是错误的。如果我使用$collection
而不是$this->getCollection()->getSelect()
显示了一个致命错误:调用未定义的方法Sathish_Listings_Model_Mysql4_Listings_collection::where()
我使用两个模型“Listings/Listings”和“categories/categories”如果您区分my和您的代码,您将看到,您将$collection发送到您的函数,并且从未使用它。您将筛选器应用于另一个不使用的集合。我不确定,但我认为您的错误出现是因为您没有进行propper继承。而且你的收藏没有Zend方法。你的问题不涉及回调函数。您应该扩展Mage_Core_Model_Mysql4_Collection_Abstract。也许试着听从我最后的建议。当您的回调开始工作时,您可以将它一部分一部分地传输到您的模块。变体,我发布的是100%可行的。
public function render(Varien_Object $row)
{
$value = $row->getData('increment_id') . $row->getData('status');
return $value;
}