3

I use the SharePoint List Filter web part on a page to control several Data View Web Parts.

How do I read the current value of the List Filter in Javascript?

3 Answers 3

8

SharePoint List Filter web part is rendered as input text field for displaying filter value:

<input  type="text" readonly="readonly" id="{ClientID}_SpListPicker_SelectionBox" title="Selected values" >

and input hidden field for storing filter value:

<input type="hidden"  id="{ClientID}_SpListPicker_Selection" value="{FilterValue}">

Below are demonstrated several ways how to retrieve filter value on the client side.

Retrieve filter value from input hidden field (jQuery dependency)

function getListFilterValueFromControl()
{
    var j = jQuery.noConflict();
    var filter = j("input[id$='SpListPicker_Selection']");
    return filter.val();
}

Retrieve filter value from context info

ContextInfo represents List Info structure on the client side. Property overrideFilterQstring contains filter string in the following format:

FilterField1={FilterName1}&FilterValue1={FilterValue1}..

function getListFilterValueFromContextInfo()
{
    var filterFields = ctx.overrideFilterQstring.split('&');   //
    if(filterFields.length > 1)
    {
       return filterFields[1].split('=')[1];
    }
    return null;  
}

Usage

ExecuteOrDelayUntilScriptLoaded(getListFilterValueFromControl, 'SP.js');
0
1

And if you have multiple list filter webparts in your page, here is how to access the value of each of them

$("div.ms-WPBody div div:eq(0) span input").val();  

for your 1st webpart

$("div.ms-WPBody div div:eq(1) span input").val(); 

for your second webpart, and so on

0

Another method I just found after more research:

$(document).ready(function() {
    var filterValue = $("div.ms-WPBody div div span input").val();
    if (filterValue != null) {
        alert("filter value is: "+$("div.ms-WPBody div div span"));
    } 
});

vgrem provided a more beautiful version though ;)

Source: http://social.msdn.microsoft.com/Forums/br/sharepointdevelopment/thread/60e6b32d-86a7-4ddc-8f7d-800b58ceb3f9

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.