Struggling with ajax on button click to table

Struggling with ajax on button click to table

AtlasAtlas Posts: 2Questions: 1Answers: 0
edited March 2016 in Free community support

Hi All,

Firstly Thank you for taking the time to read my question.
I have a table that i am trying to populate after clicking a button.
when the button is clicked i can see the json data in an alert so i know its formatting is ok.

i've been looking at this for days and just cannot see where i am going wrong, its probably something very simple but i just cannot see it, i have looked around the forum (most of the below code is from here) for answers but no luck.

i have to admit i have looked at json arrays and objects and have totally confused myself..

i have been using editor (under a different account) for a few months and have no issues with that but struggling with this one.

any help would be very much appreciated..

button click event contains the below

   $.ajax({
            url: "../php/data.php",
            type: "POST",
                    data: {
                        gettype: "alpha",
                        getdata: $('input#Search').val()
                        },
        success: function(result) {
            alert (result);
            t.clear().draw();
            t.rows.add(result)
            .draw();
                            }

as mentioned the above button does show me valid json data

the json data returned from the above button click

{"draw":0,"recordsTotal":5,"recordsFiltered":5,"data":[
["Dave","Heath","HH43","Att2","140","Tango","Van","Stage","Away","2016\/01\/06","09:24:08","09:54:25"],
["John","Heath","HH42","Att8","140","Charlie","Van","Deck","Site","2016\/01\/06","10:34:37","11:33:58"],
["John","Reddes","HH45","Att2","140","Lima","Van","Deck","Site","2016\/01\/11","07:24:18","07:49:32"],
["Nigel","Wolf","HH41","Att2","140","Bravo","Van","Stage","Away","2016\/02\/17","15:26:06","15:47:03"],
["Andy","Jakes","HH44","Att23","140","Bravo","Van","Deck","Site","2016\/03\/01","14:03:34","14:24:53"]]}

now if i create a var and add the above json data directly then the table populates fine, the trouble i am having is adding the data from the button click back to the table.
my table is as below

var t = $('#Table').DataTable({
                    //"order": [[10, "desc"]],
                    "lengthChange": true,
                    "autoWidth": false,
                    "responsive": true,
                    "processing": true,
                    "language": {
                                        "processing": "Please Wait."
                    },
                        rowCallback: function (row, data) {},
                    "data": [],
                        "columns": [
                                {
                                    "data": "0",
                                    //"defaultContent": ""
                                },
                                {
                                    "data": "1",
                                    //"defaultContent": ""
                                },
                                {
                                    "data": "2",
                                    //"defaultContent": ""
                                },
                                {
                                    "data": "3",
                                    //"defaultContent": ""
                                },
                                {
                                    "data": "4",
                                    //"defaultContent": ""
                                },
                                {
                                    "data": "5",
                                    //"defaultContent": "",
                                },
                                {
                                    "data": "6",
                                    //"defaultContent": ""
                                },
                                {
                                    "data": "7",
                                    //"defaultContent": ""
                                },
                                {
                                    "data": "8",
                                    //"defaultContent": ""
                                },
                                {
                                    "data": "9",
                                    //"defaultContent": ""
                                },
                                {
                                    "data": "10",
                                    //"defaultContent": ""
                                },
                                {
                                    "data": "11",
                                    //"defaultContent": ""
                                }
                            ],
                            retrieve: true
                            

              
          });

Thanks in advance for any help offered.
Kind Regards
Atlas...

Answers

  • AtlasAtlas Posts: 2Questions: 1Answers: 0

    Hi All,
    I have now put the table under the button click and all works fine.

    not sure if its the best solution, all works as expected though.

    Regards

    Atlas.

This discussion has been closed.