如何将 foreach 结果添加到列网格?
How to add foreach result to column grid?
我尝试在网格列中获取 foreach 结果(我有结果,但我不知道如何将其放入每个类别 ID 的列中)。
我的grid.php
public function _prepareCollection()
{
$subcategories = Mage::getModel('catalog/category')
->setStoreId(Mage::app()->getStore()->getId())
->getCollection()
->addAttributeToSelect('*')
->setOrder('parent_id', 'ASC');
$categories = array();
foreach ($subcategories as $category){
//do something with $category and put it in Route column
if ($category['level'] > 1) {
$categories[$category['entity_id']] = array('category_route' => $category['level'] == 2 ? $category['name'] : $categories[$category['parent_id']]['category_route'] ." -> ". $category['name']);
}
var_dump($categories[$category['entity_id']]);
}
$collection = Mage::getModel('thorleif/commerciaux')->getCollection();
$collection->addFieldToFilter('entity_id',array("nin"=>array(1,2)));
$this->setCollection($collection);
return parent::_prepareCollection();
}
public function _prepareColumns()
{
$this->addColumn('entity_id',
array(
'header' => 'ID',
'align' => 'left',
'width' => '10%',
'index' => 'entity_id'
)
);
$this->addColumn('name',
array(
'header' => 'Category Name',
'align' => 'left',
'index' => 'name'
)
);
$this->addColumn('route',
array(
'header' => 'Route',
'align' => 'left',
'index' => array($category['name'] .'>'. $category['name'])
)
);
var_dump的结果就像this
您不能在必填栏中直接显示您的自定义 collection。您需要使用 renderer
class 来满足此要求。您需要创建一个渲染器文件夹 extends
Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
class。
class Namespace_Module_Block_Product extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
public function render(Varien_Object $row)
{
$productId = $row->getData($this->getColumn()->getIndex());
$product = Mage::getModel('catalog/product')->load($productId);
$value = '<img src="">';
if($product->getImage()!= 'noselection')
{
$value='<img src="' . $product->getImageUrl() . '" width="100" height="100" />';
}
return $value;
}
}
在 Grid 列中需要使用渲染器参数调用此 class,如下所示。
$this->addColumn(
'product_id',
[
'header' => __('Product Name'),
'sortable' => true,
'index' => 'product_id',
'renderer' => 'Namespace\Module\Block\Product'
]
);
您还可以将类别 ID 传递给此特定渲染器 class。
我尝试在网格列中获取 foreach 结果(我有结果,但我不知道如何将其放入每个类别 ID 的列中)。
我的grid.php
public function _prepareCollection()
{
$subcategories = Mage::getModel('catalog/category')
->setStoreId(Mage::app()->getStore()->getId())
->getCollection()
->addAttributeToSelect('*')
->setOrder('parent_id', 'ASC');
$categories = array();
foreach ($subcategories as $category){
//do something with $category and put it in Route column
if ($category['level'] > 1) {
$categories[$category['entity_id']] = array('category_route' => $category['level'] == 2 ? $category['name'] : $categories[$category['parent_id']]['category_route'] ." -> ". $category['name']);
}
var_dump($categories[$category['entity_id']]);
}
$collection = Mage::getModel('thorleif/commerciaux')->getCollection();
$collection->addFieldToFilter('entity_id',array("nin"=>array(1,2)));
$this->setCollection($collection);
return parent::_prepareCollection();
}
public function _prepareColumns()
{
$this->addColumn('entity_id',
array(
'header' => 'ID',
'align' => 'left',
'width' => '10%',
'index' => 'entity_id'
)
);
$this->addColumn('name',
array(
'header' => 'Category Name',
'align' => 'left',
'index' => 'name'
)
);
$this->addColumn('route',
array(
'header' => 'Route',
'align' => 'left',
'index' => array($category['name'] .'>'. $category['name'])
)
);
var_dump的结果就像this
您不能在必填栏中直接显示您的自定义 collection。您需要使用 renderer
class 来满足此要求。您需要创建一个渲染器文件夹 extends
Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
class。
class Namespace_Module_Block_Product extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
public function render(Varien_Object $row)
{
$productId = $row->getData($this->getColumn()->getIndex());
$product = Mage::getModel('catalog/product')->load($productId);
$value = '<img src="">';
if($product->getImage()!= 'noselection')
{
$value='<img src="' . $product->getImageUrl() . '" width="100" height="100" />';
}
return $value;
}
}
在 Grid 列中需要使用渲染器参数调用此 class,如下所示。
$this->addColumn(
'product_id',
[
'header' => __('Product Name'),
'sortable' => true,
'index' => 'product_id',
'renderer' => 'Namespace\Module\Block\Product'
]
);
您还可以将类别 ID 传递给此特定渲染器 class。