1

I follow this tutorial with title Cascading Dropdown List using JSON Data - Learn Infinity https://www.youtube.com/watch?v=T79I5jOJlGU&t=11s

I have 4 select tags ( -region -province -municipality -barangay )

First I populate the dropdown list from JSON DATA

I achieve the first one that I have to select a specific region first before the province gets populate.

What I want to achieve next is to set selected text from the database into cascading dropdown.

HTML CODE:

<form name="addressForm">
  <select name="region" id="region">
    <option value="">Select Region</option>
  </select>
  <select name="province" id="province">
    <option value="">Select Province</option>
  </select>
  <select name="municipality" id="municipality">
    <option value="">Select Municipality</option>
  </select>
  <select name="barangay" id="barangay">
    <option value="">Select Barangay</option>
  </select>
</form>

JS CODE

$(document).ready(function(e){
    function get_json_data(code,region) {
        //alert("a " + code)
        var html_code = "";
        $.getJSON('/templates/ph_API/CombineAddress.json', function(data){
            ListName = code.substr(0, 1).toUpperCase() + code.substr(1);
            html_code += '<option value="">Select ' + ListName + '</option>';
            $.each(data, function(key, value) {
                if(value.region == region) {
                    html_code += '<option value="' + value.code + '">' + value.name + '</option>';
                }
            });
            $('#' + code).html(html_code);
        });
    }
    get_json_data('region', 0);

    $(document).on('change', '#region', function() {
        var region_id = $(this).val();
        if (region_id != '') {
            get_json_data('province', region_id);
        } else {
            $('#province').html('<option value="">Select Province</option>');
        }
        $('#municipality').html('<option value="">Select Municipality</option>');
        $('#barangay').html('<option value="">Select Barangay</option>');
    });

    $(document).on('change', '#province', function() {
        var province_id = $(this).val();
        if (province_id != '') {
            get_json_data('municipality', province_id);
        } else {
            $('#municipality').html('<option value="">Select Municipality</option>');
        }
    });

    $(document).on('change', '#municipality', function() {
        var municipality_id = $(this).val();
        if (municipality_id != '') {
            get_json_data('barangay', municipality_id);
        } else {
            $('#barangay').html('<option value="">Select Barangay</option>');
        }
    });

    $("#region option:selected").text('{{ form.region.value }}');
    $("#province option:selected").text('{{ form.province.value }}');
    $("#municipality option:selected").text('{{ form.municipality.value }}');
    $("#barangay option:selected").text('{{ form.barangay.value }}');
});

In last 4 line of code with

$("#region option:selected").text('{{ form.region.value }}');
$("#province option:selected").text('{{ form.province.value }}');
$("#municipality option:selected").text('{{ form.municipality.value }}');
$("#barangay option:selected").text('{{ form.barangay.value }}');

It set the text from the database into a dropdown but the populate function not working unless I change the selected region first to get load the next dropdown list "province"

I Update it here's the JSON FILE DATA

[
//List for Region drop down
 { "code": "001", "name": "Metro Manila" , "region": "000" },
 { "code": "002", "name": "Mindanao" , "region": "000" },

//Province for Metro Manila / Mindanao
 {"code": "0010", "name": "Metro Manila", "region": "001" },
 { "code": "0011", "name": "Agusan Del Norte", "region": "002" },

//Municipality for Metro Manila / Agusan Del Norte
 { "code": "00025", "name": "Caloocan City", "region": "0010" },
 { "code": "00026", "name": "Ermita", "region": "0010" },
 { "code": "00027", "name": "Buenavista", "region": "0011", },
 { "code": "00028", "name": "BUTUAN CITY", "region": "0011", },

//Barangay for Caloocan City / Ermita
 { "code": "000041", "name": "Tandang", "region": "00025", },
 { "code": "000042", "name": "Manila, "region": "00025", },
 { "code": "000041", "name": "Imus", "region": "00026", },
 { "code": "000042", "name": "Tarlac, "region": "00026", },

//Barangay for Buenavista / BUTUAN CITY"
 { "code": "000041", "name": "Rizal", "region": "00027", },
 { "code": "000042", "name": "Tayak, "region": "00027", },
 { "code": "000041", "name": "Liliw", "region": "00028", },
 { "code": "000042", "name": "Quezon, "region": "00028", },
]
miss rain
  • 15
  • 6
  • Can you please host /templates/ph_API/CombineAddress.json or give me the data so that I can build and give you solution? – sawan nirala Jun 03 '21 at 08:21
  • @sawannirala Hi, I update the question with JSON FILE DATA on it please have a look. – miss rain Jun 03 '21 at 09:11
  • Just a quick question, why do you need $("#region option:selected").text('{{ form.region.value }}'); $("#province option:selected").text('{{ form.province.value }}'); $("#municipality option:selected").text('{{ form.municipality.value }}'); $("#barangay option:selected").text('{{ form.barangay.value }}'); These 4 line. I mean without that code is also working fine. – sawan nirala Jun 03 '21 at 10:47
  • @sawannirala that line of code is to set selected value from database It's working but it shows only the text and not populated anymore select tag – miss rain Jun 03 '21 at 13:11
  • No need to set the value explicitly, the select box automatically handled that. – sawan nirala Jun 03 '21 at 15:19
  • can you show me the example? Hi have variable Region = "Metro Manila" Province = "Metro Manila" Municipality = "Ermita" Barangay = "Tarlac" How can I automatically load it or select it? – miss rain Jun 03 '21 at 17:13
  • You can do it like $("#region").val(yourVariable); https://stackoverflow.com/questions/499405/change-the-selected-value-of-a-drop-down-list-with-jquery – sawan nirala Jun 04 '21 at 10:13
  • in which line should I put that? – miss rain Jun 04 '21 at 12:06
  • Hi, these value `form.region.value..` are coming on your page when page loads or on every request to server ? Why not on each change event get selected value from server and set them inside `$.getJson..` ? – Swati Jun 04 '21 at 13:00
  • it's coming from the server it's like I'm building an automatic fill-up address. for a shopping cart if the user has a default address it should automatically select it from the populated address dropdown. i only manage to get it selected from the first dropdown list but after that, it's not working in the second dropdown list. – miss rain Jun 05 '21 at 06:16

0 Answers0