Getting the row api to work

Getting the row api to work

dbisignanidbisignani Posts: 1Questions: 1Answers: 0

Hi everyone,

I am having a tough time getting the datatables.js api working. Specifically, I am working with a datatable and I would like to get the <td> nodes of a row. To do this, I have the following code in an onclick event handler:

       $("#IndustrySummary").on("click", "a", function (evt) {
            evt.preventDefault();
            var table = $('#IndustrySummary').DataTable();
            **var rowIdx = table.row(this).index().row;  ** //This line throws an error
            var tds = table.rows(rowIdx).nodes();

            var tds2 = $(this).parent().siblings();
            alert("clicked!")
        })

which I copied from this link:

https://datatables.net/reference/api/cell().index()

The chained statement shown in bold face throws the following error:
SCRIPT5007: Unable to get property 'row' of undefined or null reference

Here is the basic structure of my table (actual names changed for security purposes)

            $('#IndustrySummary').DataTable({
                data: data.SPVIndustryInfoesSummary,
                columnDefs: [
                    { title: 'One', targets: [4] },
                    { title: 'Two', targets: [5] },
                    { title: 'Three', targets: [6] },
                    { title: 'Four', targets: [7] },
                    { title: 'Count', targets: [8] },
                    { visible: false, targets: [1, 2] },
                ],
                columns: [
                    { data: "One" },
                    { data: "Two" },
                    { data: "Three" },
                    { data: "Four" },
                    { data: "Five" },
                    { data: "Six" },
                    { data: "Seven" },
                    { data: "Eight" },
                    { data: "Count" },
                    { data: null, defaultContent: '<a href="">View</a>' } 
                ],
            })

Any advice would be helpful. Thank you for your time and assistance!

Answers

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947
    edited October 2018

    You have this:

    var rowIdx = table.row(this).index().row;

    But the example in the doc you linked to has this:

    var rowIdx = table
    .cell( this )
    .index().row;

    The difference is the row().index() will return the row ID as an integer so you don't want the .row at the end. Where cell().index() returns an object which one of the properties is row. The .row at the end of the chain returns the value (row index) of the row property.

    Kevin

This discussion has been closed.