简体   繁体   中英

How to refresh Datatable values after selecting drop down value in jQuery

According to my requirement i have a dropdown list and when i select a value from the dropdown appropriate data need to be fetch from the database. Dropdown select value sending through jQuery and as Ajax. i have few questions here

  1. Do i really want to refresh datatable to be displayed selected data?

Here are the code snappiest.

Dropdown -

<select class="filter" id="secquneceDropdownId">
    <option value="" selected>All</option>
    <option value="INSEQUENCE">In Sequence</option>
    <option value="OUTSEQUENCE">Out Sequence</option>
    <option value="RECIPES">Sequence Mapping</option>
</select>

jQuery and Ajax call to send dropdown selected value -

        $(document).ready(function() {
        $("#secquneceDropdownId").change(function() {
            var dropdownSelected = $(this).find(":selected").val()
            var clientID = {
                "isDropdownSelected" : dropdownSelected
            }
            console.log('dropdownSelected value is = ' + dropdownSelected)
            $.ajax({
                type : "POST",
                url : "/IdeaOfThings/listSequences",
                data : JSON.stringify(clientID),
                contentType : 'application/json; charset=utf-8',
                dataType : "json",

                success : function(data) {

                }
            });

        });
    });

Datatable Call for show Data -

$(document).ready(
    function() {
       var table = $('#example2').DataTable({
            "ordering" : false,
      });
});

I don't see any link between your ajax and datatable .

To answer your question 1.

Yes, you will need to refresh the grid each time on combo change as Data fetched will depend on the option selected.

If you are looking for how to implement it

Here is a Sample

$("#secquneceDropdownId").change(function() {
    loadData();

}); 


function loadData(){
   var dropdownSelected = $("#secquneceDropdownId").val()
    var clientID = {
        "isDropdownSelected" : dropdownSelected
    }  

    $("#tblDeviceDetail").DataTable({
        "columns": [
           { "data": "ip" },
           { "data": "apiPort" },
           { "data": "hostname" }

           ],
           "ajax": {
               "url": "/IdeaOfThings/listSequences",
               data : JSON.stringify(clientID),
               contentType : 'application/json; charset=utf-8',
               dataType : "json",
               "type": "POST",
               "dataSrc": "[]",

           }
       });

   }

assuming your JSON is like this

[ { "ip" : "sfsdsdfs" , "apiPort" : "322",  "hostname": "sfsdfsdf"}, 
{ "ip" : "5345345" , "apiPort" : "4444",  "hostname": "sadfasds"}] 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM