identical data in column

identical data in column

blagblag Posts: 18Questions: 0Answers: 0
edited January 2012 in DataTables 1.8
Please forgive me, this may be a common question by a newbie - if so, I apologise for my ignorance!!

My tables are derived from a database, via PHP and DataTables is being tested for use to enable sorting of the tables that are pulled from the server. It is not uncommon for me to find that one or more of the columns in the tables contain the same data in each cell. Thus in a table containing x rows in the body, the column headed 'Colour' may (sometimes) only contain 'Brown' in x cells or the Column headed 'Price' may (occasionally) only contain '2.99' in x cells.

Where this data differs within a column, having the facility to sort is useful, but unnecessary where all cells in a column (except the header, of course) contain identical data. Can anyone suggest how I disable the sort according to the contents of the column?

Replies

  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin
    So basically if one column contains all the same data, you want to disable sorting on it? Currently there isn't a built in way in DataTables to do this - what you would need to do is pre-parse the data, find the columns which match your criteria and then set the bSortable flag for the columns as needed.

    Allan
  • blagblag Posts: 18Questions: 0Answers: 0
    Allan
    Thanks for the response. My Javascript is not good, and although I have been doing some stuff in JQuery, my skill is 'basic'.
    Via PHP I can determine at the time the page is created, which columns are 'sortable' and I can even add the class 'sorting' to each 'sortable' column's header (th), but if I do this, the DataTables script overrrides those set by PHP. I'd appreciate some guidance on how to implement this.
  • blagblag Posts: 18Questions: 0Answers: 0
    Here's where I've got to. I created a little javascript routine to look at the column header () classes, and from this I create a list e.g '0,1,2,4,5' of columns whose sorting is to be disabled by default.
    The values in this list can change depending on the PHP-defined class attribute/property.
    I am then trying to use this javascript array to 'force' the sortable status at intialisation by TableData

    here is my clumsy code
    [code]//PHP assigns the class 'sorting_disabled' to each table header where the column's td cells are identical
    $(function(){if($('#tbl>tbody>tr').length>1){
    //checks to see if there is more than one row in the body of the table
    var n='',lg=$('#tbl th'),cols=lg.length-1,i=0;
    $('#tbl th').each(function(index){
    //checks each of the table's headers
    if($(this).hasClass('sorting_disabled')){
    n+=index;
    if(i
  • blagblag Posts: 18Questions: 0Answers: 0
    Eureka!!

    Finally, this works:

    [code]
    $(function(){
    if($('#tbl>tbody>tr').length>1)
    {var n=new Array(),j=0;
    $('#tbl th')
    .each(function(index){
    if($(this).hasClass('sorting_disabled')){
    n[j]=index;j++;}
    });

    $('#tbl').dataTable({"aaSorting":[],"bPaginate":false,
    "aoColumnDefs":[{"bSortable":false,"aTargets":
    n.slice()
    }],
    "bFilter":false,"bInfo":false,"bAutoWidth":false,"bLengthChange":false});}
    ;}
    );
    [/code]

    n.slice(), without the square brackets '[' and ']', passes the array as the value of 'aTargets'.

    If this can be simplified I'd appreciate advice!!
This discussion has been closed.