magento后台如何实现日期+时间同时的选择,如下图所示,作图为默认的样式,右图为修改后的功能。
两者可以共存,共存,共存!(之前问过别人,说不能)
1,修改根目录下lib/Varien/Data/Form/Element/Datetime.php
默认是下面这样的:
class Varien_Data_Form_Element_Datetime extends Varien_Data_Form_Element_Date { }
修改成:
class Varien_Data_Form_Element_Datetime extends Varien_Data_Form_Element_Date { /** * @var Zend_Date */ protected $_value; public function __construct($attributes=array()) { parent::__construct($attributes); $this->setType('text'); $this->setExtType('textfield'); if (isset($attributes['value'])) { $this->setValue($attributes['value']); } } /** * If script executes on x64 system, converts large * numeric values to timestamp limit */ protected function _toTimestamp($value) { $value = (int)$value; if ($value > 3155760000) { $value = 0; } return $value; } /** * Set date value * If Zend_Date instance is provided instead of value, other params will be ignored. * Format and locale must be compatible with Zend_Date * * @param mixed $value * @param string $format * @param string $locale * @return Varien_Data_Form_Element_Date */ public function setValue($value, $format = null, $locale = null) { if (empty($value)) { $this->_value = ''; return $this; } if ($value instanceof Zend_Date) { $this->_value = $value; return $this; } if (preg_match('/^[0-9]+$/', $value)) { $this->_value = new Zend_Date($this->_toTimestamp($value)); //$this->_value = new Zend_Date((int)value); return $this; } // last check, if input format was set if (null === $format) { $format = Varien_Date::DATETIME_INTERNAL_FORMAT; if ($this->getInputFormat()) { $format = $this->getInputFormat(); } } // last check, if locale was set if (null === $locale) { if (!$locale = $this->getLocale()) { $locale = null; } } try { $this->_value = new Zend_Date($value, $format, $locale); } catch (Exception $e) { $this->_value = ''; } return $this; } /** * Get date value as string. * Format can be specified, or it will be taken from $this->getFormat() * * @param string $format (compatible with Zend_Date) * @return string */ public function getValue($format = null) { if (empty($this->_value)) { return ''; } if (null === $format) { $format = $this->getFormat(); } return $this->_value->toString($format); } /** * Get value instance, if any * * @return Zend_Date */ public function getValueInstance() { if (empty($this->_value)) { return null; } return $this->_value; } /** * Output the input field and assign calendar instance to it. * In order to output the date: * - the value must be instantiated (Zend_Date) * - output format must be set (compatible with Zend_Date) * * @return string */ public function getElementHtml() { $this->addClass('input-text'); $html = sprintf( '<input name="%s" id="%s" value="%s" %s style="width:110px !important;" />' .' <img src="%s" alt="" class="v-middle" id="%s_trig" title="%s" style="%s" />', $this->getName(), $this->getHtmlId(), $this->_escape($this->getValue()), $this->serialize($this->getHtmlAttributes()), $this->getImage(), $this->getHtmlId(), 'Select Date', ($this->getDisabled() ? 'display:none;' : '') ); $outputFormat = $this->getFormat(); if (empty($outputFormat)) { throw new Exception('Output format is not specified. Please, specify "format" key in constructor, or set it using setFormat().'); } $displayFormat = Varien_Date::convertZendToStrFtime($outputFormat, true, (bool)$this->getTime()); $html .= sprintf(' <script type="text/javascript"> //<![CDATA[ Calendar.setup({ inputField: "%s", ifFormat: "%s", showsTime: %s, button: "%s_trig", align: "Bl", singleClick : true }); //]> </script>', $this->getHtmlId(), $displayFormat, $this->getTime() ? 'true' : 'false', $this->getHtmlId() ); $html .= $this->getAfterElementHtml(); return $html; } public function getTime() { return true; } public function getFormat() { return 'yyyy-MM-dd HH:mm:ss'; } }
2,修改根目录下lib/Varien/Data/Form/Element/Date.php,在类Varien_Data_Form_Element_Date最后添加如下代码
public function getTime() { return true; } public function getFormat() { return 'yyyy-MM-dd'; }
运用方法:
//datetime $fieldset->addField('end_time', 'datetime', array( 'name' => 'end_time', 'label' => Mage::helper('activity')->__('过期时间'), 'class' => 'required-entry', 'required' => true, 'image' => $this->getSkinUrl('images/grid-cal.gif'), 'disabled' => false, 'readonly' => true, 'style' => 'width:200px;', 'after_element_html' => '<div class="hint"><p class="note">' . $this->__('说明:过期时间需要至少晚于发送时间30分钟,时间格式如:2015-11-06 14:38:20') . '</p></div>' )); //date $fieldset->addField('end_time', 'date', array( 'name' => 'end_time', 'label' => Mage::helper('activity')->__('过期时间'), 'class' => 'required-entry', 'required' => true, 'image' => $this->getSkinUrl('images/grid-cal.gif'), 'disabled' => false, 'readonly' => true, 'style' => 'width:200px;', ));
这个是笔者转载,前面两个步骤验证过,是有效的,可是后面的这个调用方法一直不是很明白到底是怎么运用的。如果有知道怎么调用的可以联系笔者啊。
未经允许不得转载:哈勃私语 » magento后台日期时间控件显示时间
本文共4240个字 创建时间:2018年11月19日18:37