0

Now the json data has been stored in a variable "msg" in ajax. I can only alert(msg) in the page. But I want to put it into datatable or whatever proper way to see the data with columns in ajax or js.

Here is the json type:

{ "aaData": [ { "ID": "1", "FESTIVAL": "Antipodes Festival", "SUBURB": "Lonsdale Street, Melbourne", "POSTCODE": "3000", "WEBSITE": "http://www.antipodesfestival.com.au/", "DESCRIPTION": "The greek precinct in melbourne cbd will transform into a huge, free street festival with the hosting of the antipodes lonsdale street festival which will hold sway from 14 february 2015 to 15 february 2015." }, { "ID": "5", "FESTIVAL": "Boite Singers Festival", "SUBURB": "Victoria", "POSTCODE": "3000", "WEBSITE": "http://boite.com.au/index.php", "DESCRIPTION": "The boite singers festival brings you four days of vocal inspiration and sheer fun on the second weekend of january each year." } ] } 

3 Answers 3

0

I am not clear about your question but I think you want to display your JSON values as a table!

$(document).ready(function(){
var myjson = { "aaData": [ { "ID": "1", "FESTIVAL": "Antipodes Festival", "SUBURB": "Lonsdale Street, Melbourne", "POSTCODE": "3000", "WEBSITE": "http://www.antipodesfestival.com.au/", "DESCRIPTION": "The greek precinct in melbourne cbd will transform into a huge, free street festival with the hosting of the antipodes lonsdale street festival which will hold sway from 14 february 2015 to 15 february 2015." }, { "ID": "5", "FESTIVAL": "Boite Singers Festival", "SUBURB": "Victoria", "POSTCODE": "3000", "WEBSITE": "http://boite.com.au/index.php", "DESCRIPTION": "The boite singers festival brings you four days of vocal inspiration and sheer fun on the second weekend of january each year." } ] } ;
 console.log(myjson);
 for(i=0;i < myjson.aaData.length;i++){
  var html='';
  $.each(myjson.aaData[i], function(ind,val){
      html +='<td>'+val+'</td>';
  });
  $('#table_id tbody').append('<tr>'+html+'</tr>');
 }
   $('#table_id').DataTable();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="http://cdn.datatables.net/1.10.9/css/jquery.dataTables.css" rel="stylesheet"/>
<script src="http://cdn.datatables.net/1.10.9/js/jquery.dataTables.js"></script>
<table id="table_id" class="display">
    <thead>
        <tr>
          <th>ID</th>
          <th>FESTIVAL</th> 
          <th>SUBURB</th>
          <th>POSTCODE</th>
          <th>WEBSITE</th>
          <th>DESCRIPTION</th>
        </tr>
    </thead>
    <tbody>        
    </tbody>
</table>

I hope this will help you :)

Sign up to request clarification or add additional context in comments.

2 Comments

It gives an error: 0x800a138f - JavaScript runtime error: Unable to get property 'length' of undefined or null reference. Happens in the myjson.aaData.length. Btw, I have added the reference in the page, I do not know why.
I tried your code, it works. The only problem is my data is stored in a variable "msg", And it will change whenever I got data from sql server (But the format is the same). I could not var myjson={...}; I used msg.aaData.length, it will give the erro above. Could you please tell me how to fix it?
0

this is done like this

//html
<table id="example" class="display" width="100%">
</table>

//jquery
$('#example').DataTable( {
    "aaData": data,
    "aoColumns": [
        { "mDataProp": "name" },
        { "mDataProp": "position" },
        { "mDataProp": "office" },
        { "mDataProp": "extn" },
        { "mDataProp": "start_date" },
        { "mDataProp": "salary" }
    ]
} );
//data source

var data= [
{
  "name": "Tiger Nixon",
  "position": "System Architect",
  "salary": "$320,800",
  "start_date": "2011/04/25",
  "office": "Edinburgh",
  "extn": "5421"
},
{
  "name": "Garrett Winters",
  "position": "Accountant",
  "salary": "$170,750",
  "start_date": "2011/07/25",
  "office": "Tokyo",
  "extn": "8422"
}
]

you should refer to this stackoverflow question. and this fiddle

1 Comment

Sorry, my json data is achieved from sql server, I use a variable "msg" to get the content, It could change. But thank you for your answer.
0

This code working to me try this one

 function AgGetDataCtrl() {
             debugger
            var AgServiceData = EmployeeService.AgGetDataSvc();
            AgServiceData.then(function (response) {
                //$scope.totalDisplayed = 20; 
                $scope.Datas = response.data;
                var d = response.data;
                debugger
               
                $(document).ready(function () {
                    var data = [];
                   // var data = [];//.data;

                    for (var i = 0 ; i < d.length; i++) {
                        data.push([  null,
                                    d[i].ECode, d[i].EName, d[i].EmploymentStatus, d[i].Company, d[i].Location,
                                    d[i].Department, d[i].Category, d[i].Designation, d[i].TBand, d[i].Roster,d[i].Shift,
                                    d[i].ForReports, d[i].DOJ, d[i].DOB, d[i].Gender, d[i].Address, d[i].City, d[i].Phone, d[i].Bloodgroup,
                                    d[i].Supervisor1
                            ]);
                    } 
                  var table=  $('#example').DataTable({ 
                        data: data,
                        "columnDefs": [ {
                            "targets": -21,
                            "data": null,
                            "defaultContent": ' <button type="button" class="btn bg-info  btn-xs mr-sm" title="View">  <em class="fa fa-edit fa-fw"></em> </button>'
                        } ],
                        deferRender: false,
                        "scrollY": 200,
                        "scrollX": true,
                        scrollCollapse: false,
                        scroller: false
                    });
                    $('#example tbody').on('click', 'button', function () {
                        var data = table.row($(this).parents('tr')).data();
                        debugger;
                        //alert(data[0] + "'s salary is: " + data[5]);
                        $scope.AgGetDataByIdCtrl(data[1]);
                    });
                    });
               // }
            }, function () {
                $scope.Alertmsg = "Error 1101";
            });
        }

1 Comment

How is this code related to OP's issue? I can't see aaData here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.