Monday, December 3, 2012

how to use Jquery datataable

1. add jquery.dataTables.js and jquery.dataTables.css in your html. keep these files in proper folder.




2. keep below code in your html code.

AJax call with json response


Set Json response in Controller:

PrintWriter writer = response.getWriter();
JSONArray storeList = new JSONArray();
JSONObject store = null;
List list = new ArrayList();
list.add("RS");
list.add("EUR");
list.add("DOLLER");
for (String string : list) {
    store = new JSONObject();
    store.put("storeName", string);
    storeList.put(store);
}
JSONObject json = new JSONObject();
json.put("storeList", storeList);
sb = json.toString();
writer.write(sb);












ajax call:

Way 1:
$.ajax({
        url : currentPath+'/appName/partnerStoreAddress',
        data : 'store='+selectedValue+'&cbn='+cbnValue,
        dataType : 'json',
        success : function(json) {
            createAdressTable(json);
            $.unblockUI();
        },
        error:function(xhr,errorThrown){
              var r = jQuery.parseJSON(xhr.responseText);
              alert("Message: " + r.Message);
              alert("StackTrace: " + r.StackTrace);
              alert("ExceptionType: " + r.ExceptionType);
              alert(xhr.status);
              alert(errorThrown);
        }
    });

Way 2:

function setupAjaxForm(form_id, form_validations){
    var form = '#' + form_id;
   
    // setup jQuery Plugin 'ajaxForm'    
    var options = {
        target : '#refresh',  //if you want to refresh any div only
        dataType:  'json',
        beforeSubmit: function(){
        $.unblockUI();
         $.blockUI({
                message : ''
            })
            //pageRefresh = 'Y';
        },
        timeout:8000,
        success: function(json){
            $.unblockUI();
            $("#refresh").html($(html).find("#refresh"));
            $('#HPContactEmailList_filter').appendTo($('.themeheader'));
                       
            new setupAjaxForm('dummyForm'); 
           
        },
        error:function(xhr,errorThrown){
            $.unblockUI();
            alert('Selected item was not saved');
           
        }
    };
    $(form).ajaxForm(options);
}


$(document).ready( function () {
   
    $('#HPContactEmailList_filter').appendTo($('.themeheader'));
    new setupAjaxForm('dummyForm'); 
    }
);