Select a column not working

Select a column not working

hawkmasterhawkmaster Posts: 56Questions: 20Answers: 0

Hello
In the example I found a code to get the column data if clicked on
http://datatables.net/reference/type/column-selector

Get the data for a column that was clicked on:

I tried the code but I always have the error "TypeError: table.columns is not a function"

What is wrong?

table = $('#projectdata').dataTable( {
columnDefs: [
{ visible: false, targets: 1 }
]
} );
$('#projectdata tbody').on( 'click', 'td', function () {
var columnData = table.columns( $(this).index()+':visIdx' ).data();

alert(columnData);
} );

regards
hawk

Answers

  • rhinorhino Posts: 80Questions: 2Answers: 17
    edited July 2014

    You need to capitalize the the 'd' of your dataTable() call:

    //table = $('#projectdata').dataTable( { columnDefs: [ { visible: false, targets: 1 } ] } ); 
    table = $('#projectdata').DataTable( { columnDefs: [ { visible: false, targets: 1 } ] } );
    
    $('#projectdata tbody').on( 'click', 'td', function () {
        var columnData = table.columns( $(this).index()+':visIdx' ).data();
        alert(columnData); 
    } );
    

    And here is why, from the api reference page:

    It is important to note the difference between $( selector ).DataTable() and$( selector ).dataTable(). The former returns a DataTables API instance, while the latter returns a jQuery object.

This discussion has been closed.