This is a migrated thread and some comments may be shown as answers.

Hide Grid columns at run time

3 Answers 93 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
seetharam
Top achievements
Rank 1
seetharam asked on 20 Jul 2011, 12:44 PM
Hi All,

I am using telerik grid and i am binding model to the telerik grid.
At run time based on the column value i want to hide the column.
Can any one help me on this?

If possible i want to make this at client side using jquery

Thanks in Advance
T.Siva

3 Answers, 1 is accepted

Sort by
0
Paul
Top achievements
Rank 1
answered on 09 Sep 2011, 09:34 AM
HI there
I need to do this also, did you find a way of doing this?
Regards
Paul
0
Nohinn
Top achievements
Rank 1
answered on 09 Sep 2011, 09:59 AM
If you base it on the column value should we assume each row will have the same value?
Anyway to do that you can use this javascript code in the OnDataBound client event of the grid:
function onDataBound(e) {
      $('#' + e.currentTarget.id).find('> .t-grid-content > table > tbody').find('tr').each(function () {
         if ($(this).find("td:contains('YOURTEXTHERE')").length > 0) {
            var index = $(this).find("td:contains('YOURTEXTHERE')").index();
            $('#' + e.currentTarget.id).find('.t-grid-header th:eq(' + index + ')').hide();
            $('#' + e.currentTarget.id).find('> .t-grid-content > table > tbody').find('tr').each(function () {
               $(this).find('td:eq(' + index + ')').hide();
            });
         }
      });
}
Maybe there is a better way to do it, but this does what you're looking for.
0
John DeVight
Top achievements
Rank 1
answered on 09 Sep 2011, 08:42 PM
Hi Paul,

I've written a javascript library that extends the telerik client API.  You can download it at: http://telerikmvcextendedjs.codeplex.com/  I have extended the grid to have a function called hideColumn that will hide a column.  The documentation on it is at: http://www.aspnetwiki.com/page:extending-the-telerik-mvc-client-api#toc4

Here is an example of using it:

MyGrid_OnDataBound=function (e) {
    // If the second column has a cell with the text: SomeValue, then hide the column.
    if(columnHasTextEquals({ column: 1,text: 'SomeValue' })==true) {
        $('#MyGrid').data('tGrid').hideColumn(1);
    }
}
 
columnHasTextEquals=function (o) {
    var hasText=false;
    $($('#MyGrid').data('tGrid').element).find('tbody tr').each(function (idx,tr) {
        if($($(tr).find('td')[o.column]).text()==o.text) {
            hasText=true;
            return;
        }
    });
    return hasText;
}

Let me know if you need additional help with this.

Regards,

John DeVight
Tags
Grid
Asked by
seetharam
Top achievements
Rank 1
Answers by
Paul
Top achievements
Rank 1
Nohinn
Top achievements
Rank 1
John DeVight
Top achievements
Rank 1
Share this question
or