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:
public function __construct()
{
parent::__construct();
$this->setTemplate('your/adminhtml/edit/form.phtml');
}
And the _prepareLayout method:
protected function _prepareLayout()
{
// Save button
$this->setChild('save_button',
$this->getLayout()->createBlock('adminhtml/widget_button')
->setData(array(
'label' => Mage::helper('catalog')->__('Save Category'),
'onclick' => "formSubmit('" . $this->getSaveUrl() . "', true)",
'class' => 'save'
))
);
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:
<div class="entry-edit">
<?php echo $this->getFormHtml();?>
</div>
<?php echo $this->getChildHtml('form_after');?>
An extended form.phtml file that includes editible post data does something like this:
<div class="entry-edit">
<?php echo $this->getFormHtml();?>
</div>
<?php echo $this->getChildHtml('form_after');?>
<script>
function formSubmit(url){
var elements = $('[yourformid]').select('input');
var serialized = Form.serializeElements(elements, true);
window.location(url + '?' + serilized.[element_name].join(','));
}
</script>
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.
Whilst you are correct that this attribute is available as a property for addColumn, I believe FlorinelChis is actually correct in as much as a form is required. The only example I know of in Magento core code is the product ordering when editing a category. If you look into it you will see that the Grid in question is actually added as a tab to Edit form of the category. The persistence to the database is done as part of the standard edit form saving (in the saveAction
of Mage_Adminhtml_Catalog_CategoryController
the products are stored using the Varien_Object
magic setter setPostedProducts
, then when the action calls save on the category model the _afterSave
function on the category model triggers _saveCategoryProducts
, which persists the information to the database).
There is (unfortunately) no automatic support for persisting the values directly from a standard grid page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With