hello i've trying create grid serializer. upon clicking tab in admin it's displaying correctly. when in grid change select field yes find records. gives foreach error(invalid argument supplied foreach).
layout:
<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>
controller
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(); }
block
<?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 important $this->setdefaultsort('entity_id'); // default sort _preparecoloum below $this->setdefaultfilter(array('in_producted'=>1)); // default have added filter rows, in_products value 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 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 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 $obj){ $catids[$obj->getcategoryid()] = array('position'=>$obj->getposition()); } return $catids; } }
have checked $collection isn't empty/has data? var_dump($collection); before foreach loop , post output?
try replacing:
$collection = mage::getmodel('manager/cat')->getcollection(); $collection->addfieldtofilter('manager_id',$tm_id);
with
$collection = mage::getmodel('manager/cat')->getcollection()->addfieldtoselect('*')->addfieldtofilter('manager_id',$tm_id);
if doesn't work try adding $collection->load();
before foreach loop.
Comments
Post a Comment