How to Add an Admin Grid in Magento 1.8

admingridmagento-1.8

I am trying to show some products based on a attribute value in an admin grid.

This admin grid should be accessible from the admin menu.

Should be in the following format:

                                                                   SAVE

show in Homepage   Product_id   Product_name  type    SKU    searchable     
- yes
- no
- any

Some points about the requirement are:

  • No edit/add button to this grid.
  • The product collection can be saved by SAVE button after checking/unchecking the column "show in Home page". (I think this requires database table)
  • The grid must be accessible from admin menu.

What files should I change to get my requirement? if not, atleast tell me which code to refer in Mage/core files.

Best Answer

Somebody had already posted the answer with a link to inchoo blog. I don't know why the user removed the answer later.

I followed the inchoo blog and made some changes as per my needs.

Make the following directory structure:

app/code/local
     --> {namespace}
         --> {module}
             --> Block
             --> controllers
             --> etc
             --> Helper

Just for example,

 {namespace} => Namespace
 {module}    => Module

Add the following codes as per the paths.

app/etc/modules/{namespace}_{module}

    <Namespace_Module>
        <active>true</active>
        <codePool>local</codePool>
    </Namespace_Module>

app/code/local/{namespace}/{module}/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Namespace_Module>
            <version>0.0.0.1</version>
        </Namespace_Module>
    </modules>
    <global>
        <blocks>
            <namespace_module>
                <class>Namespace_Module_Block</class>
            </namespace_module>
        </blocks>
        <helpers>
            <namespace_module>
                <class>Namespace_Module_Helper</class>
            </namespace_module>
        </helpers>
    </global>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <namespace_module before="Mage_Adminhtml">Namespace_Module_Adminhtml</namespace_module>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
</config>

app/code/local/{namespace}/{module}/etc/adminhtml.xml

<?xml version="1.0"?>
<config>
    <menu>
        <sales> <!-- In menu, change as needed -->
            <children>
                <namespace_module translate="title" module="namespace_module">
                    <sort_order>10</sort_order> <!-- sort order in menu -->
                    <title>module title here</title>
                    <action>adminhtml/order/</action> <!-- controller action to call -->
                </namespace_module>
            </children>
        </sales>
    </menu>
</config>

app/code/local/{namespace}/{module}/Helper/Data.php

<?php
class Namespace_Module_Helper_Data extends Mage_Core_Helper_Abstract
{
}

app/code/local/{namespace}/{module}/Block/Adminhtml/Item/Grid.php

<?php
class Namespace_Module_Block_Adminhtml_Item_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
    public function __construct()
    {
        parent::__construct();
        $this->setId('namespace_module_grid');
        $this->setDefaultSort('entity_id');
        $this->setDefaultDir('DESC');
        $this->setSaveParametersInSession(true);
        $this->setUseAjax(true);
    }
    protected function _prepareCollection()
    {
        $collection = null; //Your collection code here instead of null
        $this->setCollection($collection);
        parent::_prepareCollection();
        return $this;
    }
    protected function _prepareColumns()
    {
        $helper = Mage::helper('namespace_module');
        $currency = (string) Mage::getStoreConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE);

        // To add a column. (column will be added if the field is available)
        $this->addColumn('entity_id', array(
            'header' => $helper->__('Product id'),
            'index'  => 'entity_id',
            'type'   => 'number'
        ));

        return parent::_prepareColumns();
    }

    //Include this if checkboxes are needed for the listed items
    protected function _prepareMassaction(){
        $this->setMassactionIdField('entity_id');
        $this->getMassactionBlock()->setFormFieldName('namespace_module');
        $this->getMassactionBlock()->addItem('status', array(
            'label'=> Mage::helper('namespace_module')->__('Save'),
            'url'  => $this->getUrl('*/*/massSave', array('_current'=>true))  // calling "massSaveAction" method in controller.
        ));
        return $this;
    }
    public function getGridUrl()
    {
        return $this->getUrl('*/*/grid', array('_current'=>true));
    }
}

app/code/local/{namespace}/{module}/Block/Adminhtml/Item.php

<?php
class Namespace_Module_Block_Adminhtml_Item extends Mage_Adminhtml_Block_Widget_Grid_Container
{
    public function __construct()
    {
        $this->_blockGroup = 'namespace_module';
        $this->_controller = 'adminhtml_sales_order';
        $this->_headerText = Mage::helper('namespace_module')->__('Module items');
        parent::__construct();
        $this->_removeButton('add');
    }
}

app/code/local/{namespace}/{module}/controllers/Adminhtml/ItemController.php

<?php
class Namespace_Module_Adminhtml_ItemController extends Mage_Adminhtml_Controller_Action
{
    public function indexAction()
    {
        $this->_title($this->__('page title here'));
        $this->loadLayout();
        $this->_setActiveMenu('active menu name'); //looks like optional to me
        $this->_addContent($this->getLayout()->createBlock('namespace_module/adminhtml_item'));
        $this->renderLayout();
    }
    public function gridAction()
    {
        $this->loadLayout();
        $this->getResponse()->setBody(
            $this->getLayout()->createBlock('namespace_module/adminhtml_item_grid')->toHtml()
        );
    }
    public function massSaveAction(){
        $urlParam = $this->getRequest()->getParam('namespace_module');
        if(!is_array($urlParam)) {
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('namespace_module')->__('Please select Items.'));
        } 
        else {
            try {
                // Add action code here
                // Like, sql queries to update database table

                $this->_getSession()->addSuccess($this->__('Total of %d Items were successfully updated.', count($urlParam)));
            }
            catch (Mage_Core_Exception $e){
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            }
            catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError(Mage::helper('namespace_module')->__('There was an error updating Items.'));
                Mage::logException($e);
            }
        }
        $this->_redirect('*/*/index');
    }
}
Related Topic