marak
marak

Reputation: 270

How to Insert JSON data into DataTable

I am new to Javascript and I am trying to dynamically load Json data into datatable on a button click.

My Json data is in below format

     [{"DeviceName":"AND1","IPAddress":"10.10.12.1221"},   {"DeviceName":"AND2","IPAddress":"10.10.12.1222"},{"DeviceName":"AND3","IPAddress":"10.10.12.1223"}]

Here is my complete Html code: When I am running this code, I am getting an UncaughtType error in processDeviceDataResults at ('#deviceTable'). But, I am pretty sure this is not the way you to load data in to the datatable.

   

                      
            //Set the hubs URL for the connection
            var url = 'http://localhost:8080/signalr';

            var connection = $.hubConnection(url);

            // Declare a proxy to reference the hub.
            var hubProxy = connection.createHubProxy('HubClass');

            hubProxy.on('DeviceDataResults', processDeviceDataResults);

            connection.start().done(function() {
                $("#GetDeviceData").click(function() {
                    hubProxy.invoke('GetDeviceData');
                });
            });

            function processDeviceDataResults(results) {                 
                $('#deviceTable').dataTable({
                    "aodata": results
                });
        }
        
       
      
 

Upvotes: 3

Views: 4472

Answers (5)

Jifho
Jifho

Reputation: 67

marak the data is loaded in other file via ajax

ajax.php

  <?php

//in ajax.php get json data
//Here you can create a function that returns the data
$response='
{
    "data": [
        {
            "DeviceName": "AND1",
            "IPAddress": "10.10.12.1221"
        },
        {
            "DeviceName": "AND2",
            "IPAddress": "10.10.12.1222"
        },
        {
            "DeviceName": "AND3",
            "IPAddress": "10.10.12.1223"
        }
    ]
}';

echo $response;
?>

dataTableJson.php

    <!DOCTYPE >
  <html>
  <head>
    <title>Data Table Json</title>

      <link rel="stylesheet" href="http://cdn.datatables.net/1.10.2/css/jquery.dataTables.min.css" />
      <link rel="stylesheet" href="http://cdn.datatables.net/autofill/1.2.1/css/dataTables.autoFill.css" />


      <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.js"></script>
      <script type="text/javascript" src="http://cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js"></script>

  <script>

    $(document).ready(function() {

      $('#deviceTable').dataTable({
          "ajax": "ajax.php",
          "columns": [
            { "data": "DeviceName" },
            { "data": "IPAddress" }
        ]
      });
    });
  </script>

</head>
<body>
  <table id="deviceTable" class="hover" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>Device</th>
                <th>IP</th>
            </tr>
        </thead> 

        <tfoot>
            <tr>
                <th>Device</th>
                <th>IP</th>
            </tr>
        <tfoot>
    </table>
</body>
</html>

Upvotes: 1

marak
marak

Reputation: 270

Yes Venkata they are referenced in the Head tags. This is the order in which they are referenced

src="text/javascript" src="Scripts/jquery-1.6.4.js"

src="//cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js"

href="https://cdn.datatables.net/1.10.2/css/jquery.dataTables.min.css"

href="https://cdn.datatables.net/autofill/1.2.1/css/dataTables.autoFill.css"

src="https://code.jquery.com/jquery-1.11.1.js"

src="Scripts/jquery.signalR-2.1.2.js"

Upvotes: 0

marak
marak

Reputation: 270

Venkata, Its not even getting until the change you mentioned, its getting an "Uncaught TypeError: Undefined is not a function on $('#deviceTable').dataTable line

Upvotes: 0

marak
marak

Reputation: 270

Jifho, Thanks for your response. I formatted my JSON data as you suggested and I am getting an "Uncaught TypeError: Undefined is not a function on $('#deviceTable').dataTable line. I have a table defined in my html body as


       $(document).ready(function () {

             var url = 'http://localhost:8080/signalr';

             var connection = $.hubConnection(url);

             // Declare a proxy to reference the hub.
             var hubProxy = connection.createHubProxy('HubClass');

             hubProxy.on('DeviceDataResults',   processDeviceDataResults);

             connection.start().done(function () {
                 $("#GetDeviceData").click(function () {


                     hubProxy.invoke('GetDeviceData');
                 });


             });

             function processDeviceDataResults(results) {

                 $('#deviceTable').dataTable({
                     "ajax": results,
                     "columns": [
                         { "data": "DeviceName" },
                         { "data": "IPAddress" }
                     ]
                 });
             }
         });
       

my JSON results:

     {"data":[{"DeviceName":"AND1","IPAddress":"10.10.12.1221"},{"DeviceName":"AND2","IPAddress":"10.10.12.1222"},{"DeviceName":"AND3","IPAddress":"10.10.12.1223"}]}

Upvotes: 1

Jifho
Jifho

Reputation: 67

Try this

data.json

    {
    "data": [
        {
            "DeviceName": "AND1",
            "IPAddress": "10.10.12.1221"
        },
        {
            "DeviceName": "AND2",
            "IPAddress": "10.10.12.1222"
        },
        {
            "DeviceName": "AND3",
            "IPAddress": "10.10.12.1223"
        }
    ]
}

js

  $('#deviceTable').dataTable({
      "ajax": "data.json",
      "columns": [
        { "data": "DeviceName" },
        { "data": "IPAddress" }
    ]
  });

example here http://www.wishesafterlive.com/stackoverflow/dataTableJson.php

Upvotes: 1

Related Questions