Learning XPages Part 20 : Restricting The Repeat To A Single Category

At the moment our location XPage shows
us all the entries in our categorized view. In this part we are going to
restrict which category is displayed in the repeat control by passing a
variable in on the URL in the format of http://demosite/xphone.nsf/location.xsp?location=Dublin.

To retrieve the variable passed in one
the URL I am going to use a serverSide JavaScript library that was created
by Thomas Gumz with some modifications by Matt White. The original script
library is available in the XPages Discussion template which should be
on your Domino 8.5 server and the modifications can be found in the TaskJam
application which you can downloaded by going to
taskjam.net..
Many thanks to Matt for giving permission to use the modified version of
the script library. The script library will be included in the next download
of the development database.

To add the script library to our application
we open the content_Location custom control and then in the outline view
on the left of your screen select the custom control node and look at the
‘Resources’ properties tab. Click the button to add a Script Library and
then select the library from the dialog box that appears.

A picture named M2

Remain in the properties for the custom
control and go to the ‘All properties’ tab and find the ‘BeforePageLoad’
property. In here we are going to write some server side javascript code
that uses the script library. Here’s the code :



var cgi = new CGIVariables();
var param = cgi.getURLParam("Location");
if (param) {
if (param != "" )
sessionScope.locationfilter = param;
}

What this code does is before the page
has loaded it looks at the URL and finds the location= part and if it exists
it puts whatever the value is into a sessionscope variable called ‘locationfilter’.
Session Scope is a special scope where you can add variables that only
last for the duration of the users session. As soon as they close their
web browser the session scope variables are lost. There is also an Application
Scope and a Page Scope that you can use to store these values. Heres how
the code will look in the source view of your xpage, you can see the resource
for the server side script library and the beforePageLoad script. As you
get more advanced in XPages you may find yourself writing code directly
in the source view.

A picture named M3

So now we have the location taken from
the URL and stored in a sessionScope variable we need to tell the data
source to only return the values in that category. To do this we need to
look at the ‘All Properties’ tab for the custom control and look at the
‘Data’ section and expand it out to find the data source that we created
for the custom control. In here we’ll find a property called ‘categoryFilter’.

A picture named M4

In here we can reference the sessionScope
variable by clicking on the blue diamond and selecting compute value and
then entering ‘sessionscope.locationfilter’ into the script dialog box.
Our XPage source will now have an extra line up in the data source definition
:

A picture named M5

Save the custom control and then in
your web browser add a ?Location=Pittsburgh ( or whatever location you
might have in your sample data ) and load the page.

Here is my ?location=Chicago result

A picture named M6

and my ?location=Pittsburgh result

A picture named M7

You will notice how the alternating
row styles are now appearing correctly.

Now that we have a page to display,
in the next part we’ll link the sidebar to this page and then I’ll update
the downloadable database for you all to work with.

Tagged with: , , , , ,
Posted in None
5 comments on “Learning XPages Part 20 : Restricting The Repeat To A Single Category
  1. Matt White says:

    Dec, you don’t need to pass parameters on the URL unless you want the details to be bookmarkable. You can also make use of the sessionScope variables to store parameters in memory.

    Like

  2. Matt Holthe says:

    There is also a MUCH easier way to get the parameter instead of including the script library (although it’s probably good that you showed that).Use the global “context” JavaScript object:context.getUrlParameter(“Location”)

    Like

  3. Brendan says:

    @Matt H:I was thinking the exact same thing but have come to realise over the past couple of days that context.getUrlParameter() is not available in certain contexts, such as described above, and when programmatically getting the document id.

    Like

  4. Larry says:

    The example shows sessionscope, but it must be sessionScope to work. That took a while to catch.

    Also, Locations with a space (New York) break the code – can you post a fix for that later?

    Like

  5. @4 : you are right, sessionscope is wrong, sessionScope is right.I usually copy/paste the code, so any typo can be very time consuming…Dec, are you going to fix the post?

    Like

Comments are closed.

Archives