Archive for November 2017

Add a custom action to grid

1. In your Grid.php action do the following   <?php …   $this->addColumn(’action’, array( ‘header’ => Mage::helper(’promolink’)->__(’Action’), ‘width’ => ’50px’, ‘type’ => ‘action’, ‘getter’ => ‘getId’, ‘actions’ => array( array( ‘caption’ => Mage::helper(’promolink’)->__(’Delete’), ‘url’ => array(’base’ => ‘*/*/delete’), ‘field’ => ‘id’ ) ), ‘filter’ => false, ‘renderer’ => ‘Vendorname_Modulename_Block_Adminhtml_Template_Grid_Renderer_Youraction’, ‘sortable’ => false, ‘index’ => ‘id’, […]

Passing post data back to form

1. Add the following to the file: Vendorname/Modulename/controllers/Adminhtml/Modulename/IndexController.php   <?php … public function saveAction() { if ( $this->getRequest()->getPost() ) { try { $postData = $this->getRequest()->getPost();   } catch (Exception $e) { Mage::getSingleton(’adminhtml/session’)->addError($e->getMessage()); Mage::getSingleton(’adminhtml/session’)->setFormData($postData);   $this->_redirect(’*/*/edit’); return; } … 2. Add this parts of code to IndexController.php as well <?php   … public function editAction() { […]

Add a sql like to filter column in grid

  <?php   protected function _addColumnFilterToCollection($column) { if ($column->getId() == ‘name’) { $this->getCollection()->addFieldToFilter($column->getId(), array(’like’ => $column->getFilter()->getValue() . ‘%’)); … $this->getCollection()->addFieldToFilter($column->geId(), $column->getFilter()->getCondition()); … $this->getCollection()->addFieldToFilter(’name’, array(’like’ => ‘%’ . $name . ‘%’)); else { parent::_addColumnFilterToCollection($column); }   return $this; }

Display grid in admin form using custom field type

Step 1. Create new field type. Add the file app/local/code/Vendorname/Modulename/Block/Adminhtml/Modulename/Edit/Form/Renderer/Fieldset/Productgrid.php   <?php   class Vendorname_Modulename_Block_Adminhtml_Modulename_Edit_Form_Renderer_Fieldset_Productgrid extends Varien_Data_Form_Element_Abstract { protected $_element;   public function getElementHtml() { return Mage::helper(’core’)->getLayout()->createBlock(’modulename/adminhtml_modulename_edit_form_renderer_fieldset_product_grid’)->toHtml(); } } Step 2. Add Grid to app/local/code/Vendorname/Modulename/Block/Adminhtml/Modulename/Edit/Form/Renderer/Fieldset/Product/Grid.php   <?php   class Vendorname_Modulename_Block_Adminhtml_Modulename_Edit_Form_Renderer_Fieldset_Product_Grid extends Mage_Adminhtml_Block_Widget_Grid {   public function __construct() { parent::__construct(); $this->setId(’productsId’); $this->setDefaultSort(’id’); $this->setUseAjax(true); }   protected […]

Adminhtml Form Field custom validation function via Prototype Js

Step 1. In your Form element add custom validate class   <?php   class Vendorname_Modulename_Block_Adminhtml_Modulename_Edit_Form extends Mage_Adminhtml_Block_Widget_Form {   protected function _prepareForm() { … $fieldset->addField(’custom_id’, ‘text’, array( ‘label’ => Mage::helper(’modulename’)->__(’Custom ID’), ‘class’ => ‘validate-custom-id’, ‘required’ => true, ‘name’ => ‘custom_id’ )); … Step 2: Add the following to your extension’s app/code/local/Vendorname/Modulename/Block/Adminhtml/Modulename/Edit.php form block Edit file. […]