こんにちは、グリッドシリアライザーを作成しようとしています。Admin のタブをクリックすると、正しく表示されます。しかし、グリッドでは、選択フィールドを [はい] から [任意] に変更して、すべてのレコードを検索します。Foreachエラーが発生します(foreachに無効な引数が指定されました)。
レイアウト:
<manager_adminhtml_manager_category>
<block type="core/text_list" name="root" output="toHtml">
<block type="manager/adminhtml_manager_edit_tab_category" name="categories.grid"/>
<block type="adminhtml/widget_grid_serializer" name="grid_serializer">
<reference name="grid_serializer">
<action method="initSerializerBlock">
<grid_block_name>categories.grid</grid_block_name>
<data_callback>getSelectedCategories</data_callback>
<hidden_input_name>links[category]</hidden_input_name>
<reload_param_name>category</reload_param_name>
</action>
<action method="addColumnInputName">
<input_name>position</input_name>
</action>
</reference>
</block>
</block>
</manager_adminhtml_manager_category>
<manager_adminhtml_manager_categorygrid>
<block type="core/text_list" name="root" output="toHtml">
<block type="manager/adminhtml_manager_edit_tab_category" name="categories.grid"/>
</block>
</manager_adminhtml_manager_categorygrid>
コントローラ
class Excellence_Manager_Adminhtml_ManagerController extends Mage_Adminhtml_Controller_action
{
public function categoryAction(){
$this->loadLayout();
$this->getLayout()->getBlock('categories.grid')->setCategory($this->getRequest()->getPost('category',null));
$this->renderLayout();
}
public function categorygridAction(){
$this->loadLayout();
$this->getLayout()->getBlock('categories.grid')
->setCategory($this->getRequest()->getPost('category', null));
$this->renderLayout();
}
ブロック
<?php
class Excellence_Manager_Block_Adminhtml_Manager_Edit_Tab_Category extends Mage_Adminhtml_Block_Widget_Grid
{
public function __construct()
{
parent::__construct();
$this->setId('categoryGrid');
$this->setUseAjax(true); // Using ajax grid is important
$this->setDefaultSort('entity_id'); // default sort from the _prepareColoum below
$this->setDefaultFilter(array('in_producted'=>1)); // By default we have added a filter for the rows, that in_products value to be 1
$this->setSaveParametersInSession(false);
}
protected function _prepareCollection()
{
$collection = Mage::getResourceModel('catalog/category_collection');
$tm_id = $this->getRequest()->getParam('id');
if(!isset($tm_id)) {
$tm_id = 0;
}
Mage::getResourceModel('manager/cat')->addGridPosition($collection,$tm_id);
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _addColumnFilterToCollection($column)
{
// Set custom filter for in product flag
if ($column->getId() == 'in_producted') {
$ids = $this->_getSelectedCategories();
if (empty($ids)) {
$ids = 0;
}
if ($column->getFilter()->getValue()) {
$this->getCollection()->addFieldToFilter('entity_id', array('in'=>$ids));
} else {
if($ids) {
$this->getCollection()->addFieldToFilter('entity_id', array('nin'=>$ids));
}
}
} else {
parent::_addColumnFilterToCollection($column);
}
return $this;
}
protected function _prepareColumns()
{
$this->addColumn('in_producted', array(
'header_css_class' => 'a-center',
'type' => 'checkbox',
'name' => 'customer',
'values' => $this->_getSelectedCategories(),
'align' => 'center',
'index' => 'entity_id'
));
$this->addColumn('entity_id', array(
'header' => Mage::helper('customer')->__('ID'),
'width' => '50px',
'index' => 'entity_id',
'type' => 'number',
));
$this->addColumn('name', array(
'header' => Mage::helper('customer')->__('Name'),
'index' => 'name'
));
$this->addColumn('is_active', array(
'header' => Mage::helper('customer')->__('Status'),
'width' => '150',
'index' => 'is_active'
));
return parent::_prepareColumns();
}
protected function _getSelectedCategories() // Used in grid to return selected customers values.
{
$categories = array_keys($this->getSelectedCategories());
return $categories;
}
public function getGridUrl()
{
return $this->_getData('grid_url') ? $this->_getData('grid_url') : $this->getUrl('*/*/categorygrid', array('_current'=>true));
}
public function getSelectedCategories()
{
// Categories Data
$tm_id = $this->getRequest()->getParam('id');
if(!isset($tm_id)) {
$tm_id = 0;
}
$collection = Mage::getModel('manager/cat')->getCollection();
$collection->addFieldToFilter('manager_id',$tm_id);
$catIds = array();
foreach($collection as $obj){
$catIds[$obj->getCategoryId()] = array('position'=>$obj->getPosition());
}
return $catIds;
}
}