3

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!

  • Update: I've removed the code where I was pulling my grid in from my controller indexAction in favor of the serializer I've added to customlayout.xml. The grid displays, but again does not post the form content when it hits the saveAction(), and pagination / filtering also result in 404 pages. – carbide20 Jun 26 '15 at 16:25
2

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:

  • 1
    I don't quite understand how this relates to what I'm working on here. Can you please provide some insight? This answer is related to generating a link to an admin page, and I'm unsure where or why I have to do this. – carbide20 Jun 25 '15 at 20:39
  • I get what the key is used for, but submitting from an admin module to an admin controller shouldn't require me to manually generate a new key, should it? When it hits my saveAction() the URL already has a key in it. – carbide20 Jun 25 '15 at 21:49
1

In this case, one tricky solution i am suggesting it is not fully solved your problem but 90% it will solve. you need to use massaction here and in your controller mass action you can get the selected Ids and use save logic in given mass action you can also call model in massaction and use it like save action. it will solve your problem except button will be in grid not in grid container. I hope this will helpful for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.