1

I'm trying to dynamically filter the records that a Data View WebPart displays based on the return value of a javascript query. I would like some advice as to how I should do this or if it's even possible.

Basically, each user who logs into my company's SharePoint site has a "Department" number in their User Information. My javascript function successfully returns that value, but, now, I want my Data View to only display rows whose Department column matches the Department value returned by this function. I selected the "Filter Option" from the Options ribbon and added a simple filter to display only rows where the Department column was equal to 1. I replaced the '1' with my javascript function, and the result looked like this:

<Query>
    <Where>
        <Eq>
            <FieldRef Name="NewColumn1"/>
            <Value Type="Number">
                <script type="text/javascript">
                    (function() {
                        var filter = [code to pull Department...];
                        return Number(filter);
                     })(); 
                </script>
            </Value>
        </Eq>
    </Where>
</Query>

Currently, no results are returned when the view is loaded.

My question is, is there a way to get this javascript function to work with the filter, or is this impossible to do? If it's not possible, are there any good alternatives to do what I am trying to do?

  • It would be cool if that worked, I guess the main problem you are facing is that JavaScript is meant to execute client side in the browser, and the rest of the logic in the DataView webpart is executed on the server – Robert Lindgren May 7 '14 at 18:35
  • What version are you using? Enterprise by chance? – Eric Alexander May 7 '14 at 18:38
  • Yes, I'm using SP2010 Enterprise – jobrien9 May 8 '14 at 13:29
1

One option you have is to implement filtering in the client side. I have done this in one of my implementation, i used to pass the values from a page using Query string and on the list view page i had another script to read the values and display only matching rows.

Check jQuery Filters

  • Thanks for the link, @Sudhir. I think I am very close to getting this to work, but I am having problems with my filterList function. When I use the .ms-itmhover class as recommended in your website, nothing is filtered. When I substitute in the .ms-vb2 or .msvb1 class (which is the class of the individual cells), the filter works, but naturally, it only removes the individual cells, not their respective rows. Have you encountered this error before? I am using SP2010, but it appears that .ms-itmhover is still the class name for the rows. – jobrien9 May 8 '14 at 17:50
  • As soon as I posted that comment, I realized that the reason my .itmhover class wasn't working is because I was filtering based on the whether or not the rows contained the number '1'. Even if the column to which I'm trying to apply the filter does not contain '1', chances are that some other column in that row does. Do you know how I might filter by only one column, but hide the entire row if the desired value is not found in that particular column? – jobrien9 May 8 '14 at 17:58
  • @jobrien9 in that case you will have to update your filterlist function and change the selector to look for specific TD (children(2) etc.), remember this will always require your searched column to be on the specific position. If you update your list view and move column to new position, make sure to change filter as well – Sudhir Kesharwani May 8 '14 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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