First Time you have see only country drop down ..
When you select country that time load ajax
And Next Update all select option with ajax
So now ready to start and display country and state in drop down in custom module.
Open your form which is in Yournamespace/Modulename/Block/Adminhtml/Modulename/Edit/Tab/Form.php then add below fields
$country = $fieldset->addField('country', 'select', array(
'name' => 'country',
'label' => Mage::helper('storelocator')->__('Country'),
'values' => Mage::getModel('adminhtml/system_config_source_country')->toOptionArray(),
'class' => 'required-entry',
'required' => true,
'onchange' => 'getstate(this)',
));
//Edit action pass your custom table to country id and get state
$storeId = $this->getRequest()->getParam('id');
if($storeId !=''):
$editState = $stateCollection = Mage::getModel('storelocator/storelocator')->load($storeId);
$stateCollection = Mage::getModel('directory/region')->getResourceCollection()->addCountryFilter($editState->getCountry())->load();
$state = "";
foreach ($stateCollection as $_state) {
$state[]= array('value'=>$_state->getCode(),'label'=>$_state->getDefaultName());
}
$fieldset->addField('state', 'select', array(
'label' => Mage::helper('storelocator')->__('State'),
'required' => false,
'name' => 'state',
'selected' => 'selected',
'values' => $state,
));
else:
//New action
$fieldset->addField('state', 'select', array(
'name' => 'state',
'required' => false,
'label' => Mage::helper('storelocator')->__('State'),
'values' => '--Please Select Country--',
));
endif;
/*
* Add Ajax to the Country select box html output
*/
$country->setAfterElementHtml("<script type=\"text/javascript\">
function getstate(selectElement){
var reloadurl = '". $this->getUrl('storelocator/adminhtml_storelocator/state') . "country/' + selectElement.value;
new Ajax.Request(reloadurl, {
method: 'get',
onComplete: function(transport){
var response = transport.responseText;
$('storelocatorstate').update(response);
}
});
}
</script>");
And Now Create State Action in modulenamecontroller.php file which will be like this
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;
}
It's working in Magento1.7 I already tested. i have use only prototype js code with ajax this code not conflict any js
I Think This Post is Usefully You :)
Great Read! I am impressed on how you make your article easy to understand. I'll come back for more :D
ReplyDeleteJaps Buidon is a Social Media Specialist and SEO from a renowned Magento Development Company in Florida. He loves hiking as well as electronics.
HI,
ReplyDeleteIts very useful article and easy to understand, Thanks for share this kind of article.Save my lot of time.
I'll come back for more.:)
Nice blog. Thank you for sharing.
ReplyDeleteMagento Support Service
Magento 2 Development Service