Initializing DataTable

Initializing DataTable

herculeshercules Posts: 21Questions: 9Answers: 0

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

  • kthorngrenkthorngren Posts: 21,324Questions: 26Answers: 4,949
    edited September 2021 Answer ✓

    My question is where/why in the code would I put a console.log(data) so that I can interrogate the data

    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.

    I kept seeing the squiggly lines that said I need a comma.

    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.

    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,

    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.

    I couldn't find the information that shows you the data, number of columns, and rows etc

    Checkout the API docs.

    data

    rows().data()

    number of columns

    columns() with count() chained, for example table.columns().count().

    and rows

    rows() with count(). Or page.info().

    If there is some literature where I can read all about my question I would certainly appreciate being referred to that as well

    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

  • herculeshercules Posts: 21Questions: 9Answers: 0

    Could you show me how you would implement that using my code because its not working for me

  • kthorngrenkthorngren Posts: 21,324Questions: 26Answers: 4,949
    Answer ✓

    We discussed a lot of items above. Can you be more specific of what you want help with?

    Kevin

  • herculeshercules Posts: 21Questions: 9Answers: 0

    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.

  • kthorngrenkthorngren Posts: 21,324Questions: 26Answers: 4,949
    Answer ✓
Sign In or Register to comment.