我已经使用它自己的网格构建了一个自定义商店定位器扩展,并在Adminhtml中编辑页面,并且一切正常。对于商店的营业时间,我想实现一个动态网格,例如属性选项。
现在,我找到了一个解决方案,但我希望有更好的方法,或者至少是更清洁的方法。到目前为止,我正在向表单中的字段添加渲染器fieldset
class Redkiwi_Rkstorelocator_Block_Adminhtml_Rkstorelocator_Edit_Tab_General extends Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareForm()
{
$form = new Varien_Data_Form();
$this->setForm($form);
$fieldset = $form->addFieldset('rkstorelocator_form', array('legend'=>Mage::helper('rkstorelocator')->__('Store information')));
[...]
$officehours_field = $fieldset->addField('office_hours', 'editor', array(
'name' => 'office_hours',
'label' => Mage::helper('rkstorelocator')->__('Office hours'),
'required' => false,
));
$officehours_block = $this->getLayout()
->createBlock('rkstorelocator/adminhtml_rkstorelocator_edit_renderer_officehours')
->setData(array(
'name' => 'office_hours',
'label' => Mage::helper('rkstorelocator')->__('Office hours'),
'required' => false,
));
$officehours_field->setRenderer($officehours_block);
[...]
}
}
还有一个要渲染的块类
class Redkiwi_Rkstorelocator_Block_Adminhtml_Rkstorelocator_Edit_Renderer_Officehours
extends Mage_Adminhtml_Block_Abstract
implements Varien_Data_Form_Element_Renderer_Interface
{
public function render(Varien_Data_Form_Element_Abstract $element)
{
$required_indicator = $this->getData('required') ? '<span class="required">*</span>' : '' ;
$html = '
<table id="attribute-options-table" class="dynamic-grid rkstorelocator-officehours" cellspacing="0" cellpadding="0"><tbody>
<tr>
<th>Day indicator</th>
<th>Opening hour</th>
<th>Closing hour</th>
<th>
<button id="add_new_option_button" title="Add Option" type="button" class="scalable add"><span><span><span>Add Option</span></span></span></button>
</th>
</tr>
</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="'.$this->getData('name').'[value][option_{{id}}][0]" value="" class="input-text required-option" type="text"></td><td><input name="'.$this->getData('name').'[value][option_{{id}}][2]" value="" class="input-text required-option" type="text"></td><td><input name="'.$this->getData('name').'[value][option_{{id}}][2]" value="" class="input-text required-option" type="text"></td><td class="a-left" id="delete_button_container_option_{{id}}"><input type="hidden" class="delete-flag" name="'.$this->getData('name').'[delete][option_{{id}}]" value=""/><button onclick="$(\\\'hour-row-{{id}}\\\').remove();" title="Delete" type="button" class="scalable delete delete-option"><span><span><span>Delete</span></span></span></button></td></tr>\';
var _rkstorelocator_counter = 0;
$(\'add_new_option_button\').on(\'click\', \'button\', function(){
$(\'attribute-options-table\').insert(_form_html_row.replace(/\{\{id\}\}/ig, _rkstorelocator_counter));
_rkstorelocator_counter++;
});
//]]></script>
';
return $html;
}
}
这给我以下结果
现在这基本上可以用了,但是获取当前值会很混乱,而且总的来说,我对自己编写的代码并不感到骄傲(正如您可能想象的那样)。
我已经用Google搜索了几种解决方案,但通常都采用这种方法。有谁知道一种更清洁的方法吗?
我可以获取用于此自定义模块的每个文件的完整代码吗?
—
UdayPavan Reddy