Initializing DataTable
Initializing DataTable
Hi Friends,
I am a beginner at this obviously. But the below code initializes the DataTable. My question is where/why in the code would I put a console.log(data) so that I can interrogate the data. I have tried putting console.log(data) in different places in the code and I kept seeing the squiggly lines that said I need a comma. Also I read where you can use a command in the console window, but digging through all of that did not yield me any results I couldn't find the information that shows you the data, number of columns, and rows etc... If there is some literature where I can read all about my question I would certainly appreciate being referred to that as well. Thanks !!!!
$(document).ready(function () {
var table = $('#productTable').DataTable(
{
"ajax": {
"url": "/api/Products/",
"type": "GET",
"dataSrc": "",
"datatype": "json"
},
"columns": [
{
data: null,
className: "dt-center editor-delete",
defaultContent: "<button>Edit</button>",
orderable: false
},
{ "data": "ProductName" },
{ "data": "IntroductionDate" },
{ "data": "Url" },
{
data: null,
className: "dt-center editor-delete",
defaultContent: "<button>Edit</button>",
orderable: false
}
],
"language": {
"emptyTable": "No data found, Please click on <b>Add Product</b> Button"
}
});
This question has accepted answers - jump to:
Answers
The code you posted is an initialization object for Datatables so in general there is no place to use console.log statements. You could use
ajax.dataSrc
as a function and in the function use console.log to output the JSON data. Instead of doing that you can use the browser's network inspector tool to see the XHR response. Steps can be found in this technote.Yes, the Datatables init code is an object. Placing a console.log statement inside will cause the syntax errors you are seeing. Some options allow for functions. You can place console log statements eithin the function.
You can use the
ajax.json()
API to get the JSON response. Open this example and open the console.log and type$('#example').DataTable().ajax.json();
. You should see the JSON data response.Checkout the API docs.
rows().data()
columns()
withcount()
chained, for exampletable.columns().count()
.rows()
withcount()
. Orpage.info()
.All the docs are linked in the menu to the left of the page:
Click the Reference link to read about the Datatables options, API's and events. The examples are a good place for live examples of many options and APIs. The Manual provides lots of information about installation and supported data.
Kevin
Could you show me how you would implement that using my code because its not working for me
We discussed a lot of items above. Can you be more specific of what you want help with?
Kevin
Where would i put the console.log(data) in the initialization of my DataTable? I tried putting it in the dataSrc as you recommended and I got an error about reading length.
Here you go:
http://live.datatables.net/sonesavi/1/edit
Kevin