« [Tip] Dynamic buttons management | Main | [Showcase] Bob Cusick's ReportWriter module »

December 26, 2005

[Tip] Choosing one record from a list

by David Workman
Data Mosaic

Picture_2 Choosing a record from a list and then proceeding on to another action is a common task. In Servoy this is typically accomplished by presenting a list form in a popup dialog, choosing a record, and then proceeding on with the methods attached to whatever action buttons you have set up.

How do we control the user selecting only one record? We need a technique that behaves like a radio type field (only one value of a value list allowed) when choosing a record.

Let's start with our list form. We need a field to capture the user choice so create a field of type integer (called "value_linked" in the following code example) in the form table. Put this field on the form and set it as a check box type. Do not attach a value list to this field. This will give you a lone checkbox on each row of your list that will put a value of "1" into the field if checked and a value of "0" when unchecked. For free. (Using a radio type field doesn't work as well because the default radio widget doesn't appear unless a value list is attached. I know, inconsistent but it has been this way forever.)

You would stop here if you wanted to allow the user to select multiple records. We need to do some additional work to restrict a user to selecting only one record—we need a way to turn off any currently selected record when a new selection is made.

The Database Manager node in the method editor has a most useful set of functions attached to the JSFoundSetUpdater object. The following method creates an updater object (the current found set in this case), sets "value_linked" to "0", and updates all the records. The result at this point is no records with a check mark.

The next step is set the value of the "value_linked" field of the current record to "1" so that it has a check mark. Last, save the record and update the user interface.

//clear all values in record set
var fsUpdater = databaseManager.getFoundSetUpdater(foundset)
fsUpdater.setColumn('value_linked',0)
fsUpdater.performUpdate()

//set current record
value_linked = 1

//update UI
controller.saveData()
application.updateUI()

Attach this method to the "onFocusGained" event of the "value_linked" field so that it fires immediately when the field is clicked.

A simple technique to enforce the selection of one and only one record in a list of records.

| Posted by David Workman on December 26, 2005 at 01:37 PM in Tips | Permalink

Comments

Post a comment