Magento - 覆盖网格不刷新
问题描述:
我覆盖class My_CategoryFilters_Block_Adminhtml_Catalog_Category_Tab_Product extends Mage_Adminhtml_Block_Widget_Grid
并将列'可见性'添加到_prepareCollection和_prepareColumns。每当我尝试过滤时,网格都不会刷新。Magento - 覆盖网格不刷新
我读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()
));
}
答
这个答案有点超出了我的理解范围。我没有深入挖掘,但我认为它必须按照父母处理我的请求的方式进行。
发布更新后的代码片段。
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;
}
任何JS错误? – 2012-07-27 19:28:41
您是否检查了此链接? http://stackoverflow.com/questions/8879130/magento-how-do-i-add-a-column-to-products-selection-in-admin-category-page – Kalpesh 2012-07-27 19:57:12
@Tim没有JS错误。 – 2012-07-27 20:03:57