Magento – Save Grid Checkbox Values

adminformadminhtmlgridgrid-serlization

I'm working with a custom item model that I would like to display on a grid with checkboxes on each row. The grid does not need 'edit' functionality, simply the checkbox save functionality. The grid currently displays correctly, and hits my saveAction when I click save, but the checkbox data doesn't seem to be included in the post data. I expect an array of item IDs that are checked after save to be posted, so that I can map this data to a different model where it is needed.

Here is the relevant code:

app/code/local/Company/MyModule/Block/Adminhtml/Homepage/Grid/Container.php:

<?php
class Company_MyModule_Block_Adminhtml_Homepage_Grid_Container extends Mage_Adminhtml_Block_Widget_Grid_Container
{
    public function __construct()
    {
        parent::__construct();
        $this->_objectId = 'id';
        $this->_blockGroup = 'mymodule';
        $this->_controller = 'adminhtml_homepage';

        $this->_updateButton('save', 'label', Mage::helper('mymodule')->__('Save'));
        $this->_updateButton('delete', 'label', Mage::helper('mymodule')->__('Delete'));
        $this->_removeButton('add');

        $this->_addButton('saveandcontinue', array(
            'label'     => Mage::helper('adminhtml')->__('Save And Continue Edit'),
            'onclick'   => "setLocation('{$this->getUrl('*/*/save')}')",
            'class'     => 'save',
        ), -100);
    }

    public function getHeaderText()
    {
        return Mage::helper('mymodule')->__('Title Goes Here');
    }

}

app/code/local/Company/MyModule/Block/Adminhtml/Homepage/Grid.php:

<?php
class Company_MyModule_Block_Adminhtml_Homepage_Grid extends Mage_Adminhtml_Block_Widget_Grid
{

    public function __construct()
    {
        parent::__construct();
        $this->setId('company_mymodule_homepage_grid');
        $this->setDefaultSort('ID'); // Primary ID
        $this->setDefaultDir('DESC'); // Sorting order
        $this->setSaveParametersInSession(true);
        $this->setUseAjax(true);
    }

    protected function _prepareMassaction()
    {
        return $this;
    }

    protected function _prepareCollection()
    {
        $collection = Mage::getModel('mymodule_db/item')
            ->getCollection();

        $this->setCollection($collection);
        parent::_prepareCollection();
        return $this;
    }

    protected function _prepareColumns()
    {


        $helper = Mage::helper('mymodule');
        $currency = (string) Mage::getStoreConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE);

        $this->addColumn('itemCheckbox', array(
            'index'      => 'ID',
            'type'       => 'checkbox',
            'width'      => 20,
            'sortable'   => false,
            'field_name' => 'map[]'
        ));

        $this->addColumn('item_id', array(
            'header' => $helper->__('ID'),
            'index'  => 'item_id'
        ));

        $this->addColumn('item_name', array(
            'header' => $helper->__('Name'),
            'index'  => 'item_name'
        ));

        $this->addExportType('*/*/exportRelatedCsv', $helper->__('CSV'));
        $this->addExportType('*/*/exportRelatedXml', $helper->__('XML'));

        return parent::_prepareColumns();
    }



    public function getGridUrl()
    {
        return $this->getUrl('*/*/grid', array('_current'=>true));
    }


    /**
     * Determines whether to display the tab
     * In our case, we show it on the product edit and new product pages
     *
     * @return bool
     */
    public function canShowTab()
    {

        // Pull in our product
        $product = Mage::registry('current_product');

        // The request
        $request = Mage::app()->getRequest();

        // Show if we have a product already (edit product page)
        if ($product->getId()) {
            return true;
        }

        // Do not show if we don't have any attribute set
        if (!$product->getAttributeSetId()) {
            return false;
        }

        // If we do have an attribute set, it's okay to show (add new product page)
        if ($request->getParam('set')) {
            return true;
        }

        return false;

    }


}

app/code/local/Company/MyModule/controllers/Adminhtml/HomepageController.php:

<?php
class Company_MyModule_Adminhtml_HomepageController extends Mage_Adminhtml_Controller_Action
{
    /**
     * Get custom products grid and serializer block
     */
    public function indexAction()
    {
//        $this->loadLayout()
//            ->_addContent(
//                $this->getLayout()
//                    ->createBlock('pixwordpress/adminhtml_homepage_grid_container'))
//            ->renderLayout();

        $this->loadLayout();
        $this->renderLayout();
    }

    public function saveAction()
    {

        if ($this->getRequest()->getPost())
        {
            echo 'got post data!';
        } else {
            echo 'sadly, no post data';
        }

        //$this->_redirect('*/*/');
    }

}

app/design/adminhtml/default/newinstall/layout/company/customlayout.xml:

<adminhtml_homepage_index>
    <reference name="content">
        <block type="mymodule/adminhtml_homepage_grid_container" name="adminhtml.homepage.container" />
    </reference>
    <!--<reference name="content">-->
        <!--<block type="mymodule/adminhtml_homepage_grid" name="ajaxgrid.index" />-->
    <!--</reference>-->
</adminhtml_homepage_index>

<adminhtml_mymodule_homepage_grid_container>
    <block type="core/text_list" name="root" output="toHtml">
        <block type="mymodule/adminhtml_homepage_grid" name="mymodule.homepage.grid"/>
        <block type="adminhtml/widget_grid_serializer" name="homepage_grid_serializer">
            <reference name="homepage_grid_serializer">
                <action method="initSerializerBlock">
                    <grid_block_name>mymodule.homepage.grid</grid_block_name>
                    <data_callback>getInitialHomepageGrid</data_callback>
                    <hidden_input_name>map[]</hidden_input_name>
                    <reload_param_name>item</reload_param_name>
                </action>
            </reference>
        </block>
    </block>
</adminhtml_mymodule_homepage_grid_container>

<!--<adminhtml_mymodule_homepage_grid>-->
    <!--<block type="core/text_list" name="root" output="toHtml">-->
        <!--<block type="mymodule/adminhtml_homepage_grid" name="mymodule.homepage.grid"/>-->
    <!--</block>-->
<!--</adminhtml_mymodule_homepage_grid>-->

Any help greatly appreciated!

Best Answer

You most likely need to generate the secret key when creating the URL to POST to:

Mage::helper("adminhtml")->getUrl("acompany_mymodule/index/index");
$key = Mage::getSingleton('adminhtml/url')
             ->getSecretKey("acompany_mymodule/index/","index");

Reference:

Related Topic