Can anyone point me in the right direction on how to save the editable columns within the Magento Grid?
I have a column called ‘sort_order’ of which has ‘editable’ => true , it adds a field to edit, but how do I make it save the value to the row?
Thank you in advance for your help.
here is my grid.php code,
class ***_Imagegallery_Block_Manage_Imagegallery_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
public function __construct()
{
parent::__construct();
$this->setId('imagegalleryGrid');
$this->setDefaultSort('sort_order');
$this->setDefaultDir('ASC');
$this->setSaveParametersInSession(true);
}
protected function _getStore()
{
$storeId = (int) $this->getRequest()->getParam('store', 0);
return Mage::app()->getStore($storeId);
}
protected function _prepareCollection()
{
$collection = Mage::getModel('imagegallery/imagegallery')->getCollection();
$store = $this->_getStore();
if ($store->getId()) {
$collection->addStoreFilter($store);
}
$filter = $this->getParam('filter');
$filter_data = Mage::helper('adminhtml')->prepareFilterString($filter);
if(!isset($filter_data['status']))
{
$collection->addFieldToFilter('status', array('eq' => 1));
}
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
$this->addColumn('sort_order', array(
'header' => Mage::helper('imagegallery')->__('Sort'),
'align' =>'right',
'width' => '50px',
'index' => 'sort_order',
'type' => 'number',
'width' => '1',
'sortable' => true,
'editable' => true
));
$this->addColumn('post_id', array(
'header' => Mage::helper('imagegallery')->__('ID'),
'align' =>'right',
'width' => '50px',
'index' => 'post_id',
));
$this->addColumn('nfile', array(
'header' => Mage::helper('cms')->__('Image File'),
'align' => 'left',
'index' => 'nfile',
'type' => 'image',
'width' => '100',
));
$this->addColumn('title', array(
'header' => Mage::helper('imagegallery')->__('Title'),
'align' =>'left',
'index' => 'title',
));
/*$this->addColumn('identifier', array(
'header' => Mage::helper('imagegallery')->__('Identifier'),
'align' => 'left',
'index' => 'identifier',
));
$this->addColumn('user', array(
'header' => Mage::helper('imagegallery')->__('Poster'),
'width' => '150px',
'index' => 'user',
));*/
$this->addColumn('created_time', array(
'header' => Mage::helper('imagegallery')->__('Created'),
'align' => 'left',
'width' => '120px',
'type' => 'date',
'default' => '--',
'index' => 'created_time',
));
$this->addColumn('update_time', array(
'header' => Mage::helper('imagegallery')->__('Updated'),
'align' => 'left',
'width' => '120px',
'type' => 'date',
'default' => '--',
'index' => 'update_time',
));
$this->addColumn('status', array(
'header' => Mage::helper('imagegallery')->__('Status'),
'align' => 'left',
'width' => '80px',
'index' => 'status',
'type' => 'options',
'options' => array(
1 => Mage::helper('imagegallery')->__('Enabled'),
2 => Mage::helper('imagegallery')->__('Disabled'),
3 => Mage::helper('imagegallery')->__('Hidden'),
),
));
$this->addColumn('action',
array(
'header' => Mage::helper('imagegallery')->__('Action'),
'width' => '100',
'type' => 'action',
'getter' => 'getId',
'actions' => array(
array(
'caption' => Mage::helper('imagegallery')->__('Edit'),
'url' => array('base'=> '*/*/edit'),
'field' => 'id'
)
),
'filter' => false,
'sortable' => false,
'index' => 'stores',
'is_system' => true,
));
return parent::_prepareColumns();
}
protected function _prepareMassaction()
{
$this->setMassactionIdField('post_id');
$this->getMassactionBlock()->setFormFieldName('imagegallery');
$this->getMassactionBlock()->addItem('delete', array(
'label' => Mage::helper('imagegallery')->__('Delete'),
'url' => $this->getUrl('*/*/massDelete'),
'confirm' => Mage::helper('imagegallery')->__('Are you sure?')
));
$statuses = Mage::getSingleton('imagegallery/status')->getOptionArray();
array_unshift($statuses, array('label'=>'', 'value'=>''));
$this->getMassactionBlock()->addItem('status', array(
'label'=> Mage::helper('imagegallery')->__('Change status'),
'url' => $this->getUrl('*/*/massStatus', array('_current'=>true)),
'additional' => array(
'visibility' => array(
'name' => 'status',
'type' => 'select',
'class' => 'required-entry',
'label' => Mage::helper('imagegallery')->__('Status'),
'values' => $statuses
)
)
));
return $this;
}
public function getRowUrl($row)
{
return $this->getUrl('*/*/edit', array('id' => $row->getId()));
}
}
This is actually quite complex; ideally your grid needs to be part of a Container which instantiates a Form, and is called via a Tab section. The save buttons are usually some part of a custom phtml template that is invoked along with the layout block that is called via the controller action in your Adminhtml.
This form.phtml can be very simple but usually contains some javascript that formats the save url (see below) and gathers the submission data.
Form.php
The constructor:
And the _prepareLayout method:
Your save button is what directs the submission to the correct controller. In this example above they use getSaveUrl as a method of these block classes. You can also hard code this or use
$this->getUrl('*/*/save')as you probably do elsewhere. Clicking this save button will serialize the form:form.phtml
The default file simply has:
An extended form.phtml file that includes editible post data does something like this:
I haven’t tested every part of the above code, but the theory is solid, and it’s what I do in these situations.
Cheers.