所属分类: 深入理解Magento Magento后台表单添加层级表格

第一步:

在后台表单中添加如下代码

  $officehours_field = $fieldset->addField('rules', 'text', array(
            'name'      => 'rules',
            'label'     => Mage::helper('ditui')->__('规则区间'),
            'required'  => false,
             ));
  
           $office_hours = $form->getElement('rules');
  
           $office_hours->setRenderer(
                $this->getLayout()->createBlock('ditui/adminhtml_rebate_edit_renderer_rules')
            );

简单来描述一下以上代码的意思

在后台表单中新增一个namerules 表单元素,接着用

ditui/adminhtml_rebate_edit_renderer_rules 类对这个表单元素的输出进行重写

第二步:创建rules文件

内容如下

class Ares_Ditui_Block_Adminhtml_Rebate_Edit_Renderer_Rules extends Mage_Adminhtml_Block_Widget
 implements Varien_Data_Form_Element_Renderer_Interface
{
     /**
     * Initialize block
     */
    public function __construct()
    {
        $this->setTemplate('ditui/rebate/edit/renderer/rules.phtml');
    }
     
    /**
     * Render HTML
     *
     * @param Varien_Data_Form_Element_Abstract $element
     * @return string
     */
    public function render(Varien_Data_Form_Element_Abstract $element)
    {
        $this->setElement($element);
        return $this->toHtml();
    }
}

Construct方法中指定输出phtml文件相对路径

Render 用来输出html

第三步:创建模板文件


<?php 
$_htmlId      = $this->getElement()->getHtmlId();
$_htmlClass   = $this->getElement()->getClass();
$_htmlName    = $this->getElement()->getName();
$_readonly    = $this->getElement()->getReadonly();
  
  
$collection = Mage::registry('rebate_rule_data');
       
$_counter = 0;
?>
<tr>
    <td class="label"><?php echo $this->getElement()->getLabel() ?></td>
    <td colspan="10" class="grid hours">
        <table id="attribute-options-table" class="dynamic-grid rkstorelocator-officehours" cellspacing="0" cellpadding="0"><tbody>
            <tr>
                <th><?php echo $this->__('销售数量(台)') ?></th><th><?php echo $this->__('推荐人单台返利(%) ') ?></th><th><?php echo $this->__('购买人单台折扣(%) ') ?></th>
                <th><button id="add_new_option_button" title="Add Option" type="button" class="scalable add"><span><span><span><?php echo $this->__('Add Option') ?></span></span></span></button></th>
            </tr>
<?php if(count($collection)>=1){?>            
<?php foreach ($collection as $_item): ?>
<tr class="option-row rkstorelocator-officehours-dayrow" id="hour-row-<?php echo $_counter?>">
    <td><input name="<?php echo $_htmlName; ?>[value][option_<?php echo $_counter ?>][num]" value="<?php echo $_item->getNum() ?>" class="input-text" type="text"></td>
    <td><input name="<?php echo $_htmlName; ?>[value][option_<?php echo $_counter ?>][recommend]" value="<?php echo $_item->getRecommend() ?>" class="input-text" type="text"></td>
    <td><input name="<?php echo $_htmlName; ?>[value][option_<?php echo $_counter ?>][buyer]" value="<?php echo $_item->getBuyer() ?>" class="input-text" type="text"></td>
    <td class="a-left" id="delete_button_container_option_<?php echo $_counter ?>'">
        <input name="<?php echo $_htmlName; ?>[value][option_<?php echo $_counter ?>][id]" value="<?php echo $_item->getId() ?>" type="hidden">
        <input id="delete-row-<?php echo $_counter ?>" type="hidden" class="delete-flag" name="<?php echo $_htmlName; ?>[delete][option_<?php echo $_counter ?>]" value=""/>
        <button οnclick="$('hour-row-<?php echo $_counter ?>').style.display='none'; $('delete-row-<?php echo $_counter ?>').setValue(1);" title="Delete" type="button" class="scalable delete delete-option"><span><span><span>Delete</span></span></span></button>
    </td>
</tr>
<?php
        $_counter++;
    endforeach;
    }
?>
</tbody></table>
  
<script type="text/javascript">//<![CDATA[
  
var _form_html_row = '<tr class="option-row rkstorelocator-officehours-dayrow" id="hour-row-{{id}}"><td><input name="<?php echo $_htmlName; ?>[value][option_{{id}}][num]" value="" type="text"></td><td><input name="<?php echo $_htmlName; ?>[value][option_{{id}}][recommend]" value="" type="text"></td><td><input name="<?php echo $_htmlName; ?>[value][option_{{id}}][buyer]" value="" type="text"></td><td id="delete_button_container_option_{{id}}"><input name="<?php echo $_htmlName; ?>[value][option_{{id}}][id]" value="" type="hidden"><input id="delete-row-{{id}}" type="hidden" name="<?php echo $_htmlName; ?>[delete][option_{{id}}]" value=""/><button οnclick="$(\'hour-row-{{id}}\').style.display=\'none\'; $(\'delete-row-{{id}}\').setValue(1);" title="Delete" type="button" class="scalable delete delete-option"><span><span><span>Delete</span></span></span></button></td></tr>';
  
var _rkstorelocator_counter = <?php echo $_counter?>;
  
$('add_new_option_button').observe('click', function(){
    $('attribute-options-table').insert(_form_html_row.replace(/\{\{id\}\}/ig, _rkstorelocator_counter));
    _rkstorelocator_counter++;
});
  
//]]></script>
    </td>
</tr>
以上就是对 Magento 后台开发表单添加层级表格的关键代码,更多 Magento开发与服务,请查看 Magento 开发

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值