How to get access of a cell's value?

How to get access of a cell's value?

ngungongungo Posts: 64Questions: 23Answers: 2

How do I get value of a cell cellval so I can use it later in the fnRowCallback? Any hint will be appreciated.

    $(document).ready(function() {
        
        var cellval = ...

        var table = $('#cases').DataTable( {
            ajax: 'php/table.cases.php',
            "fnRowCallback": function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
                console.log(cellval);
            },
        });
    });

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin
    Answer ✓

    The data that is used to make up the row is in aData in your above code. So you might use aData[0] if you are using an array data source, or aData.myProp if you are using objects.

    Allan

  • ngungongungo Posts: 64Questions: 23Answers: 2
    edited December 2016

    Thanks Allan!
    How about outside and before the definition of var table?

        $(document).ready(function() {
            ...
            var cellval = ...
            console.log(cellval);
            ...
            var table = $('#cases').DataTable( {
                ajax: 'php/table.cases.php',
                "fnRowCallback": function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
                    ...
                },
            });
        });
    
  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin
    Answer ✓

    Use cell().data() to get the data for a cell.

    Allan

  • ngungongungo Posts: 64Questions: 23Answers: 2

    Hi Allan,

    Your two previous answers help me to understand more about the nuts and bolds of the DataTables, but neither aData nor cell().data() would help since their existences were created by definition of table. In my case, I need to have access of the cells without and before the definition of table. Is there way?

        $(document).ready(function() {
            ...
            var cellval = ...
            console.log(cellval);
            // I need access to the database HERE
            // before the table defined.
            
            ...
            var table = $('#cases').DataTable( {
                ajax: 'php/table.cases.php',
                "fnRowCallback": function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
                    ...
                },
            });
        });
    
  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin

    Sure - just use jQuery - $('#myTable tbody tr td'). You'd probably need to add extra information to the selector, such as the row and column index, but that's not a DataTables issue.

    Allan

This discussion has been closed.