嗨,我想根据管理员在magento产品中选择的国家/地区显示状态下拉列表,任何人都可以给出一个如何做到这一点的想法。
最佳答案
Sunel.please检查休闲的解决方案可能会对您有帮助。
打开Yournamespace/Modulename/Block/Adminhtml/Modulename/Edit/Tab/Form.php
中的表单,然后添加以下字段
$country = $fieldset->addField('country', 'select', array(
'name' => 'country',
'label' => 'Country',
'values' => Mage::getModel('adminhtml/system_config_source_country') ->toOptionArray(),
'onchange' => 'getstate(this)',
));
$fieldset->addField('state', 'select', array(
'name' => 'state',
'label' => 'State',
'values' => Mage::getModel('modulename/modulename')->getstate('AU'),
));
/*
* Add Ajax to the Country select box html output
*/
$country->setAfterElementHtml("<script type=\"text/javascript\">
function getstate(selectElement){
var reloadurl = '". $this->getUrl('modulename/adminhtml_modulename/state') . "country/' + selectElement.value;
new Ajax.Request(reloadurl, {
method: 'get',
onLoading: function (stateform) {
$('state').update('Searching...');
},
onComplete: function(stateform) {
$('state').update(stateform.responseText);
}
});
}
</script>");
现在在modulenamecontroller.php文件中创建State Action,就像这样
public function stateAction() {
$countrycode = $this->getRequest()->getParam('country');
$state = "<option value=''>Please Select</option>";
if ($countrycode != '') {
$statearray = Mage::getModel('directory/region')->getResourceCollection() ->addCountryFilter($countrycode)->load();
foreach ($statearray as $_state) {
$state .= "<option value='" . $_state->getCode() . "'>" . $_state->getDefaultName() . "</option>";
}
}
echo $state;
}
谢谢。