get Data from DB using Spring MVC to show in dropdown list like Country, state, city

Sadina Khatun

I have 3 Drop Down list which are dependent on each other as Specific country contain Certain States and those states contain specific City,

all These Country, state and City have a Service call to DAO to get data from DB how i can manage Drop Down with the Help of Spring MVC to display proper Country and state and city in Drop down List of each

Joe ONeil

Create a REST Call to produce JSON Country object has a list of State Objects

   @RequestMapping(value= "/countryState", produces = MediaType.APPLICATION_JSON_VALUE)
    public List< CountryStateCity> retrieveCountryState() {
    return ddlService.retrieveCountryState();
    } 

Create another Rest call to get the City base on Country and State

  @RequestMapping(value= "/city", produces = MediaType.APPLICATION_JSON_VALUE)
    public List< CountryStateCity> retrieveCity(String country,String state) {
    return ddlService.retrievecity(country,state);
    } 

I used AJAX/jquery in this example To

   //Fill first Dropdown

    $.ajax({
    type:     "GET",
     url: "http://192.168.33.60:8080/countryStateCity?callback=?",
     dataType: "jsonp",
     jsonpCallback: 'jsonCallback',
     cache:false,
    success: function(data) {
        var dataToStore = JSON.stringify(data);
        localStorage.setItem('jsonResults', dataToStore);//Store json to browser local storage
        $("#country").get(0).options.length = 0;
        $("#country").get(0).options[0] = new Option("Select Country", "0");   

        $.each(data, function(index, item) {
            $("#country").get(0).options[$("#country").get(0).options.length] = new Option(item.mfrname, item.countryId);
        });
    },
    error: function(e) {
      //  alert("Error Loading Manufactures");
    }
});



 $("#country").on("change", function (event) { //Change event on country Drop down clear State and City Dropdowns
    var countryId=$("#country").val();
    var stateId=$("#state").val();
    $('#City').find('option').remove().end(); //clear City dropdown and add only select
    $("#City").get(0).options[0] = new Option("--Select--", "0"); 
    $('#state').find('option').remove().end();
    $('#category').find('option').remove().end();

    resetTables();


     var localData = JSON.parse(localStorage.getItem('jsonResults'));// Go to local storage and get state values
    var i = null;
    for (i = 0; localData.length > i; i += 1) {
        if (localData[i].countryId === countryId) {
              $.each(localData[i].states, function(index, value) {
               $("#state").get(0).options[$("#state").get(0).options.length] = new Option(value.description, value.code);
            });
        }
    }

 });     
$("#state").on("change", function (event) {//State change event clear city then go to 
    var countryId=$("#country").val();
    var stateId=$("#state").val();
 // alert(countryId!="0"&&stateId!="0");
     $('#City').find('option').remove().end();
    $("#City").get(0).options[0] = new Option("--Select--", "0"); 
    $('#category').find('option').remove().end();
    resetTables();
    if(countryId!="0"&&stateId!="0"){
    //now call the citys Rest call   
    $.ajax({
        type:     "GET",
         url: "http://192.168.33.60:8080/cities?countryId="+countryId+"&stateId="+stateId+"&callback=?",
         dataType: "jsonp",
         jsonpCallback: 'jsonCallback',
         cache:false,
        success: function(data) {
            jsonResults=data;
              $.each(data, function(index, item) {
                   $("#City").get(0).options[$("#City").get(0).options.length] = new Option(item.description, item.code);
            });
        },
        error: function(e) {
          //  alert("Error Loading cities");
        }
    });
}  else{
    $("#City").get(0).options[0] = new Option("--Select--"+countryId, "0"); 
} 

I made this from existing code and have not tried it but you should get the gist

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Country City State List

From Dev

State city dropdown list

From Dev

Get state and country place_id from city using Google Maps API

From Dev

How to get count of State and city of country using SQL query from database?

From Dev

How to get City / State / Country for Mapbox using reverse geocoding?

From Dev

Get list of all city/place names in a country using Nominatim/OpenStreetMap

From Dev

Get list of all city/place names in a country using Nominatim/OpenStreetMap

From Dev

Get city and country from AutoCompleteTextView

From Dev

get user country by ip and show it on top of dropdown list but NOT preselect it

From Dev

Using MVC dropdown list how to edit form and show the data in dropdown and previously selected item in dropdown?

From Dev

How to get object instead of string from dropdown list in Spring MVC

From Dev

Get DB content into dropdown and show DB info from selected dropdown

From Dev

Aggregate by country, state and city

From Dev

Dropdown List bingding of State and City through database

From Dev

After Login Show me from which city and country user is using vb.net

From Dev

Get State from City PHP

From Dev

How to get city , state and Country name by Pincode in Swift iOS?

From Dev

How to store City, State and Country data related json in single area

From Dev

best data structure for country,state,city,zip-code hierarchy

From Dev

Jquery Ui autocomplete get city from only one country

From Dev

Getting Country / City data from google-app-engine

From Dev

How to set the 'selected option' of a select dropdown list country and state

From Dev

Using the WooCommerce API to get the country/state lists

From Dev

Reverse Geocoding: Get city and country

From Dev

python geopy get city and country

From Dev

How to make dropdown list with checkboxes using Spring MVC?

From Dev

Chained Select Boxes (Country, State, City)

From Dev

Storing Country, State, City SQL Design

From Dev

Country, state and city spinner not working correctly

Related Related

  1. 1

    Country City State List

  2. 2

    State city dropdown list

  3. 3

    Get state and country place_id from city using Google Maps API

  4. 4

    How to get count of State and city of country using SQL query from database?

  5. 5

    How to get City / State / Country for Mapbox using reverse geocoding?

  6. 6

    Get list of all city/place names in a country using Nominatim/OpenStreetMap

  7. 7

    Get list of all city/place names in a country using Nominatim/OpenStreetMap

  8. 8

    Get city and country from AutoCompleteTextView

  9. 9

    get user country by ip and show it on top of dropdown list but NOT preselect it

  10. 10

    Using MVC dropdown list how to edit form and show the data in dropdown and previously selected item in dropdown?

  11. 11

    How to get object instead of string from dropdown list in Spring MVC

  12. 12

    Get DB content into dropdown and show DB info from selected dropdown

  13. 13

    Aggregate by country, state and city

  14. 14

    Dropdown List bingding of State and City through database

  15. 15

    After Login Show me from which city and country user is using vb.net

  16. 16

    Get State from City PHP

  17. 17

    How to get city , state and Country name by Pincode in Swift iOS?

  18. 18

    How to store City, State and Country data related json in single area

  19. 19

    best data structure for country,state,city,zip-code hierarchy

  20. 20

    Jquery Ui autocomplete get city from only one country

  21. 21

    Getting Country / City data from google-app-engine

  22. 22

    How to set the 'selected option' of a select dropdown list country and state

  23. 23

    Using the WooCommerce API to get the country/state lists

  24. 24

    Reverse Geocoding: Get city and country

  25. 25

    python geopy get city and country

  26. 26

    How to make dropdown list with checkboxes using Spring MVC?

  27. 27

    Chained Select Boxes (Country, State, City)

  28. 28

    Storing Country, State, City SQL Design

  29. 29

    Country, state and city spinner not working correctly

HotTag

Archive