How to load json to datatables on jquery on click function?

Munna Babu

I have json data like this and now i need to feed this json data into datatables whenever the search button is clicked based on sending values.

[
{
       "port_code":"BOM",
       "cont_details_id":"9",
       "price":"44.000",
       "cont_price":"500",
       "cont_no":"11",
       "cont_size":"20",
       "cont_type":"GP"
},
{
       "port_code":"BOM",
       "cont_details_id":"10",
       "price":"87.000",
       "cont_price":"500",
       "cont_no":"22",
       "cont_size":"20",
       "cont_type":"GP"        
},
.....
.....
etc.,
]

and this is what i tried in jquery to store json $("#search").click(function() in this function i am calling json file and tried to store in datatables, but its not working. please someone help me from this. Thank you.

$(document).ready(function() 
    {

    var oTable = $('#example').DataTable();

    $("#search").click(function()
    {
        $.post("invoice_ajax.php",
        {
            loc  : $("#location").val(),
            cust : $("#customer_details_id").val()
        },
        function(data)
        {
            $("#text").html(data);
            var s = JSON.parse(data);

                        for(var i = 0; i < s.length; i++) 
                        {   
                            oTable.fnAddData([
                            s[i].block_id,
                            s[i].block_id,
                            s[i].block_id,
                            s[i].block_id,
                            s[i].block_id,
                            s[i].block_id,
                            s[i].block_id
                            ]);
                        } // End For
        });
    }); 


    });
davidkonrad

You are almost right, except you are using the old 1.9.x fnAddData method on a 1.10.x API. Do this instead :

oTable.row.add([
   s[i].port_code,
   s[i].cont_details_id,
   s[i].price,
   s[i].cont_price,
   s[i].cont_no,
   s[i].cont_size,
   s[i].cont_type
]).draw();

or

var oTable = $('#example').dataTable();
...
oTable.fnAddData([
   s[i].port_code,
   s[i].cont_details_id,
   s[i].price,
   s[i].cont_price,
   s[i].cont_no,
   s[i].cont_size,
   s[i].cont_type
]);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Jquery and Datatables call function on checkbox click is not working

From Dev

JS click in a Jquery function on load

From Dev

JS click in a Jquery function on load

From Dev

How load json data on table using datatables?

From Dev

How To Load Two Sourced Data into DataTables on Button Click event

From Dev

jQuery click and load function do not work

From Dev

jquery load + DataTables

From Dev

Why does jQuery .on( 'click', foo ) run the function on page load and not on click?

From Dev

Why does jQuery .on( 'click', foo ) run the function on page load and not on click?

From Dev

how to enable and disable paging from jquery dataTables on button click

From Dev

how to enable and disable paging from jquery dataTables on button click

From Dev

How to use a .click function in jQuery

From Dev

how load function behaviour in jquery

From Dev

How to load input elements by the load function ? jquery

From Dev

How to load Datatables from JSON object directly on Ajax success result?

From Dev

jquery datatables undefined is not a function

From Dev

JQuery DataTables plugin click button

From Dev

Load new Data into Jquery DataTables

From Dev

Load new Data into Jquery DataTables

From Dev

jQuery Datatables load data ajax

From Dev

jquery Datatables using JSON

From Dev

jquery Datatables using JSON

From Dev

On('click') jquery doesn't work after page loaded with load() function

From Dev

jQuery .click() function only works once with $(window).load()

From Dev

Create a jquery function independent of a click event and run in on load of page

From Dev

JQuery function - run on click and on page load for specific radio button

From Dev

How can I call click function to another click function in jQuery

From Dev

Jquery Datatables How to get column Id in render function

From Dev

How to pass html in jquery datatables row add function

Related Related

  1. 1

    Jquery and Datatables call function on checkbox click is not working

  2. 2

    JS click in a Jquery function on load

  3. 3

    JS click in a Jquery function on load

  4. 4

    How load json data on table using datatables?

  5. 5

    How To Load Two Sourced Data into DataTables on Button Click event

  6. 6

    jQuery click and load function do not work

  7. 7

    jquery load + DataTables

  8. 8

    Why does jQuery .on( 'click', foo ) run the function on page load and not on click?

  9. 9

    Why does jQuery .on( 'click', foo ) run the function on page load and not on click?

  10. 10

    how to enable and disable paging from jquery dataTables on button click

  11. 11

    how to enable and disable paging from jquery dataTables on button click

  12. 12

    How to use a .click function in jQuery

  13. 13

    how load function behaviour in jquery

  14. 14

    How to load input elements by the load function ? jquery

  15. 15

    How to load Datatables from JSON object directly on Ajax success result?

  16. 16

    jquery datatables undefined is not a function

  17. 17

    JQuery DataTables plugin click button

  18. 18

    Load new Data into Jquery DataTables

  19. 19

    Load new Data into Jquery DataTables

  20. 20

    jQuery Datatables load data ajax

  21. 21

    jquery Datatables using JSON

  22. 22

    jquery Datatables using JSON

  23. 23

    On('click') jquery doesn't work after page loaded with load() function

  24. 24

    jQuery .click() function only works once with $(window).load()

  25. 25

    Create a jquery function independent of a click event and run in on load of page

  26. 26

    JQuery function - run on click and on page load for specific radio button

  27. 27

    How can I call click function to another click function in jQuery

  28. 28

    Jquery Datatables How to get column Id in render function

  29. 29

    How to pass html in jquery datatables row add function

HotTag

Archive