In Custom Grid Search Button Is Not Working
-
06-10-2020 - |
Domanda
i have created custom grid at admin side. in grid i created dropdown list. but when i select any value from dropdown for searching, then search is not working...
C:\xampp2\htdocs\testmagento\app\code\local\Sigmasolve\Makemodel\Block\Adminhtml\Makemodel\Grid.php
<?php
class Sigmasolve_Makemodel_Block_Adminhtml_Makemodel_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
public function __construct()
{
parent::__construct();
$this->setId('makemodelGrid');
// This is the primary key of the database
$this->setDefaultSort('id');
$this->setDefaultDir('ASC');
$this->setSaveParametersInSession(true);
$this->setUseAjax(true);
}
protected function _prepareMassaction()
{
$this->setMassactionIdField('id');
$this->getMassactionBlock()->setFormFieldName('makemodel');
$this->getMassactionBlock()->addItem('delete', array(
'label' => Mage::helper('makemodel')->__('Delete'),
'url' => $this->getUrl('*/*/massDelete', array('' => '')),
'confirm' => Mage::helper('makemodel')->__('Are you sure?')
));
return $this;
}
protected function _prepareCollection()
{
$collection = Mage::getModel('makemodel/makemodel')->getCollection();
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
$this->addExportType('*/*/exportCsv', Mage::helper('makemodel')->__('CSV'));
$this->addExportType('*/*/exportXml', Mage::helper('makemodel')->__('XML'));
$this->addColumn('id', array(
'header' => Mage::helper('makemodel')->__('Id'),
'align' =>'right',
'width' => '50px',
'index' => 'id',
));
$this->addColumn('manufacturer', array(
'header' => Mage::helper('makemodel')->__('Manufacturer'),
'align' =>'left',
'index' =>'manufacturer',
'type' =>'options',
'options'=> Mage::getModel('makemodel/makemodel')->getManufacturers()
));
$this->addColumn('model', array(
'header' => Mage::helper('makemodel')->__('Model'),
'align' =>'left',
'index' => 'model',
'type' =>'options',
'options'=> Mage::getModel('makemodel/makemodel')->getModels()
));
$this->addColumn('capacity', array(
'header' => Mage::helper('makemodel')->__('Capacity'),
'align' =>'left',
'index' => 'capacity',
'type' =>'options',
'options'=> Mage::getModel('makemodel/makemodel')->getCapacitys()
));
$this->addColumn('year_from', array(
'header' => Mage::helper('makemodel')->__('Year-From'),
'align' =>'left',
'index' => 'year_from',
'type' =>'options',
'options'=> Mage::getModel('makemodel/makemodel')->getYearFroms()
));
$this->addColumn('year_to', array(
'header' => Mage::helper('makemodel')->__('Year-To'),
'align' =>'left',
'index' => 'year_to',
'type' =>'options',
'options'=> Mage::getModel('makemodel/makemodel')->getYearTos()
));
$this->addColumn('fuel', array(
'header' => Mage::helper('makemodel')->__('Fuel'),
'align' =>'left',
'index' => 'fuel',
'type' =>'options',
'options'=> Mage::getModel('makemodel/makemodel')->getFuels()
));
$this->addColumn('ktyp', array(
'header' => Mage::helper('makemodel')->__('KTYP'),
'align' =>'left',
'index' => 'ktyp',
));
$this->addColumn('engine_code', array(
'header' => Mage::helper('makemodel')->__('Engine code'),
'align' =>'left',
'index' => 'engine_code',
));
return parent::_prepareColumns();
}
public function getRowUrl($row)
{
return $this->getUrl('*/*/edit', array('id' => $row->getId()));
}
public function getGridUrl()
{
return $this->getUrl('*/*/grid', array('_current'=>true));
}
}
C:\xampp2\htdocs\testmagento\app\code\local\Sigmasolve\Makemodel\Model\Makemodel.php
<?php
class Sigmasolve_Makemodel_Model_Makemodel extends Mage_Core_Model_Abstract
{
public function _construct()
{
parent::_construct();
$this->_init('makemodel/makemodel');
}
public function getManufacturers() {
$manufacturersArray = array();
$ManufacturerCollection = Mage::getModel('makemodel/makemodel')->getCollection();
$ManufacturerCollection->getSelect()->group('manufacturer');
$ManufacturerCollection->setOrder('manufacturer', 'ASC');
foreach($ManufacturerCollection->getData() as $manufacturer){
$manufacturersArray[$manufacturer['id']] = $manufacturer['manufacturer'];
}
return $manufacturersArray;
}
/* public function getModels() {
$modelsArray = array();
foreach($this->getCollection() as $model){
$modelsArray[$model->getId()] = $model->getModel();
}
return $modelsArray; // All Commented Code For My Refference......
} */
public function getModels() {
$modelsArray = array();
$ModelCollection = Mage::getModel('makemodel/makemodel')->getCollection();
$ModelCollection->getSelect()->group('model');
$ModelCollection->setOrder('model', 'ASC');
foreach($ModelCollection->getData() as $model){
$modelsArray[$model['id']] = $model['model'];
}
return $modelsArray;
}
public function getCapacitys() {
$capacitysArray = array();
$CapacityCollection = Mage::getModel('makemodel/makemodel')->getCollection();
$CapacityCollection->getSelect()->group('capacity');
$CapacityCollection->setOrder('capacity', 'ASC');
foreach($CapacityCollection->getData() as $capacity){
$capacitysArray[$capacity['id']] = $capacity['capacity'];
}
return $capacitysArray;
}
public function getYearFroms() {
$yearfromsArray = array();
foreach($this->getCollection() as $yearfrom){
$yearfromsArray[$yearfrom->getId()] = $yearfrom->getYearFrom();
}
return $yearfromsArray;
}
public function getYearTos() {
$yeartosArray = array();
foreach($this->getCollection() as $yearto){
$yeartosArray[$yearto->getId()] = $yearto->getYearTo();
}
return $yeartosArray;
}
public function getFuels() {
$fuelsArray = array();
$FuelCollection = Mage::getModel('makemodel/makemodel')->getCollection();
$FuelCollection->getSelect()->group('fuel');
$FuelCollection->setOrder('fuel', 'ASC');
foreach($FuelCollection->getData() as $fuel){
$fuelsArray[$fuel['id']] = $fuel['fuel'];
}
return $fuelsArray;
}
}
Soluzione
I think here is the problem :
$this->addColumn('manufacturer', array(
'header' => Mage::helper('makemodel')->__('Manufacturer'),
'align' =>'left',
'index' =>'manufacturer',
'type' =>'options',
'options'=> Mage::getModel('makemodel/makemodel')->getManufacturers()
));
In above code you have mention index=>manufacturer
while in below code you are passing id as a index
public function getManufacturers() {
$manufacturersArray = array();
$ManufacturerCollection = Mage::getModel('makemodel/makemodel')->getCollection();
$ManufacturerCollection->getSelect()->group('manufacturer');
$ManufacturerCollection->setOrder('manufacturer', 'ASC');
foreach($ManufacturerCollection->getData() as $manufacturer){
$manufacturersArray[$manufacturer['id']] = $manufacturer['manufacturer'];
}
return $manufacturersArray;
}
So just replace to
$manufacturersArray[$manufacturer['id']] = $manufacturer['manufacturer'];
from
$manufacturersArray[$manufacturer['manufacturer']] = $manufacturer['manufacturer'];
I hope this will work.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange