Magento – Search in custom admin grid redirects to dashboard

admingridmagento-1.7

I have created a custom module to display some data in admin grid, but when I am trying to search it is redirecting me to admin dashboard.
Below is my grid.php code

class My_Custom_Block_Adminhtml_Business_Grid extends Mage_Adminhtml_Block_Widget_Grid {

public function __construct() {
    parent::__construct();
    $this->setId('custom_grid');
    $this->setDefaultSort('id');
    $this->setDefaultDir('desc');
    $this->setUseAjax(true);
    $this->setSaveParametersInSession(true);
}

/**
 * Prepare collection for grid
 *
 * @return Mage_Adminhtml_Block_Widget_Grid
 */
protected function _prepareCollection() {
    $collection = Mage::getSingleton('custom/business')->getCollection();
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

/**
 * Prepare columns for grid
 */
protected function _prepareColumns() {
    $this->addColumn('id', array(
        'header' => Mage::helper('custom')->__('ID'),
        'align' => 'right',
        'width' => '50px',
        'index' => 'id',
    ));

    $this->addColumn('name', array(
        'header' => Mage::helper('custom')->__('Name'),
        'align' => 'left',
        'index' => 'extension_name',
    ));

    $this->addColumn('address', array(
        'header' => Mage::helper('custom')->__('Address'),
        'align' => 'left',
        'index' => 'address',
    ));

    $this->addColumn('phone', array(
        'header' => Mage::helper('custom')->__('Phone'),
        'align' => 'left',
        'index' => 'phone',
    ));

    $this->addColumn('email', array(
        'header' => Mage::helper('custom')->__('Email'),
        'align' => 'left',
        'index' => 'email',
    ));

    $this->addExportType('*/*/exportCsv', Mage::helper('custom')->__('CSV'));
    $this->addExportType('*/*/exportXml', Mage::helper('custom')->__('XML'));

    return parent::_prepareColumns();
}

/**
 * Generate grid data in csv
 *
 * @return string
 */
public function getCsv() {
    $csv = '';
    $this->_isExport = true;
    $this->_prepareGrid();

    $this->_afterLoadCollection();

    $data = array();
    foreach ($this->_columns as $column) {
        if (!$column->getIsSystem()) {
            $data[] = '"' . $column->getExportHeader() . '"';
        }
    }
    $csv.= implode(',', $data) . "\n";

    foreach ($this->getCollection() as $item) {
        $data = array();
        foreach ($this->_columns as $column) {
            if (!$column->getIsSystem()) {
                $data[] = '"' . str_replace(array('"', '\\'), array('""', '\\\\'), $column->getRowFieldExport($item)) . '"';
            }
        }
        $csv.= implode(',', $data) . "\n";
    }

    if ($this->getCountTotals()) {
        $data = array();
        foreach ($this->_columns as $column) {
            if (!$column->getIsSystem()) {
                $data[] = '"' . str_replace(array('"', '\\'), array('""', '\\\\'), $column->getRowFieldExport($this->getTotals())) . '"';
            }
        }
        $csv.= implode(',', $data) . "\n";
    }

    return $csv;
}

/**
 * Generate xml for grid data
 *
 * @return string
 */
public function getXml() {
    $this->_isExport = true;
    $this->_prepareGrid();

    $this->_afterLoadCollection();
    $indexes = array();
    foreach ($this->_columns as $column) {
        if (!$column->getIsSystem()) {
            $indexes[] = $column->getIndex();
        }
    }
    $xml = '<?xml version="1.0" encoding="UTF-8"?>';
    $xml.= '<cards>';
    foreach ($this->getCollection() as $item) {
        $xml.= $item->toXml($indexes);
    }
    if ($this->getCountTotals()) {
        $xml.= $this->getTotals()->toXml($indexes);
    }
    $xml.= '</cards>';
    return $xml;
}

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

}

I have tried both ways by overidding

protected function _addColumnFilterToCollection($column)

my custom grid.php and also using the parent method. But nothing works.
And also I am getting all the data from a single table.

Please help me out in finding some solution and what is the issue.
Thanks in advance.

Best Answer

When using ajax for grid refresh ($grid->setUseAjax(true)), it's necessary to define (override) Mage_Adminhtml_Block_Widget_Grid::getGridUrl() to return the proper AJAX content endpoint; this URL will be inserted into the JS. You have done this and have indicated "current module, current controller, grid action" as the arguments.

The next step is to define the indicated controller action to return just the grid content. You can do this a couple of ways: with layout and without. It's more conventional to use layout as it provides flexibility and ensures certain layout-based events are dispatched. Typically you can define a layout update handle with just the grid block declared as an output block. You have done this as well.

However, the controller action which handles the AJAX request needs to not invoke the typical full adminhtml layout, namely the <default> handle. By calling loadLayout(false), the default handle is omitted. For more information, see Mage_Core_Controller_Action::loadLayout():

public function loadLayout($handles = null, $generateBlocks = true, $generateXml = true)
{
    // if handles were specified in arguments load them first
    if (false!==$handles && ''!==$handles) {
        $this->getLayout()->getUpdate()->addHandle($handles ? $handles : 'default');
    }
    // ... etc. ...
}
Related Topic