Search
Categories

Entries in Categories (1)

Monday
05Oct2009

Restricting a View to Multiple Categories in XPages

Over on Jake Howlett's Blog about Restricting View To Mutliple Categories With Flex I posted a comment on how to do the same thing in XPages. After I posted the "how to" I thought this was probably a good enough tip to post here versus getting lost in the comments out there, so here goes.

The idea is to be able to select multiple categories, then display the documents from those categories in the view. To accomplish this with XPages, I don't use the standard "categoryFilter" property that restricts a view to a single category. Problem with that is, its a SINGLE category.

What I use, is the seach parameter. This allows us to create a full text search query that will filter the view for us.

To accomplish, this, I add a List box to the XPage, that contains the list of category values. I then bind the listbox to a variable in the viewScope:

After binding the variable, I change the onchange event of the listbox to do a partial refresh on the appropriate region of my XPage:

(In this image, you can ignore the Execute Script action I have added, it serves a different purpose than this example)

Then to actually perform the filter, I added the following code to the 'search' property of the data, which is a Domino View defined at the XPage level.

try {

    return "FIELD ProductKeywords CONTAINS " + viewScope.selectedTag.join(' OR ');

} catch (e) {

    return "FIELD ProductKeywords CONTAINS " + viewScope.selectedTag

}

The try { } catch (e) { } block is an error handling block that attempts to execute the code in the try { } block, and if it generates an error, it executes the code in the catch { } block.  The reason for this is if the user has selected a single field, the value of viewScope.selectedTag is just a simple variable, but if the user has selected multiple values, then viewScope is an array. So we first attempt to join selectedTag, treating it as an array. If it's not an array, an error is generated, and the catch block is executed, which treats selectedTag as a simple variable.

Here is where the search property is located:

The database must be full text indexed in order for this to work.

You can view a demo of this working on the test YellowBubble.org site.

Happy Coding...

 

Oh, and one more thing ... this works in 8.5 and 8.5.1 ...