网格列过滤器不适用于 magento 自定义模块中的两个模型
Grid column filter not working with two models in magento custom module
我正在为 magento 后端使用自定义模块,在使用过滤器回调时此过滤器不起作用!
谁能建议我?
谢谢!
我试过一些这样的代码,
Grid.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();
}
回调函数
protected function _categoriesFilter($collection, $column)
{
if (!$value = $column->getFilter()->getValue()) {
return $this;
}
$this->getCollection()->getSelect()->where(
"cat_ids ?"
, "%$value%");
return $this;
}
Categories.php
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()
和
$collection
以下是我的建议:
Magento 过滤器假定不是 100% 的相似性。我的意思是,如果你想应用过滤器,你应该使用构造:
where("cat_ids LIKE ?", "%$value%");
我还是不明白下面问题的答案:
Define not working. Is the callback ever being called? Is the callback
code having no affect on the results? – Lee Saferite 1 hour ago
将Mage::log('blabla', false, 'grid.log', true);
放在回调方法的开头。然后检查这个日志文件。如果它不为空 - 您的方法调用成功。
如果您的方法调用 - 尝试
Mage::log($collection->getSelectSQL(1), false, 'grid.log', true);
过滤器应用前后。并尝试在 phpmyadmin 中 运行 这些查询。检查结果
尝试在 Mage_Adminhtml_Block_Sales_Order_Grid class
中应用这些更改
这是我所做的:
$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')
));
.....
protected function _categoriesFilter($collection, $column)
{
if (!$value = $column->getFilter()->getValue()) {
return $this;
}
$collection->getSelect()->where(
"status LIKE ?", "%$value%"
);
return $this;
}
...
public function render(Varien_Object $row)
{
$value = $row->getData('increment_id') . $row->getData('status');
return $value;
}
我正在为 magento 后端使用自定义模块,在使用过滤器回调时此过滤器不起作用! 谁能建议我? 谢谢!
我试过一些这样的代码,
Grid.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();
}
回调函数
protected function _categoriesFilter($collection, $column)
{
if (!$value = $column->getFilter()->getValue()) {
return $this;
}
$this->getCollection()->getSelect()->where(
"cat_ids ?"
, "%$value%");
return $this;
}
Categories.php
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()
和
$collection
以下是我的建议:
Magento 过滤器假定不是 100% 的相似性。我的意思是,如果你想应用过滤器,你应该使用构造:
where("cat_ids LIKE ?", "%$value%");
我还是不明白下面问题的答案:
Define not working. Is the callback ever being called? Is the callback code having no affect on the results? – Lee Saferite 1 hour ago
将Mage::log('blabla', false, 'grid.log', true);
放在回调方法的开头。然后检查这个日志文件。如果它不为空 - 您的方法调用成功。
如果您的方法调用 - 尝试
Mage::log($collection->getSelectSQL(1), false, 'grid.log', true);
过滤器应用前后。并尝试在 phpmyadmin 中 运行 这些查询。检查结果
尝试在 Mage_Adminhtml_Block_Sales_Order_Grid class
中应用这些更改这是我所做的:
$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')
));
.....
protected function _categoriesFilter($collection, $column)
{
if (!$value = $column->getFilter()->getValue()) {
return $this;
}
$collection->getSelect()->where(
"status LIKE ?", "%$value%"
);
return $this;
}
...
public function render(Varien_Object $row)
{
$value = $row->getData('increment_id') . $row->getData('status');
return $value;
}