网格列过滤器不适用于magento自定义模块中的两个模型 [英] Grid column filter not working with two models in magento custom module
问题描述
我正在使用magento后端的自定义模块,在使用过滤器回调函数时,此过滤器不工作!
任何人都可以建议我吗?
Thanks!
我尝试了一些这样的代码, Grid.php
保护函数_prepareCollection()
{
$ collection = Mage :: getModel('listing / listings') - > getCollection();
$ this-> setCollection($ collection);
返回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
));
$ b $ 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' =>数组($ this,'_categoriesFilter')
));
$ b $ this-> addColumn(url_key,array(
header=> Mage :: helper(linkmanagement) - > __(URL
index=>url_key,
width=>200px,
));
$ this-> addColumn('status',
array(
'header'=> Mage :: helper('linkmanagement') - > __('状态'),
'index'=>'status',
'type'=>'options',
'options'=> array('1'=> Mage :: helper('linkmanagement') - > __('Active'),
'0'=> Mage :: helper('linkmanagement') - > __('Inactive')),
)
);
返回parent :: _ prepareColumns();
$ b $回调函数
protected function _categoriesFilter($ collection,$ column)
{
if(!$ value = $ column-> getFilter() - > getValue()){
return $ this;
$ b $ this-> getCollection() - > getSelect() - >其中(
cat_ids?
,%$ value% );
返回$ 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 =学院,学院
返回$ result;
code
$ b 注意:
渲染器工作正常,
只有过滤器回调函数不工作!!! 我自己的渲染器,并试图检查您的回调。
替换您的回调方法
< code $ $ this-> getCollection() - > getSelect()
/ p>
$ collection
这里是我的建议:
Magento过滤器假定不是100%的相似性。我的意思是,如果你想应用过滤器,你应该使用结构:
where(cat_ids LIKE?,%$ value %);
我还是不明白有关问题的答案如下:
定义不起作用。回调是否被称为?回调
代码是否对结果没有影响? - Lee Saferite 1小时前
放置 Mage :: log('blabla',false,'grid。 log',true);
在回调方法的开头。然后检查这个日志文件。如果它不是空的 - 你的方法调用成功。
如果你的方法调用 - 尝试
Mage :: log($ collection-> getSelectSQL(1),false,'grid.log',true);
过滤器应用前后。并尝试在phpmyadmin中运行这些查询。检查结果
尝试在Mage_Adminhtml_Block_Sales_Order_Grid类中应用这些更改
$ this-> addColumn(cat_ids,array(
('sales') - > __('Ship to Name'),
align=>center,
index=>
renderer=>My_Class _.....,
filter_condition_callback=> array($ this,'_categoriesFilter')
));
....
protected function _categoriesFilter($ collection,$ column)
{
if(!$ value = $ column-> getFilter() - > getValue()){
返回$ this;
$ collection-> getSelect() - > where(
status LIKE?,%$ value%
);
返回$ this;
code
< pre $ public function render(Varien_Object $ row)
{
$ value = $ row-> getData('increment_id')。 $行向>的getData(状态);
返回$ value;
}
I'm working with custom module for the magento backend, in this filter not working when using filter calback! Can anyone suggest me? Thanks!
I have tried some codes like this,
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();
}
callback function
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;
}
}
Note: Renderer working fine., only filter callback function is not working!!!
I checked your example with my own renderer and tried to check your callback.
Replace in your callback method
$this->getCollection()->getSelect()
with
$collection
Here are my pieces of advice:
Magento filter assumes not 100% similarity. I mean that if you want to apply filter you should use construction:
where("cat_ids LIKE ?", "%$value%");
I still don't understand answer on question below:
Define not working. Is the callback ever being called? Is the callback code having no affect on the results? – Lee Saferite 1 hour ago
Put Mage::log('blabla', false, 'grid.log', true);
at the beginning of the callback method. Then check this log file. If it's not empty - your method calls successfully.
If your method calls - try to
Mage::log($collection->getSelectSQL(1), false, 'grid.log', true);
before and after filter applying. And try to run these queries in phpmyadmin. Check the result
Try to apply these changes inside Mage_Adminhtml_Block_Sales_Order_Grid class
Here what I did:
$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自定义模块中的两个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!