SharePoint 2010: Extended Live Filtering For Lists and Libraries

SharePoint Search is working well. But sometimes you do not need a search, you just need a filter function that’s seems like a search. We extended some script from the internet with cookies to send filter values from one page to another. So now you are able to filter a current page by typing word phrases into an input box and if you navigate to another page the filter value is set by the cookie.

Part 1

Therefore we need two Parts. Part 1 is a CEWP which is placed above the list we want to filter. Within this CEWP we place the following code:


<script type="text/javascript">
var minimalCharacters = 0;
function set_cookie(value,days) {
var my_cookie = "P120680";
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else expires = ""
document.cookie = my_cookie+"="+value+expires+"; path=/";
}
function read_cookie(my_cookie) {
var my_cookie_eq = my_cookie + "=";
var ca = document.cookie.split(';');
for(var i=0;i< ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') {
c = c.substring(1,c.length);
}
if (c.indexOf(my_cookie_eq) == 0) {
return c.substring(my_cookie_eq.length,c.length);
}
}
return ""
}
function getElementsByCssClass(sTagName, sClassName) {
var results = new Array();
var allTagElements = document.getElementsByTagName(sTagName);
for (i = 0; i < allTagElements.length; i++) {
if(allTagElements[i].className == sClassName) results.push(allTagElements[i]);
}
return results;
}
function getTxt(obj) {
if(obj.innerText) return obj.innerText;
else return obj.textContent;
}
var lvRows = null;
function quickSearch(term) {
set_cookie(term,"2");
if (term.length >= minimalCharacters){
if(lvRows == null) {
var lvTable = getElementsByCssClass("table","ms-listviewtable")[0];
lvRows = lvTable.childNodes[0].childNodes;
}
for(i = 1; i < lvRows.length; i++) {
if(getTxt(lvRows[i]).toLowerCase().indexOf(term.toLowerCase()) > -1) {
lvRows[i].style.display = "table-row";
}
else lvRows[i].style.display = 'none';
}
}
}
</script>
Live Filter on Current Page:<!-- Change the displayed text here --> <input name="t_in" id="txtINPUT" onkeyup="quickSearch(this.value)" type="text"/>

The value of minimalCharacters declares a minimal amount of characters for starting a search.

Part 2

Within Part 2 we place a CEWP under the list we want to filter. Here we just read the cookie value to filter the page if it is refreshed e.g. by paging.


var cookie = "" + read_cookie("P120680");
document.forms[0].t_in.value = cookie;
quickSearch(cookie);

The result looks like this:

On the first 3 results we filter for “un” and will get Hunter as filter result.

If we click on paging arrows to navigate to the next page the filter is already set to “un” and the list is already filtered.

Thanks to Christian Smierzchalski for helping to figure this out.

..:: I like SharePoint ::..

The article or information provided here represents completely my own personal view & thought. It is recommended to test the content or scripts of the site in the lab, before making use in the production environment & use it completely at your own risk. The articles, scripts, suggestions or tricks published on the site are provided AS-IS with no warranties or guarantees and confers no rights.

Dennis de Vries About Dennis de Vries
Loving SharePoint, Social Media and like to work together with creative people all over the world.