如何在magento的admin网格表格下拉列表中设置默认值?

时间:2013-12-20 11:47:54

标签: magento magento-1.7 adminhtml

我正在研究Magento 1.7版本。 我的代码如下: -

$categoryArray = Mage::getSingleton('lookbook/category')->getOptionArray();

    $catId = Mage::getSingleton('core/session')->getCatId();   //it has value 4

    $fieldset->addField('category_id', 'select', array(
        'label' => Mage::helper('lookbook')->__('Lookbook'),
        'name' => 'category_id[]',
        'values' => $categoryArray,            
        'value' => $catId,            
        'disabled' => true
    ));

如何在Magento的管理网格表格下拉列表中选择此值?

2 个答案:

答案 0 :(得分:3)

看看@ https://magento.stackexchange.com/questions/544/how-to-set-default-value-for-form-fields

protected function _prepareForm()
{
    $form_data = new Varien_Object();

    $form = new Varien_Data_Form();
    $this->setForm($form);
    ....

    if ( Mage::getSingleton('adminhtml/session')->getXyzData() )
    {
        $form_data = Mage::getSingleton('adminhtml/session')->getXyzData();
        Mage::getSingleton('adminhtml/session')->setXyzData(null);
    } 
    else if ( Mage::registry('xyz_data') ) {
        $form_data = Mage::registry('xyz_data');  
    }

    $catId = Mage::getSingleton('core/session')->getCatId();   // it has value 4

    if( empty($form_data->getData('category_id')) ){
        $form_data->setData('category_id', $catId);
    }

    $fieldset->addField('category_id', 'select', array(
      'label' => Mage::helper('lookbook')->__('Lookbook'),
      'name' => 'category_id[]',
      'values' => $categoryArray,            
     'disabled' => true
    ));


   ....

    $form->setValues($form_data);

    $this->setForm($form);

}

答案 1 :(得分:1)

在网格的__construct方法中添加此内容。

$this->_defaultFilter = array('category_id'=>Mage::getSingleton('core/session')->getCatId());