Return to Snippet

Revision: 66074
at March 11, 2014 20:44 by muzahidict


Initial Code
/**
 * author : MD. MUZAHIDUL ISLAM ([email protected])
 * file name : cascade-load.js
 * This segment of code uses jQuery(jquery-1.7.2.js) that helps to load cascade data from sever.
 * To use this code, 'div' element should contains a 'select' html element and
 * needs following  attributes of div element:
 *         class : class attribute must contain 'geo'
 *         load-data-container : selector of an element that will contain the response that comes from server
 *         url : server url to load data
 *
 * Sample code :
 * <!-- action div -->
 * <div id="parentDivId" class="geo" url="/loadGeoDistrict" load-datacontainer="#childDivId" >
 *     <select> <option>1</option><option>1</option></select>
 * </div>
 * <!-- load div -->
 * <div id ="childDivId">
 * </div>
 * */
$("document").ready(function(){
    $(".geo").on("change", function() {
        var currElement =  $(this);
        var loadDataContainer = $(currElement.attr("load-data-container"));
        loadDataContainer.empty();//to clear data container element
        loadDataContainer.trigger("change");//to clear all child needs a trigger
        var url = currElement.attr("url");
        var firstSelectElement = currElement.find("select:first");
        var geoId = $(firstSelectElement).find(":selected").attr("value");
        
        if(geoId) {
            loadData(url, "get", geoId).done(function(data) {
                loadDataContainer.html(data);
            });
        }
    });
    /**
     * return data from successful AJAX request
     * */
    function loadData(url, type, geoId) {
        return $.ajax(
            {
                url : url,
                type : type,
                data : {id : geoId}
            }
        );
    }
});

Initial URL

                                

Initial Description
/* author : MD. MUZAHIDUL ISLAM ([email protected])
 * file name : cascade-load.js
 * This segment of code uses jQuery(jquery-1.7.2.js) that helps to load cascade data from sever.
 * To use this code, 'div' element should contains a 'select' html element and
 * needs following  attributes of div element:
 *         class : class attribute must contain 'geo'
 *         load-data-container : selector of an element that will contain the response that comes from server
 *         url : server url to load data
 *
 */

Initial Title
Cascade Data Load by jQuery AJAX

Initial Tags
javascript, jquery

Initial Language
jQuery