这是未经测试的,但它应该足以让你走上正轨:
仅输出特定块
在前端,大多数块都是通过布局 XML 实例化的。在该adminhtml
领域中这是不同的,因此您需要更多地使用 PHP 实例化。
在您的 AJAX 操作中,我假设您当前正在调用loadLayout()
和renderLayout()
.
要仅输出特定部分,请改用:
public function yourAjaxAction()
{
// assuming the required config section is set in the AJAX request
$sectionCode = $this->getRequest()->getParam('section');
$sections = Mage::getSingleton('adminhtml/config')->getSections();
$blockName = (string)$sections->frontend_model;
if (empty($blockName)) {
$blockName = Mage_Adminhtml_Block_System_Config_Edit::DEFAULT_SECTION_BLOCK;
}
$block = $this->getLayout()->createBlock($blockName)->initForm();
// Set the AJAX response content
$this->getResponse()->setBody($block->toHtml());
}
表单键
可以通过以下方式获取表单密钥
Mage::getSingleton('core/session')->getFormKey()
它必须以回发到服务器的形式存在。您可以使用以下代码使用表单键创建 HTML 隐藏字段:
// If loadLayout() was called:
$formkeyHtml = Mage::app()->getLayout()->getBlock('formkey')->toHtml();
// If working without layout XML:
$formkeyHtml = Mage::app()->getLayout()->createBlock('core/template', 'formkey')
->setTemplate('formkey.phtml') // adminhtml theme formkey
//->setTemplate('core/formkey.phtml') // frontend theme formkey
->toHtml();
添加 configForm JavaScript
该configForm
变量是varienForm
包含配置字段的 DOM 元素的 JS 对象。
它是使用实例化的:
// config_edit_form is the CSS id
configForm = new varienForm('config_edit_form');
声明在varienForm
文件js/varien/form.js中。
系统配置还使用了一些额外的 javascript。Magento 总是添加这些块来设置系统配置 JS 环境:
Mage::app()->getLayout()->getBlock('js')->append(
$this->getLayout()->createBlock('adminhtml/template')
->setTemplate('system/shipping/ups.phtml')
);
Mage::app()->getLayout()->getBlock('js')->append(
$this->getLayout()->createBlock('adminhtml/template')
->setTemplate('system/config/js.phtml')
);
Mage::app()->getLayout()->getBlock('js')->append(
$this->getLayout()->createBlock('adminhtml/template')
->setTemplate('system/config/applicable_country.phtml')
);
我希望这能让你开始。