| | 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!

Friday, May 14, 2010

Display a SharePoint List from a Parent Site

This bit of code will let you take a list from a parent site in your SharePoint hierarchy and display it in your sub-site. You may have to tweak the code a bit to match your specifications if you have custom code in your list templates, but all you *should* have to mess with is the '#WebPartWPQ2' bit.

Anyway, here it is =)
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script>
<script type='text/javascript'>

function get_list() {
var list = $('#list').contents().find('#WebPartWPQ2').children('table').eq(1);
$('#my_list').html(list.html());
}

</script>
<div id='my_list'></div>
<iframe onLoad='get_list()' id='list' style='display:none;' src='http://path/to/your/list/AllItems.aspx'></iframe

And if you don't like how the list is displayed and you want to remove some of the columns, use this code:
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script>
<script type='text/javascript'>

function get_list() {
var list = $('#list').contents().find('#WebPartWPQ2').children('table').eq(1);
var rows = list.find('table').eq(0).children('tbody').eq(0).children('tr');
var hide_cols = ['2','3','4','5']; // columns number 2, 3, 4, & 5 will be hidden. Index is 0
for (var r=0;r<rows.length;r++) {
for (var c=0;c<hide_cols.length;c++) {
rows[r].childNodes[hide_cols[c]].style.display = 'none';
}
}
$('#my_list').html(list.html());
}

</script>
<div id='my_list'></div>
<iframe onLoad='get_list()' id='list' style='display:none;' src='http://path/to/your/list/AllItems.aspx'></iframe
Just be sure to replace the column numbers with the columns you want to hide.

Cheers!