Counting Columns in a table

Counting Columns in a table

akreiderakreider Posts: 33Questions: 0Answers: 0
edited September 2009 in General
How can I count how many columns are in a table? Is there a DataTables method for that?

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi akreider,

    There isn't a method specifically for this, but you can access the required property:

    [code]
    var iColumns = oTable.fnSettings().aoColumns.length;
    [/code]
    The DataTables settings object contains internal information, and hence is undocumented on this site, but there is a wealth of information in the source code if you need anything more than just the number of columns.

    The other option is to query the DOM directly:

    [code]
    var iColumns = $('#example thead th').length;
    [/code]
    Regards,
    Allan
  • akreiderakreider Posts: 33Questions: 0Answers: 0
    I should have thought of that solution. Thanks!

    Aaron
  • akreiderakreider Posts: 33Questions: 0Answers: 0
    For some reason, Datatables says that this table below has 4 columns (oTable.fnSettings().aoColumns.length;) when it only has 3.

    Any ideas?

    aoColumns has a fourth column, eg aoColumns[3], that has a blank name and a width of 6 pixels.




    Group
    St.
    ttt
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi akreider,

    Odd - it works okay for me:

    [code]
    $(document).ready(function() {
    var oTable = $('#example').dataTable( );
    console.log( oTable.fnSettings().aoColumns.length );
    } );
    [/code]
    Gives a length of 3. Have you initialised your table with bStateSave and altered the number of columns - that is showing a few problems in 1.5.2?

    Thanks,
    Allan
  • akreiderakreider Posts: 33Questions: 0Answers: 0
    edited September 2009
    I'm unsure what is going on.

    This example doesn't work:
    http://www.energyjustice.net/map-test/test.html

    When I add an extra column, then this example works:
    http://www.energyjustice.net/map-test/test2.html

    The only difference is an extra in the only column row.


    I've tried making tables with 3 or fewer columns, and they don't work.
    Could there be a problem with tables that only have three (or fewer) columns?
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi akreider,

    Your pages are both rendering in quirks mode - that in combination with the fact that neither page validates ( http://validator.w3.org/ ) will mean that the browser can do some very weird things. I've tried your table code from the first link in a validating page (strict HTML 4) and it works no problem - so I suspect if you add a doctype to the pages, it will work as expected - you might also need to fix the HTML errors.

    Regards,
    Allan
  • akreiderakreider Posts: 33Questions: 0Answers: 0
    I got it to validate in html strict, but it still won't work.

    http://www.energyjustice.net/map-test/test.html

    The html validates. The CSS has many errors.

    In Firebug, I'm getting "sData is undefined" - line 84 of the jQuery.datatables.min.js file

    I guess I'll try removing all the other scripts (except jquery) in case they are causing problems.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi akreider,

    I totally can't explain that :-). I would imagine it must be an interaction with one of the other script, since I can get your basic HTML table to work for me no problems at all. So yup - I agree - removing the other scripts is the way to go.

    Regards,
    Allan
  • akreiderakreider Posts: 33Questions: 0Answers: 0
    Hmm, this page still has the same trouble. Got rid of most of the scripts (except for my main one). I got the CSS to validate as well.

    http://www.energyjustice.net/map-test/test4.html
  • akreiderakreider Posts: 33Questions: 0Answers: 0
    Oops! It's in the datatable definition statement. I define it with four columns! So problem solved!

    $("#sub-facility").dataTable({"iDisplayLength":20,
    "bLengthChange":false,
    [{"sType":"html"}, null, null, null]});
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Ah ha - that would do it :-). Good one.

    Allan
This discussion has been closed.