3

see my script. why no data is displayed? I try many thing here https://datatables.net/manual/ajax#Data-array-location but i need help to display my json data to the datatable

$(document).ready(function() {

var json = '{"company_id":"1","company_name":"schneider"}';

    $('#example').DataTable( {
	    "data": JSON.parse(json),
        "columns": [
            { "data": "company_id" },
			{ "data": "company_name"}
        ]
    } );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>

<table id="example" class="display" style="width:100%">
	<thead>
		<tr>
			<th>C_ID</th>
			<th>C_NAME</th>
		</tr>
	</thead>
</table>

1 Answer 1

2

The data parameter accepts an array not an individual object.

You JSON should look like this:

var json = '[{"company_id":"1","company_name":"schneider"}]';

Here's a JSFiddle

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

1 Comment

thanks this id what i do now var myarr = Array(); myarr.push(JSON.parse(json)); $('#example').DataTable( { "data": myarr,

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.