cancel
Showing results for 
Search instead for 
Did you mean: 

Magento Add date field in custom extension

Magento Add date field in custom extension

Hello, i have my ext and it work perfect, but now i need change something:

 

i need date field moving to 1st column, pls advice.

 

date block:

 

class Holiday_Block_Adminhtml_System_Config_Date extends Mage_Adminhtml_Block_System_Config_Form_Field
{
protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
{
$date = new Varien_Data_Form_Element_Date;
$format = 'MM/dd/yyyy';

$data = array(
'name' => $element->getName(),
'html_id' => $element->getId(),
'image' => $this->getSkinUrl('images/grid-cal.gif'),
);
$date->setData($data);
$date->setValue($element->getValue(), $format);
$date->setFormat($format);
$date->setClass($element->getFieldConfig()->validate->asArray());
$date->setForm($element->getForm());

return $date->getElementHtml();
}
}

 

Dynamic block:

 

class Mage_Adminhtml_Block_System_Config_Form_Field_Holiday extends Mage_Adminhtml_Block_System_Config_Form_Field_Array_Abstract
{
public function __construct()
{

$this->addColumn('fromdate', array(
'label' => Mage::helper('adminhtml')->__('1'),
// 'style' => 'width:180px',
// 'renderer' => 'holiday/adminhtml_system_config_date',
));

$this->addColumn('todate', array(
'label' => Mage::helper('adminhtml')->__('2'),
'style' => 'width:180px',
));
$this->addColumn('notifications1', array(
'label' => Mage::helper('adminhtml')->__('3'),
'style' => 'width:180px',
));
$this->_addAfter = false;
$this->_addButtonLabel = Mage::helper('adminhtml')->__('Add Notification');

parent::__construct();

}

4 REPLIES

Re: Magento Add date field in custom extension

In order to have a date type field, in the dynamic block you can add the property 'renderer' to map 'core/html_date'. Or you can build your own renderer with custom properties that will override default ones.

 

ex.

 

<?php 

class Namespace_ModuleName_Block_Adminhtml_Renderer_Date extends Mage_Core_Block_Html_Date
{
    //.... override methods like title, styles, name, ... 
}


and in dynamic block:

$this->addColumn(
    'my_date',
    array(
        'label' => 'label needs translation support',
        'renderer' => 'namespace_mymodule/adminhtml_renderer_date'
    )
);



Re: Magento Add date field in custom extension

thanks, but in some reason nothing change when i add renderer, in that case i posted my question coz i was do it before

 

$this->addColumn('fromdate', array(
'label' => Mage::helper('adminhtml')->__('Active From'),
'style' => 'width:180px',
'renderer' => 'core/html_date',
));

 

have input field instead date field, pls advice

Re: Magento Add date field in custom extension

You can achieve it by extending 'core/html_date' and overriding the method _toHtml() for the renderer. Or define the column without a renderer, add class 'datepicker' and try to append the javascript part for date picker along within the column (at the end the date field type is an extended version for text field type with an additional javascript for datepicker)

 

I will write a full solution tomorrow, if someone else doen't manage to help you until then.

Re: Magento Add date field in custom extension

thx, i'll be waiting.