2

こんにちは、管理者のmagento製品で選択した国に基づいて状態のドロップダウンを表示したいのですが、これを行う方法を教えてください。

4

4 に答える 4

8

Sunel.次の解決策を確認してください。

フォームを開き、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ファイルに状態アクションを作成します

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;
}

ありがとうございました。

于 2013-08-03T11:01:11.460 に答える
0
/*
* Add Ajax to the Country select box html output
*/
$url = Mage::getBaseUrl();
$country->setAfterElementHtml("<script type=\"text/javascript\">
    function getstate(selectElement){
        var reloadurl = '".$url.'admin/customer/state/' . "country/' + selectElement.value;
        new Ajax.Request(reloadurl, {
            method: 'POST',
            onComplete: function(){
            },
            success: function (response) {
            alert(response)
            }
        });
    }
</script>");
于 2016-05-18T07:56:56.553 に答える
-1
//        if (Mage::app()->isSingleStoreMode()) {
//            $fieldset->removeField('website_id');
//            $fieldset->addField('website_id', 'hidden', array(
//                'name'      => 'website_id'
//            ));
//            $customer->setWebsiteId(Mage::app()->getStore(true)->getWebsiteId());
//        }
//        $fieldset->addField('country', 'select', array(
//                            'name'  => 'country',
//                            'label' => 'Country',
//                            'values' => Mage::getModel('adminhtml/system_config_source_country')->toOptionArray(),  
//            ));
//       
//        $fieldset->addField('state', 'select', array(
//            'name'  => 'state',
//            'label'     => 'State',
//            'values'    => Mage::getModel('adminhtml/system_config_source_region')->getResourceCollection(),
//        ));

 

        $country = $fieldset->addField('country', 'select', array(

            'name' => 'country',

            'label' => Mage::helper('customer')->__('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('directory/region')->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('customer')->__('State'),

                'required' => false,

                'name' => 'state',

                'selected' => 'selected',

                'values' => $state,

            ));

        else:

            //New action

            $fieldset->addField('state', 'select', array(

                'name' => 'state',

                'required' => false,

                'label' => Mage::helper('customer')->__('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('adminhtml_customer/state') . "country/' + selectElement.value;

                new Ajax.Request(reloadurl, {

                    method: 'get',

                    onComplete: function(transport){

                        var response = transport.responseText;

                        $('storelocatorstate').update(response);

                    }

                });

            }

        </script>");
于 2016-05-12T09:02:42.250 に答える