views:

835

answers:

6

I have a .net datagrid with some 20 columns. I need a column's visibilty to be toggled based on the click of a button using javascript. Any ideas?

A: 

Probably the most "ASP.NET" way to do it would be to use ASP.NET AJAX. Wrap the datagrid in an UpdatePanel and have the button's server-side code modify the columns of the datagrid.

Randolpho
@Randolpho : Thanks for the qucik answer. But unfortunately i need a pure JS solution
blntechie
In that case, JerSchnied's solution is probably your best bet.
Randolpho
I was worried Solution Yogi's Column Manager wouldn't work with ASP.NET, but the more I look at it, the more convinced I am that it should work. You'll need the ClientId of the DataGrid to pull it off, but that's easy.
Randolpho
A: 

Use jQuery! It's awesome.

Your link can look like this:

<a href="javascript:ToggleColumn();">Toggle My Column</a>

Your javascript function can look like this:

function ToggleColumn()
{
    $(".myColumn").toggle();
}

Each generated in the desired column will have to have the attribute class="myColumn" so your javascript can find it. To do this, you can add an <ItemStyle /> element to the desired column of your DataGrid, like this:

<asp:TemplateColumn runat="server">
    <ItemStyle CssClass="myColumn" />
</asp:TemplateColumn>

And finally, remember to include a link to jQuery in your header file somewhere, like this:

<script src="/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>

You can download jQuery here.

JerSchneid
'column' isn't really an HTML element which you can toggle like that.
SolutionYogi
@SolutionYogi: Sure it is! The jQuery selector is looking for the myColumn class. This code works great! :)
JerSchneid
Not quite. What ASP.NET will do is put the .myColumn class on every TD for that column. So those individual TDs will each get toggled. Ugly as sin, but it should work.
Randolpho
@Randolpho Of course... The state of HTML is that <table>s are described in terms of rows and cells. So if you want to hide a "column" what other choice do you have, other than hiding all of the cells in that column?
JerSchneid
@JerSchneid It can be done. Using colgroup tag in combination with a CSS class. http://www.w3schools.com/tags/tag_colgroup.asp
Josh Stodola
See my answer below!
Josh Stodola
@JerSchneid: actually, Josh Stodola is correct, you should use colgroups under normal circumstances to provide styling for table columns (provided you don't have IE 5 or IE 6 compatibility issues). That said, Datagrids do not emit colgroups, so his answer does not solve the problem stated by the question.
Randolpho
A: 

My personal opinion would be to use JQuery.

Use the selectors to find the column and add a 'hidden:true' to the style. This is a pure JS solution.

Lobut
A: 

Look at this jQuery plugin,

http://p.sohei.org/jquery-plugins/columnmanager/

Incorporate this plugin on your page and call it on the click event of the link.

SolutionYogi
A: 

Use jquery as others suggested...a selector something like this ought to do it. This would essentially hide column 3.

$('table.tableCSS tr').find('td:eq(2)').hide(); // eq(index)
Jab
+5  A: 

You want to use COLGROUP to do this, otherwise you have to apply a style to every cell on every row, which will be terribly inefficient and will likely hang the browser, especially if your grid is large. All of the aforementioned answers that rely on a third-party library (jQuery) are doing it the slow/lazy way. Since all Javascript runs on the client-side, you probably want to have a little more consideration when it comes to efficiency.

Here ya go...

function hideColumns(tableId, colIndexArray) {
  var tbl = document.getElementById(tableId);
  if(!tbl) return;

  var rows = tbl.getElementsByTagName("TBODY");

  if(rows.length == 0)
    rows = tbl.getElementsByTagName("TR");
  else
    rows = rows[0].getElementsByTagName("TR");

  var cols = rows[rows.length - 1].getElementsByTagName("TD");
  var colgroup = document.createElement("COLGROUP");

  for(var i = 0, l = cols.length; i < l; i++) {
    var col = document.createElement("COL");

    for(var num in colIndexArray) {
      if(colIndexArray[num] == i) {
        if(document.all)
          col.style.display = 'none'
        else
          col.style.visibility = 'collapse';

        break;
      }
    }

    colgroup.appendChild(col);
  }

  tbl.insertBefore(colgroup, tbl.childNodes[0]);
}

Use it like this...

var columnsToHide = [0, 1, 2]; // hide the first 3 columns
var tableId = "tableIdHere"; // view the source of your page to get this
hideColumns(tableId, columnsToHide);

Tested in IE7 and FF3: Hide Table Columns Using Javascript

Josh Stodola
Wow a correct answer without the use of JQuery. +1
epascarello
Datagrids do not emit colgroups.
Randolpho
LOL! Yeah, and that's why I am using Javascript to inject them into the DOM. Don't down-vote just because you do not understand!
Josh Stodola
I confess, I didn't read your code, I just complained when I saw "colgroup". When I came to this question, my first thought was "set 'display: none' on the col tag." But I know that a Datagrid won't emit them, so I suggested the UpdatePanel. As I read your code I agree, your solution would work. That said, I still prefer SolutionYogi's ColumnManager option.
Randolpho
Also, I would still recommend you use jQuery or mootools to do your DOM manipulation, if only to rely on their underlying cross-browser compatibility tricks.
Randolpho
Thanks. It's good for the person asking the question to know that it can be done with or without third-party libraries (and understand the benefits/drawbacks of each). Best regards...
Josh Stodola
Doesn't work in Chrome or IE6... This is one of the reasons to use jQuery. Not to mention your function is 20 lines long, instead of 1.
JerSchneid
The experience for the user is much more important than the number of lines of code. You're right it doesn't work in Chrome, but that's because they haven't properly implemented the standard. I challenge you to run your code on a large data grid and then you will realize how worthless it is.
Josh Stodola
And for what it's worth, you need to realize that in order to use jQuery you have to bring in over 4,000 lines of code. Now what's your argument?
Josh Stodola
@Josh: Exactly. First reason i preferred JS was that code behind code was slow like hell as it reloaded the entire grid to do this. Your code is sorta cool. But praying that it works on IE6 as my users are just going to be IE6 and IE7. I will try and let u know on Monday. Thanks again for a plain simple JS code.
blntechie
If it does not work in IE6, I might be able to fix it.
Josh Stodola