Pick what columns to show

Pick what columns to show

ChrilleChrille Posts: 19Questions: 0Answers: 0
edited July 2010 in General
I want to make my users able to pick what columns to show, and hide all others. You know like the icon to the right in most table-based software like Thunderbird/office.

I think I can solve it with hidden columns example, but just wanted to make sure that this function (or plugin) doesn't already exist?

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    An example of how it can be done: http://datatables.net/examples/api/show_hide.html

    There isn't a plug in as such, but a simple call to the API function from whatever UI widget you want to use will do the trick.

    Allan
  • ChrilleChrille Posts: 19Questions: 0Answers: 0
    edited July 2010
    Thanks! I cannot make it work... can you see what I'm doing wrong?

    I have a checkbox with class toggleColumn for each column. This function is called whenever the box is changed:
    [code]
    function updateColumns()
    {
    $('input.toggleColumn').each(function() {
    var col = $(this).val();
    var show = $(this).is(":checked");
    var bVis = oTable.fnSettings().aoColumns[col].bVisible;

    if(show && !bVis)
    oTable.fnSetColumnVis(col, true);
    else if(!show && bVis)
    oTable.fnSetColumnVis(col, false);

    });
    }
    [/code]

    This gives me error message "nTd is undefined"
  • ChrilleChrille Posts: 19Questions: 0Answers: 0
    Anybody?
  • ChrilleChrille Posts: 19Questions: 0Answers: 0
    Got it. The value is not an integer, it works after typecasting:
    [code]
    var col = parseInt($(this).val());
    [/code]
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Thanks for the update - useful knowledge this. One day I'll get around to making a 'debug' version of DataTables which should pick this kind of thing up...

    Allan
  • giorgio79giorgio79 Posts: 43Questions: 0Answers: 0
    Hi Chrille,

    Could you post the checkbox (or select box? ) code as well?

    Looking for exactly this.

    cheers,
    G
  • ChrilleChrille Posts: 19Questions: 0Answers: 0
    Sure:

    [code]
    function updateColumns()
    {
    if($('table#example').length>0)
    {
    var colsToHide=new Array(20);
    var cookiestr = "";
    $('input.toggleColumn').each(function() {
    var col = parseInt($(this).val());
    var show = $(this).is(":checked");


    if(show) cookiestr = cookiestr + col + ",";

    oTable.fnSetColumnVis(col, show);
    });

    cookiestr = cookiestr.substr(0,(cookiestr.length-1));

    var date = new Date();
    date.setTime(date.getTime() + (3600 * 24 * 1000));
    $.cookie("selectedcols", cookiestr, { path: '/', expires: date });
    }
    }
    [/code]
  • giorgio79giorgio79 Posts: 43Questions: 0Answers: 0
    edited September 2010
    Much appreciated Chrille. Would you care to share the HTML Input element that calls updateColumns? :) Do you have maybe x number of checkboxes? That way we could test it straight with a copy and paste.
    [code]
    Show/hide columns here.
    Col1:


    Col2:


    Col3:


    [/code]
  • ChrilleChrille Posts: 19Questions: 0Answers: 0
    Just create a checkbox with class toggleColumn. The value of the checkbox must be the column number
  • giorgio79giorgio79 Posts: 43Questions: 0Answers: 0
    Thanks tried but no luck. I did successfuly convert though Allan's show hide links to checkboxes like this

    [code]

    Show / Hide Columns:
    Service
    Title




    (Does not Work)
    Show / Hide Columns2:
    Service
    Title
    [/code]
  • ChrilleChrille Posts: 19Questions: 0Answers: 0
    You have forgotten the class on the boxes, and the onclick function is not needed
  • giorgio79giorgio79 Posts: 43Questions: 0Answers: 0
    Thanks, I corrected it like this, but no luck. I guess I will stick with the current API show hide, but thanks for your time and help.

    HTML
    [code]
    Show / Hide Columns2:
    Service
    Title
    [/code]
    JS
    [code]
    function updateColumns()
    {
    if($('#MYTABLEID').length>0)
    {
    var colsToHide=new Array(20);
    var cookiestr = "";
    $('input.toggleColumn').each(function() {
    var col = parseInt($(this).val());
    var show = $(this).is(":checked");


    if(show) cookiestr = cookiestr + col + ",";

    oTable.fnSetColumnVis(col, show);
    });

    cookiestr = cookiestr.substr(0,(cookiestr.length-1));

    var date = new Date();
    date.setTime(date.getTime() + (3600 * 24 * 1000));
    $.cookie("selectedcols", cookiestr, { path: '/', expires: date });
    }
    }
    [/code]
This discussion has been closed.