jquery Datatables using JSON

swifty

I am using datatables jQuery plugin to show the data nicely withing a table. I am making an ajax request on a click of a button which is then running a php script returning a JSON.

Here's how my code:

 $('#searchInSugar').button().on('click', function (e) {
                    var searchTxt = $('#searchEntry').val();
                    var moduleName = $('#moduleSelect').val();

                    if (!searchTxt.trim() || searchTxt.length === 0) {
                        alert("Please provide some search text string..");
                        return false;
                    }
                    if (moduleName === "select") {
                        alert("Please select a module..");
                        return false;
                    }
                    $.ajax({
                        type: 'POST',
                        url: "fetch_records.php",
                        data: {"searchText": searchTxt,
                            "module": moduleName},
                        success: function (data) {                            
                            obj = JSON.parse(data);

                            $(document).ready(function () {
                                $('#dialog_entry_table').DataTable({
                                    "info": false,
                                    data: data,
                                 columns: [
                                        {"records": "id"},
                                        {"records": "name"},
                                        {"records": "account_name"}
                                    ]
                                });
                            });
                        },
                        error: function (exception) {
                            alert('Exeption:' + exception);
                        }
                    });
                });

Here's the json that I get from the php script.

 {  
   "next_offset":-1,
   "records":[  
      {  
         "id":"a54e81f8-72b2-ae9b-d526-5608761a28e8",
         "name":"Mr. James Smith",
         "date_modified":"2015-09-27T23:52:29+00:00",
         "account_name":"",
         "_acl":{  
            "fields":{  

            }
         },
         "_module":"Contacts"
      },
      {  
         "id":"b8ec2e0a-ade1-f70f-d722-56098e5c4370",
         "name":"james bond",
         "date_modified":"2015-09-28T22:50:56+00:00",
         "account_name":"",
         "_acl":{  
            "fields":{  

            }
         },
         "_module":"Contacts"
      },
      {  
         "id":"4de93888-155c-7e59-9c4b-56058f1b7ce9",
         "name":"Mr. James Bond",
         "date_modified":"2015-09-28T01:50:49+00:00",
         "account_name":"OSSG",
         "_acl":{  
            "fields":{  

            }
         },
         "_module":"Contacts"
      }
   ]
}

Now, I ONLY WANT TO SHOW id, name and account_name IN THE TABLE, But I am having a hard time achieving this, could someone help/advise what I am doing wrong here.

This is the error I am getting:

enter image description here

madalinivascu

Try:

    var dt = [];
    $.each(data.records,function(i,v) {
    dt.push([v.id,v.name,v.account_name]);
    });
   $('#dialog_entry_table').DataTable({
            "info": false,
             data: dt,
             columns: [
                {"title": "id"},
                {"title": "name"},
                {"title": "account_name"}
            ]
      });

jsfiddle: https://jsfiddle.net/bwqfq2gr/1/

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 Datatables using JSON

From Dev

Sorting jquery DataTables on JSON object property - using Orthogonal data

From Dev

Put JSON Data on JQuery DataTables

From Dev

Using ZeroClipboard with Jquery.DataTables

From Dev

Invalid JSON privilige draw. in AJAX call to Webmethod in ASP.NET webforms using jquery Datatables

From Dev

Spring MVC + jQuery Datatables + JSON Response

From Dev

Display nested JSON data in jquery datatables

From Dev

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

From Dev

jquery datatables 1.10 -check json at first

From Dev

Display nested JSON data in jquery datatables

From Dev

jQuery DataTables POST Server Side Json error

From Dev

jQuery DataTables Ajax GET json formatting

From Dev

Not able to fill data into datatable using Datatables jquery

From Dev

Using jQuery Datatables with non tabular layout

From Dev

Using mRender with two data variables in jquery Datatables?

From Dev

is it possible to add datasets using jQuery DataTables

From Dev

Using DataTables with Bootstrap theme, jQuery $.get function

From Dev

Using jQuery DataTables with Select extension and checkboxes

From Dev

Populating jQuery datatables from mysql using ajax

From Dev

Custom sort durations using jQuery DataTables

From Dev

is it possible to add datasets using jQuery DataTables

From Dev

jquery datatables using jumpToData() getting undefined error

From Dev

Custom sort durations using jQuery DataTables

From Dev

unable to add row in DataTable using jquery datatables

From Dev

DataTables in ASP.net using JSON

From Dev

How load json data on table using datatables?

From Dev

Get data in json using javascript and datatables

From Dev

Datatables(jquery plugin) not populating table with AJAX, valid JSON being returned

From Dev

Create Python code to supply json data for jQuery datatables

Related Related

  1. 1

    jquery Datatables using JSON

  2. 2

    Sorting jquery DataTables on JSON object property - using Orthogonal data

  3. 3

    Put JSON Data on JQuery DataTables

  4. 4

    Using ZeroClipboard with Jquery.DataTables

  5. 5

    Invalid JSON privilige draw. in AJAX call to Webmethod in ASP.NET webforms using jquery Datatables

  6. 6

    Spring MVC + jQuery Datatables + JSON Response

  7. 7

    Display nested JSON data in jquery datatables

  8. 8

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

  9. 9

    jquery datatables 1.10 -check json at first

  10. 10

    Display nested JSON data in jquery datatables

  11. 11

    jQuery DataTables POST Server Side Json error

  12. 12

    jQuery DataTables Ajax GET json formatting

  13. 13

    Not able to fill data into datatable using Datatables jquery

  14. 14

    Using jQuery Datatables with non tabular layout

  15. 15

    Using mRender with two data variables in jquery Datatables?

  16. 16

    is it possible to add datasets using jQuery DataTables

  17. 17

    Using DataTables with Bootstrap theme, jQuery $.get function

  18. 18

    Using jQuery DataTables with Select extension and checkboxes

  19. 19

    Populating jQuery datatables from mysql using ajax

  20. 20

    Custom sort durations using jQuery DataTables

  21. 21

    is it possible to add datasets using jQuery DataTables

  22. 22

    jquery datatables using jumpToData() getting undefined error

  23. 23

    Custom sort durations using jQuery DataTables

  24. 24

    unable to add row in DataTable using jquery datatables

  25. 25

    DataTables in ASP.net using JSON

  26. 26

    How load json data on table using datatables?

  27. 27

    Get data in json using javascript and datatables

  28. 28

    Datatables(jquery plugin) not populating table with AJAX, valid JSON being returned

  29. 29

    Create Python code to supply json data for jQuery datatables

HotTag

Archive