您的位置:首页 > 其它

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

2015-08-18 09:57 585 查看
第一步:

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

$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')
);

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

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

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() ?
4000
>" 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 onclick="$('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 onclick="$(\'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
开发
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息