| | Stumble It! | Add to Mixx! | | diigo it | | Slashdot |

Friday, May 21, 2010

Hide the Filtered Column on a SharePoint List Web Part

Have you ever included a filtered list on one of your SharePoint pages and been incredibly annoyed that you had to show the value that you were filtering on? Yeah, me too.

Here's what I did to fix it:
  1. Add a Content Editor Web Part to your page. Anywhere will do.
  2. In the 'Source Editor...' for that CEWP, paste in this code:
    <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script>
    <script type='text/javascript'>

    $(document).ready(function () {
    $('iframe[title="Hidden frame to filter list"]').each(function() {
    var tbl = ($.browser.msie) ? $(this).parent().parent().parent().parent() : $(this).next();
    var rows = tbl.children('tbody').eq(0).children('tr');
    var cols = rows.eq(0).children('th').length;
    var filter_col = '';

    for (var c=0;c<cols;c++) {
    if (rows.eq(0).children('th').eq(c).html().match('/_layouts/images/filter.gif')) {
    filter_col = c;
    }
    }

    if (filter_col > -1) {
    for (var r=0;r<rows.length;r++) {
    rows.eq(r).children('th, td').eq(filter_col).css('display','none');
    }
    }

    });
    });

    </script>
  3. The code works by detecting the little filter icon and hiding that column from the list.
  4. Yay jQuery!
Enjoy!

5 comments:

WCDulanyJr said...

What if you have multiple web parts on the page? I added your code to a page and it only worked on the first web part that it found. The second one still showed the filter column.

It is interesting that they both have the same Title. I expected .each() to iterate through all iFrames on the page, but it seems to just find the first one on the page and does not move to the next web part. Odd.

ranjith said...

Excellent work...This is exactly what I needed.

Philip said...

This is perfect. I have been needing something like this for a long time. I uploaded the jquery file to a SharePoint document library so that I didn't get the IE message about an unsecure site reference and changed the code to reference the SharePoint document.

Julien Furmann said...

Thanks for this code, but like WCDulanyJr, I have multiple web parts which are filtered. What should I do to hide filtered columns in all web parts ?
Thx

Julien Furmann said...

In fact this code is not working when there is a group by. Do you know how to improve here ?