0

我覆盖class My_CategoryFilters_Block_Adminhtml_Catalog_Category_Tab_Product extends Mage_Adminhtml_Block_Widget_Grid并将“可见性”列添加到_prepareCollection 和_prepareColumns。每当我尝试过滤时,网格都不会刷新。

我读到Grid 没有出现在 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()
    ));
}
4

1 回答 1

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;
}
于 2012-07-30T17:43:11.510 回答