天天看点

Magento Adminhtml Form

开发magento后台的时候经常用到表单(varien_data_form),下面是所有表单元素的添加方法:

非必填 注释掉

Magento Adminhtml Form

'class'     => 'required-entry',  

'required'  => true,  

1.input type='text'

Magento Adminhtml Form

$fieldset->addfield('title', 'text', array(  

    'label'     => 'title3',  

    'class'     => 'required-entry',  

    'required'  => true,  

    'name'      => 'title',  

    'onclick' => 'alert("on click");',  

    'onchange' => 'alert("on change");',  

    'style'   => 'border:10px',  

    'value'  => 'hello !!',  

    'disabled' => false,  

    'readonly' => true,  

    'after_element_html' => 'comments',  

    'tabindex' => 1  

));  

 2.time时间类型

Magento Adminhtml Form

$fieldset->addfield('time', 'time', array(  

    'label'     => 'time',  

    'onclick' => '',  

    'onchange' => '',  

    'value'  => '12,04,15',  

    'readonly' => false,  

    'after_element_html' => '<script src="js/extjs/ext-tree.js" type="text/javascript">',  

3.textarea

Magento Adminhtml Form

$fieldset->addfield('textarea', 'textarea', array(  

    'label'     => 'textarea',  

    'value'  => '<b><b/>',  

    'after_element_html' => '<small>comments</small>',  

4.input type='submit'

Magento Adminhtml Form

$fieldset->addfield('submit', 'submit', array(  

    'label'     => 'submit',  

    'value'  => 'submit',  

    'after_element_html' => '',    

5.select option

Magento Adminhtml Form

$fieldset->addfield('select', 'select', array(  

    'label' => '页面样式',  

    'class' => 'required-entry',  

    'required' => true,  

    'name' => 'skin',  

    'value' => '1',  

    'values' => array('-1' => '请选择', '1' => '红', '2' => '蓝'),  

    'after_element_html' => '',  

 6.带optgroup的select

Magento Adminhtml Form

$fieldset->addfield('select2', 'select', array(  

    'label' => 'select type2',  

    'name' => 'title',  

    'value' => '4',  

    'values' => array(  

        '-1' => 'please select..',  

        '1' => array(  

            'value' => array(array('value' => '2', 'label' => 'option2'), array  

            ('value' => '3', 'label' => 'option3')),  

            'label' => 'size'  

        ),  

        '2' => array(  

            'value' => array(array('value' => '4', 'label' => 'option4'), array  

            ('value' => '5', 'label' => 'option5')),  

            'label' => 'color'  

    ),  

 7.单选框input type='radio'

Magento Adminhtml Form

$fieldset->addfield('radio', 'radio', array(  

    'label' => 'radio',  

$fieldset->addfield('radio2', 'radios', array(  

    'label' => 'radios',  

    'value' => '2',  

        array('value' => '1', 'label' => 'radio1'),  

        array('value' => '2', 'label' => 'radio2'),  

        array('value' => '3', 'label' => 'radio3'),  

8.password元素 input type='password

Magento Adminhtml Form

$fieldset->addfield('password', 'password', array(  

    'label' => mage::helper('form')->__('password'),  

    'style' => '',  

    'value' => 'hello !!',  

$fieldset->addfield('obscure', 'obscure', array(  

    'label' => mage::helper('form')->__('obscure'),  

    'name' => 'obscure',  

    'value' => '123456789',  

9.note

Magento Adminhtml Form

$fieldset->addfield('note', 'note', array(  

    'text'     => mage::helper('form')->__('text text'),  

 10.multiselect复选框

Magento Adminhtml Form

$fieldset->addfield('multiselect2', 'multiselect', array(  

    'label' => mage::helper('form')->__('select type2'),  

    'onclick' => 'return false;',  

    'onchange' => 'return false;',  

        '-1' => array('label' => 'please select..', 'value' => '-1'),  

    'after_element_html' => ' ',  

11.multiline

Magento Adminhtml Form

$fieldset->addfield('multiline', 'multiline', array(  

    'label' => mage::helper('form')->__('multi line'),  

    'style' => 'border:10px',  

12.link 链接

Magento Adminhtml Form

$fieldset->addfield('link', 'link', array(  

    'label'     => mage::helper('form')->__('link'),  

    'style'   => '',  

    'href' => 'www.excellencemagentoblog.com',  

    'value'  => 'magento blog',  

    'after_element_html' => ''  

13.普通label

Magento Adminhtml Form

$fieldset->addfield('label', 'label', array(  

    'value' => mage::helper('form')->__('label text'),  

14.image upload图片上传

Magento Adminhtml Form

$fieldset->addfield('image', 'image', array(  

    'value' => 'http://www.test.com//images/logo.png',  

15.file upload文件上传

Magento Adminhtml Form

$fieldset->addfield('file', 'file', array(  

    'label'     => mage::helper('form')->__('upload'),  

    'value'  => 'uplaod',  

16.日期date

Magento Adminhtml Form

$fieldset->addfield('date_expires', 'date', array(  

    'name'   => 'date_expires',  

    'label'  => '过期时间',  

    'title'  => '过期时间',  

    'image'  => $this->getskinurl('images/grid-cal.gif'),  

    'input_format' => varien_date::date_internal_format,  

    'format'       => mage::app()->getlocale()->getdateformat(mage_core_model_locale::format_type_medium)  

17.checkbox

Magento Adminhtml Form

$fieldset->addfield('checkbox', 'checkbox', array(  

    'label' => mage::helper('form')->__('checkbox'),  

    'name' => 'checkbox',  

    'checked' => false,  

$fieldset->addfield('checkboxes', 'checkboxes', array(  

    'label' => mage::helper('form')->__('checkboxs'),  

        array('value' => '1', 'label' => 'checkbox1'),  

        array('value' => '2', 'label' => 'checkbox2'),  

        array('value' => '3', 'label' => 'checkbox3'),  

 添加文本编辑器   js部分

Magento Adminhtml Form

protected function _preparelayout() {  

    parent::_preparelayout();  

    if (mage::getsingleton('cms/wysiwyg_config')->isenabled()) {  

        $this->getlayout()->getblock('head')->setcanloadtinymce(true);  

    }  

}  

 form

Magento Adminhtml Form

$fieldset->addfield('content', 'editor', array(  

    'name'      => 'content',  

    'label'     => 'content',  

    'title'     => 'content',  

    'style'     => 'height:15em',  

    'config'    => mage::getsingleton('cms/wysiwyg_config')->getconfig(),  

    'wysiwyg'   => true,  

    'required'  => false,  

));