aoColumns When number of columns vary

aoColumns When number of columns vary

NotaceNotace Posts: 21Questions: 0Answers: 0
edited September 2012 in General
I have a page which will have a different number of columns, depending on the access rights of the user. There will either be 6 or 4 columns in the table. If there are 6, then this initialisation works fine.
[code]
jQuery(document).ready(function() {
oTable = jQuery('#rate').dataTable( { // Initialise the Open Quote Table
"sScrollY": "540px", // Set scrollable area to 300 pixels
"bScrollCollapse": true, // Shrink the table if only a few rows displayed
"bPaginate": false, // Turn off pagination furniture (turning it on breaks Scrolling)
"bAutoWidth": true, // Let Datatable manage the column widths
"bSort": true, // Turn on Sorting for all columna
"bInfo": false, // Turn off "Showing x to y of nn entries" element
"aaSorting": [[ 0, "desc" ]], // By default, sort by Quote No. Descending
"oLanguage": {"sEmptyTable": "No Sales Activities found for selected year.", // Change the empty table text
"sSearch": "Search:"}, // Change Text for Filter box
"aoColumns": [
null,
{ "bSortable": false },
{ "bSortable": false },
{ "bSortable": false },
{ "bSortable": false },
{ "bSortable": false }
]
} );
[/code]
However, if I am only displaying 4 columns, it obviously breaks the table.

I gather from trolling through the discussions, that the number of columns can be retrieved from

oSettings.aoColumns.length

but I don't know if, or how, I can make use of that to adjust the initialisation depending on how many columns I am displaying. If this is possible, how should I alter the initialisation? I am by no means a jQuery or Javascript expert, so you may have to use words of one syllable for me to understand. Thanks.

Ron

Replies

  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin
    I'd suggest you use aoColumnDefs - this option was introduced for the case where there are a variable number of columns - you can assign column definitions by class, index or globally. For example:

    [code]
    aoColumnDefs: [
    { bSortable: true, aTargets: [ 0 ] },
    { bSortable: false, aTargets: [ '_all' ] }
    ]
    [/code]

    will disable sorting on all columns, except the first one: http://datatables.net/usage/columns .

    Allan
  • NotaceNotace Posts: 21Questions: 0Answers: 0
    Thanks Allan. Yes that works nicely. I used

    [code]

    "aoColumnDefs": [
    {"aTargets": [ 0 ], "bSortable": true },
    {"aTargets": [ -3 ], "bSortable": true },
    { "aTargets": [ '_all' ], "bSortable": false }
    ]
    [/code]

    To allow sorting on the first and third last columns, and not allowing the others to be used for sorting. I have noticed a glitch, however. When defining more than one column as sortable, only the first column has the sort arrows shown. If you click on one of the other sortable columns, the arrows are shown for that column, but are removed from the other column. So, only one column at a time displays the sort arrows. (I am using DT 1.7.5). Thanks
  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin
    Looks like that is also present in the latest 1.9.4 :-(. I'll look into it - thanks for flagging that up.

    Allan
  • NotaceNotace Posts: 21Questions: 0Answers: 0
    Don't be too hard on yourself. Datatables is AWESOME, as I'm sure many delighted users have told you. :)
  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin
    Heh - thanks! Still frustrating to find a new bug though!

    Allan
  • sreejithsreejith Posts: 2Questions: 0Answers: 0
    Hi Allan,

    Am using the latest version 1.9.4. When I use following code my sorting arrow is not showing by default.

    [code] "aoColumnDefs": [{ "bSortable": true, "aTargets": [ 0 ] },{ "bSortable": false, "aTargets": [ '_all' ] }][/code]

    Where I was wrong?
  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin
    Don't know - please post a link to a test case: http://datatables.net/forums/discussion/12899/post-test-cases-when-asking-for-help-please-read

    Allan
  • NotaceNotace Posts: 21Questions: 0Answers: 0
    Hi Allan.

    Have you had a chance to look at the issue of the sort arrows not being displayed on sortable columns which are not currently selected for sort? This is not a major issue, but the arrows provide a useful cue for users. Thanks.

    PS Happy New Year
  • NotaceNotace Posts: 21Questions: 0Answers: 0
    I've been having a play with this, and I think I have a workaround to the sort arrows issue. This original coding:

    [code]
    "aoColumnDefs": [
    {"aTargets": [ 0,1 ], "bSortable": true },
    {"aTargets": [ '_all' ], "bSortable": false }
    ]

    with a table definition of:
    Pgm Year
    GST Rate AU
    GST Rate NZ
    AU/NZ Exchg Rate
    Edit
    Delete
    [/code]

    Results in the sort arrows only being displayed for the currently sorted columns. However, if you change it to:

    [code]
    "aoColumnDefs": [
    {"aTargets": [ "sortme"], "bSortable": true },
    {"aTargets": [ 'nosort' ], "bSortable": false }
    ]
    :
    :
    Pgm Year
    GST Rate AU
    GST Rate NZ
    AU/NZ Exchg Rate
    Edit
    Delete
    [/code]

    The sort arrows are displayed correctly.

    Hope this helps.
This discussion has been closed.