我覆盖class My_CategoryFilters_Block_Adminhtml_Catalog_Category_Tab_Product extends Mage_Adminhtml_Block_Widget_Grid
并将列'可见性'添加到_prepareCollection和_prepareColumns。每当我尝试过滤时,网格都不会刷新。
我看了Grid doesn't appear in custom admin module in Magento和Magento Grid Container Block not loading grid。两者都建议创建一个重载的控制器。拥有更多Magento经验的人能否对我所缺少的东西有所了解?
提前致谢。
编辑:从代码中删除了'渲染器'和'控制器'。
CategoryFilters
├── Block
│ └── Adminhtml
│ └── Catalog
│ └── Category
│ └── Tab
│ └── Product.php
├── Core
│ └── etc
│ └── config.xml
└── etc
protected function _prepareCollection()
{
if ($this->getCategory()->getId()) {
$this->setDefaultFilter(array('in_category'=>1));
}
$store = $this->_getStore();
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('name')
->addAttributeToSelect('sku')
->addAttributeToSelect('price')
->addAttributeToSelect('store_id')
->addStoreFilter($this->getRequest()->getParam('store'))
->joinField('position',
'catalog/category_product',
'position',
'product_id=entity_id',
'category_id='.(int) $this->getRequest()->getParam('id', 0),
'left')
->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner', $store->getId());
;
$this->setCollection($collection);
$this->getCollection()->addWebsiteNamesToResult();
if ($this->getCategory()->getProductsReadonly()) {
$productIds = $this->_getSelectedProducts();
if (empty($productIds)) {
$productIds = 0;
}
$this->getCollection()->addFieldToFilter('entity_id', array('in'=>$productIds));
}
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
$this->addColumn('visibility', array(
'header' => Mage::helper('catalog')->__('Visibility'),
'width' => '100',
'sortable' => false,
'index' => 'visibility',
'type' => 'options',
'options' => Mage::getModel('catalog/product_visibility')->getOptionArray(),
#'renderer' => new Rogue_CategoryFilters_Block_Adminhtml_Renderer_Visibility()
));
}
答案 0 :(得分:0)
答案有点超出了我的理解范围。我没有太深入挖掘,但我认为它必须按照父母处理我的请求的方式进行。
发布更新的代码段。
protected function _addColumnFilterToCollection($column)
{
// Set custom filter for in category flag
if ($column->getId() == 'in_category') {
$productIds = $this->_getSelectedProducts();
if (empty($productIds)) {
$productIds = 0;
}
if ($column->getFilter()->getValue()) {
$this->getCollection()->addFieldToFilter('entity_id', array('in'=>$productIds));
}
elseif(!empty($productIds)) {
$this->getCollection()->addFieldToFilter('entity_id', array('nin'=>$productIds));
}
}
else if ($column->getId() == 'websites') {
$this->getCollection()->joinField('websites',
'catalog/product_website',
'website_id',
'product_id=entity_id',
null,
'left');
return parent::_addColumnFilterToCollection($column);
}
else {
parent::_addColumnFilterToCollection($column);
}
return $this;
}
protected function _prepareCollection()
{
$store = $this->_getStore();
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('name')
->addAttributeToSelect('sku')
->addAttributeToSelect('price')
->addAttributeToSelect('store_id')
->addStoreFilter($this->getRequest()->getParam('store'))
->joinField('position',
'catalog/category_product',
'position',
'product_id=entity_id',
'category_id='.(int) $this->getRequest()->getParam('id', 0),
'left');
if ($store->getId()) {
$collection->addStoreFilter($store);
$collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner', $store->getId());
}
else {
$collection->addAttributeToSelect('visibility');
}
$this->setCollection($collection);
if ($this->getCategory()->getProductsReadonly()) {
$productIds = $this->_getSelectedProducts();
if (empty($productIds)) {
$productIds = 0;
}
$this->getCollection()->addFieldToFilter('entity_id', array('in'=>$productIds));
}
parent::_prepareCollection();
$this->getCollection()->addWebsiteNamesToResult();
return $this;
}