Learning XPages Part 43 : Enabling All Fields On A New Document

So now we have the ability to add a new
person document to our database via XPages but you may have noticed that
there are some fields that are set as readonly when the document is in
edit mode because you wouldn’t want the end user changing them. The main
fields that have been turned off so far are the users name and location.
In this part we will turn them back into editable fields but only when
you are creating a new document.

In traditional Notes client development
there is a formula called @IsNewDoc that we can use to determine if a document
has just been created and has never been saved yet. In XPages we can make
use of our formula language knowledge and use @IsNewDoc() in our JavaScript
to do the exact same thing.

Open the content_Person custom control
and from the design view select the ‘firstname’ editbox control. If you
look at the properties for the control you’ll see that the ‘ReadOnly’ property
is currently ticked on. We need to compute this value instead so click
on the diamond and select the compute value option. Here’s the JavaScript
I have written :

if(@IsNewDoc()
== 1)
{
       return false;
} else
{
       return true;
}

I have added this code to all the fields
that that I had originally set as ReadOnly. Save and refresh your XPage
and try create a new person and you’ll see the fields are now editable.

A picture named M2

While they are editable it doesn’t look
great, there are no lables for the data entry person to know what to type
in so lets add some labels and set them so they only appear when you are
creating a new document.

Drag in a label control from the controls
pane on the right of your screen. Set it’s value to whatever label you
are applying and then in the ‘visible’ property click the diamond and use
the same code as above except reverse the logic, this time you need to
return ‘true’ when it is a new document.

A picture named M3

Here’s how my content_person design
view looks now :

A picture named M4

You can see I also added a table to
help align the labels over the fields. Now when I save my XPage and preview
it in the browser it looks like this :

A picture named M5

In the next part We’ll add a default
value for the location and also look at how we can change it into a dropdown
list of all the valid locations.

Advertisement
Tagged with: , ,
Posted in None
Archives
%d bloggers like this: