如何在我的自定义模块中添加添加按钮

时间:2013-12-27 10:07:53

标签: magento-1.7 magento

我有一个模块,用于通过管理面板添加横幅图片。我已成功创建模块。我的模块编辑页面有两个选项卡。一个用于添加横幅的一般信息,另一个用于添加图像。一般信息选项卡现在正常工作。

我最初需要在第二个标签中添加一个“添加横幅图片”按钮。单击它时,它应该加载文件类型按钮。我们可以使用此按钮加载图像。我们可以在任意次使用“添加横幅图像”来加载图像。这与产品的添加自定义选项类似。我在这里需要完全相同的功能。

我搜索了很多。但无法找到如何在我的第二个标签中添加“添加横幅图片”按钮。请帮我解决这个问题。让我知道如何实现这一功能。抱歉我的英语不好。谢谢

1 个答案:

答案 0 :(得分:0)

我创建了一个类似的功能,我可以使用下面的代码在我的自定义模块中添加邮政编码范围。

在我添加的Module_name/Block/Adminhtml/Regions/Edit/Tab/Form.php中。

$fieldset->addField('postcodes', 'text', array(
                'name'=>'postcodes',
                'class'=>'requried-entry'
        ));

        $form->getElement('postcodes')->setRenderer(
            $this->getLayout()->createBlock('zones/adminhtml_regions_edit_tab_postcodes')
        );

这将使用渲染器adminhtml_regions_edit_tab_postcodes在我的标签中添加邮政编码字段 在我的Module_name/Block/Adminhtml/Regions/Edit/Tab/Postcodes.php中,我添加了带代码的按钮。

class Ripples_Zones_Block_Adminhtml_Regions_Edit_Tab_Postcodes
        extends Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Price_Group_Abstract
    {
        public function __construct()
        {
            $this->setTemplate('zones/postcodes.phtml');
        }

        protected function _prepareLayout()
        {
            $button = $this->getLayout()->createBlock('adminhtml/widget_button')
                ->setData(array(
                    'label' => Mage::helper('zones')->__('Add Postcode Range'),
                    'onclick' => 'return addPostCodeRange.addItem()',
                    'class' => 'add'
                ));
            $button->setName('add_postcode_range_button');

            $this->setChild('add_button', $button);
            return parent::_prepareLayout();
        }

    }

这将在我的标签上添加一个按钮,其中包含onclick函数addPostCodeRange.addItem()

之后我在design/adminhtml/default/default/template/zones/postcodes.phtml

中添加了一个phtml文件
<?php $_htmlId      = $this->getElement()->getHtmlId() ?>
<?php $_htmlName = 'postcodes';?>
<tr>
    <td class="label">Postcode Range</td>
    <td colspan="10" class="grid">
    <table cellspacing="0" class="data border" id="postcodes_table">
        <col width="50" />
        <col width="50" />
        <col width="1" />
        <thead>
            <tr class="headings">
                <th><?php echo Mage::helper('zones')->__('From') ?></th>
                <th><?php echo Mage::helper('zones')->__('To')  ?></th>
                <th class="last"><?php echo Mage::helper('catalog')->__('Action') ?></th>
            </tr>
        </thead>
        <tbody id="<?php echo $_htmlId ?>_container"></tbody>
        <tfoot>
            <tr>
                <td colspan="3" class="a-right"><?php echo $this->getAddButtonHtml() ?></td>
            </tr>
        </tfoot>
    </table>

<script type="text/javascript">
//<![CDATA[
var tierPriceRowTemplate = '<tr>'
    + '<td><input type="text" value="{{from}}" class="custgroup required-entry" name="<?php echo $_htmlName ?>[{{index}}][from]" id="postcodes_row_{{index}}_from" />'
    + '</td>'
    + '<td><input type="text" value="{{to}}" class="custgroup required-entry" name="<?php echo $_htmlName ?>[{{index}}][to]" id="postcodes_row_{{index}}_to" />'
    + '</td>'
    + '<td class="last"><input type="hidden" name="<?php echo $_htmlName ?>[{{index}}][delete]" class="delete" value="" id="postcodes_row_{{index}}_delete" />'
    + '<button title="<?php echo Mage::helper('zones')->__("Delete Range") ?>" type="button" class="scalable delete icon-btn delete-product-option" id="postcodes_row_{{index}}_delete_button" onclick="return addPostCodeRange.deleteItem(event);">'
    + '<span><span><span><?php echo Mage::helper('zones')->__("Delete") ?></span></span></span></button></td>'
    + '</tr>';

var addPostCodeRange = {
    template: new Template(tierPriceRowTemplate, new RegExp('(^|.|\\r|\\n)({{\\s*(\\w+)\\s*}})', "")),
    itemsCount: 0,
    addItem : function () {

        var data = {
            from: '',
            to: '',
            readOnly: false,
            index: this.itemsCount++
        };

        if(arguments.length >= 2) {
            data.from = arguments[0];
            data.to      = arguments[1];
        }
        if (arguments.length == 3) {
            data.readOnly = arguments[2];
        }

        Element.insert($('<?php echo $_htmlId ?>_container'), {
            bottom : this.template.evaluate(data)
        });

        $('postcodes_row_' + data.index + '_from').value = data.from;
        $('postcodes_row_' + data.index + '_to').value    = data.to;

        if (data.readOnly == '1') {
            ['from', 'to', 'delete'].each(function(idx){
                $('postcodes_row_'+data.index+'_'+idx).disabled = true;
            });
            $('postcodes_row_'+data.index+'_delete_button').hide();
        }
    },
    disableElement: function(el) {
        el.disabled = true;
        el.addClassName('disabled');
    },
    deleteItem: function(event) {
        var tr = Event.findElement(event, 'tr');
        if (tr) {
            Element.select(tr, '.delete').each(function(elem){elem.value='1'});
            Element.hide(tr);
            Element.addClassName(tr, 'no-display template');
        }
        return false;
    }
};
<?php $collection = Mage::getModel('zones/regions')->getCollection()->addFieldToFilter('regions_id',array('eq' => $this->getRequest()->getParam('id'))); ?>
<?php foreach ($collection as $_item): ?>
<?php $postcodes = unserialize($_item['postcodes']); ?>
<?php foreach ($postcodes as $postcode): ?>
addPostCodeRange.addItem( '<?php echo $postcode['from'] ?>', '<?php echo $postcode['to'] ?>');
<?php endforeach; ?>
<?php  endforeach; ?>
//]]>
</script>
</td></tr>

此文件包含该功能。我在函数调用中添加了两个文本框。您可以在此功能中添加文件字段。

希望这有帮助。

相关问题