Magento管理网格日期格式

时间:2014-03-18 19:55:19

标签: php magento magento-1.8

我希望在编辑/添加项目时能够在我的一个管理网格中设置日期/日历选择器。如何编写日历样式的格式?

我尝试将“格式”参数更改为“日期”但没有成功。

我将包含代码和快照以便澄清。一些代码来自现有项目,一些来自教程。我无论如何都不想成为这个代码的主人,所以欢迎任何建议。

PHP 5.4.25 Magento 1.8.1.0

我的索引上的搜索部分似乎正在运行:

grid index http://i62.tinypic.com/34dszsw.png

但我想将“关闭日期”的文本字段更改为日历选择器:

grid edit http://i60.tinypic.com/2mnky78.png grid add http://i58.tinypic.com/av0z15.png

这是我的代码../ Party / Block / Adminhtml / Party / Edit / Tab / Form.php:

class Foobar_Party_Block_Adminhtml_Party_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form {

protected function _prepareForm() {

    $form = new Varien_Data_Form();
    $this->setForm($form);
    $fieldset = $form->addFieldset("party_form", array("legend" => Mage::helper("party")->__("Party information")));

    /* Code for other columns */

    $fieldset->addField("party_close_date", "date", array(
        "label" => Mage::helper("party")->__("Close Date"),

        'format' => 'date',
        'name' =>   'party_close_date',
        'time' =>   'true',
    ));

    if (Mage::getSingleton("adminhtml/session")->getPartyData()) {
        $form->setValues(Mage::getSingleton("adminhtml/session")->getPartyData());
        Mage::getSingleton("adminhtml/session")->setPartyData(null);
    } elseif (Mage::registry("party_data")) {
        $form->setValues(Mage::registry("party_data")->getData());
    }
    return parent::_prepareForm();
}

}

这是我的代码../ Party / Block / Adminhtml / Party / Edit / Form.php:

<?php

class Foobar_Party_Block_Adminhtml_Party_Edit_Form extends Mage_Adminhtml_Block_Widget_Form {

protected function _prepareForm() {
    $form = new Varien_Data_Form(array(
        "id" => "edit_form",
        "action" => $this->getUrl("*/*/save", array("id" => $this->getRequest()->getParam("id"))),
        "method" => "post",
        "enctype" => "multipart/form-data",
            )
    );
    $form->setUseContainer(true);
    $this->setForm($form);
    return parent::_prepareForm();
}

}

这是我的代码../ Party / Block / Adminhtml / Party / Grid.php:

<?php

class Foobar_Party_Block_Adminhtml_Party_Grid extends Mage_Adminhtml_Block_Widget_Grid {

public function __construct() {
    parent::__construct();
    $this->setId("partyGrid");
    $this->setDefaultDir("ASC");
    $this->setSaveParametersInSession(true);
}

protected function _prepareCollection() {
    $collection = Mage::getModel("party/party")->getCollection();
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

protected function _prepareColumns() {

    /* Code for other columns */

    $this->addColumn("party_close_date", array(
        "header" => Mage::helper("party")->__("Close Date"),
        "type" =>   "datetime", 
        "index" =>  "party_close_data",
    ));

    $this->addExportType('*/*/exportCsv', Mage::helper('party')->__('CSV'));
    $this->addExportType('*/*/exportExcel', Mage::helper('party')->__('Excel'));

    return parent::_prepareColumns();
}

public function getRowUrl($row) {
    return $this->getUrl("*/*/edit", array("id" => $row->getId()));
}

}

1 个答案:

答案 0 :(得分:3)

我找到了问题的答案!

我在我的..Edit / Tab / Form.php文件中添加了3行。

我改变了这些线......

        $fieldset->addField("party_close_date", "date", array(
        "label" => Mage::helper("party")->__("Close Date"),
        'format' => Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT),
        'name' =>   'party_close_date',
        'time' =>   'true',
    ));

......对此:

        $fieldset->addField("party_close_date", "date", array(
        "label" => Mage::helper("party")->__("Close Date"),
        'format' => Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT), 
        'after_element_html' => '<small>Comments</small>',
        'tabindex' => 1,
        'image' => $this->getSkinUrl('images/grid-cal.gif'),
        'name' =>   'party_close_date',
        'time' =>   'true',
    ));

有一个方便的教程here帮我解决了负担。