Magento编辑表单字段集 - 获取选择下拉列表到标签的值

时间:2014-10-10 12:05:05

标签: php magento

我正在为网格行编辑屏幕。这就是我到目前为止的形式:

<?php

class Intellibi_Integration_Block_Adminhtml_Manageasendiapickinglists_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm()
    {
        $form = new Varien_Data_Form();
        $this->setForm($form);

        $fieldset = $form->addFieldset('integration_form', array(
            'legend' => Mage::helper('integration')->__('Asendia Pick Information')
        ));

        $fieldset->addField('order_number', 'label', array(
            'label' => Mage::helper('integration')->__('Order Number'),
            'name' => 'order_number'
        ));

        // snipped

        $fieldset->addField('pick_status', 'select', array(
            'required' => false,
            'class' => 'required-entry',
            'label' => Mage::helper('integration')->__('Pick Status'),
            'name' => 'pick_status',
            'values' => Mage::getSingleton('ibi/asendiapickstatus')->getOptionArray(),
            'readonly' => 'readonly'
        ));

        // snipped

        return parent::_prepareForm();
    }
}

这会在admin后端产生以下输出:

enter image description here

我想要做的是将pick_status列从select更改为label。当我这样做时,它不是显示状态值“New”,而是显示数组索引,如下所示:

enter image description here

asendiapickstatus的我的选项数组在我的模型中定义如下:

class Intellibi_Integration_Model_Asendiapickstatus extends Varien_Object
{
    const PICK_STATUS_NEW       = 1;
    const PICK_STATUS_SENT      = 2;
    const PICK_STATUS_SHIPPED   = 3;

    static public function getOptionArray()
    {
        return array(
            self::PICK_STATUS_NEW       => Mage::helper('integration')->__('New'),
            self::PICK_STATUS_SENT      => Mage::helper('integration')->__('Sent'),
            self::PICK_STATUS_SHIPPED   => Mage::helper('integration')->__('Shipped')
        );
    }
}

所以我的问题是;在编辑表单字段集构建器上,如何显示下拉字段“pick_status”值,而不是它当前的索引?因此输出将显示“New”而不是“1”,如上所示。我需要自定义渲染器吗?

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题(使用自定义表单呈现元素):

添加了自定义字段集类型

$fieldset->addType('pickstatus', 'Intellibi_Integration_Block_Adminhtml_Manageasendiapickinglists_Edit_Tab_Form_Renderer_Fieldset_Pickstatus');

像这样使用字段集

$fieldset->addField('pick_status', 'pickstatus', array(
    'label' => Mage::helper('integration')->__('Pick Status'),
    'name' => 'pick_status',
));

对此内容进行编码

class Intellibi_Integration_Block_Adminhtml_Manageasendiapickinglists_Edit_Tab_Form_Renderer_Fieldset_Pickstatus extends Varien_Data_Form_Element_Abstract
{
    protected $_element;

    public function getElementHtml()
    {
        // Load Pick Status
        $pick_status = (int)$this->getValue();
        $pick_status_list = Mage::getSingleton('ibi/asendiapickstatus')->getOptionArray();

        // Finish
        return array_key_exists($pick_status, $pick_status_list) ? $pick_status_list[$pick_status] : 'Unknown';
    }
}

它会像这样呈现

enter image description here