0

您好我正在尝试创建一个网格序列化程序。单击管理中的选项卡后,它会正确显示。但是在网格中我将选择字段从 Yes 更改为 Any 以查找所有记录。它给出了 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;
    }


}
4

1 回答 1

0

您是否检查过 $collection 不是空的/有数据?你能 var_dump($collection); 在你的 foreach 循环之前并发布输出?

通过替换来试试这个:

$collection = Mage::getModel('manager/cat')->getCollection();
$collection->addFieldToFilter('manager_id',$tm_id);

$collection = Mage::getModel('manager/cat')->getCollection()->addFieldToSelect('*')->addFieldToFilter('manager_id',$tm_id);

如果这不起作用,请尝试$collection->load();在 foreach 循环之前添加。

于 2013-07-11T14:02:33.293 回答