Learning XPages Part 20 : Restricting The Repeat To A Single Category
Tags : Lotus Domino XPages CGI Repeat Category
Bookmark :
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.
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.
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'.
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 :
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
and my ?location=Pittsburgh result
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.
Bookmark :
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.
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.
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'.
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 :
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
and my ?location=Pittsburgh result
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.
Comments
Posted by Matt White At 02:53:04 PM On 02/27/2009 | - Website - |
Use the global "context" JavaScript object:
context.getUrlParameter("Location")
Posted by Matt Holthe At 06:25:37 PM On 02/27/2009 | - Website - |
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.
Posted by Brendan At 07:04:17 PM On 03/02/2009 | - Website - |
Also, Locations with a space (New York) break the code - can you post a fix for that later?
Posted by Larry At 01:07:04 PM On 03/04/2009 | - Website - |
I usually copy/paste the code, so any typo can be very time consuming...
Dec, are you going to fix the post?
Posted by Cristian D'Aloisio At 10:46:31 AM On 03/08/2009 | - Website - |