Consultor Eletrônico



Kbase 13590: reopening browse query based on typed input: code example
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   5/10/1998
reopening browse query based on typed input: code example


This knowledgebase describes one strategy for designing a browse
interface where the query changes dynamically based on what the
user types into a field.

The scenario is as follows. There is a browser on the screen, along
with a field that can accept user input. With each character that the
user types into the field, the query for the browser changes so that
only those records that match the typed input are showing. For
example, the browse might use the customer.name field, and as
characters are typed in, the query is limited only to those customers
whose names begin with the input characters.

Let's say the query we're using is the following:

DEFINE QUERY q FOR sports.customer SCROLLING.


The browse for this query might display the name and cust-num fields:

DEFINE BROWSE b FOR QUERY q DISPLAY sports.customer.name
sports.customer.cust-num WITH NO-LABELS NO-UNDERLINE
SIZE 34.14 BY 5.42.


The way to limit the records included in any browse is by opening
(or reopening) the query using a WHERE clause. For instance, if we
want only the customers whose names begin with "B", the OPEN QUERY
statement will be:

OPEN QUERY q FOR EACH sports.customer WHERE
sports.customer.name BEGINS "B".


Now, what if we wanted to do this based on a variable? Simple
enough: instead of using the literal "B", use a variable which has
been defined as a CHARACTER:

DEFINE VARIABLE t AS CHARACTER NO-UNDO.
UPDATE t.
OPEN QUERY q FOR EACH sports.customer WHERE
sports.customer.name BEGINS t.


The next step is now to use a widget as the means of obtaining our
WHERE criteria. For the sake of our design, we want the widget
to be one where the user can type both forward and backward - in
other words, we want cursor control. For this reason, a simple
fill-in widget is not the solution: it has no backward cursor
control.

The answer is to use an EDITOR widget which is designed to *look*
like a fill-in field (in other words, an editor which consists only
of one line, with no word wrap.) Let's define the editor widget as
as follows:

DEFINE VARIABLE editor-1 AS CHARACTER
VIEW-AS EDITOR NO-WORD-WRAP
SIZE 34 BY 1 NO-UNDO.


The helpful feature of editor widgets is that they have an attribute
called :CURSOR-OFFSET which allows a programmer to place the cursor
exactly as needed. Editors also have the :DELETE-CHAR() method
which allows deletion of characters when a user types backward.

Once the widgets and the query have been defined, bringing everything
together requires coding trigger code for the editor widget which
affects the query each time the user types into it. For this we
use the ANY-KEY event. The trigger for ON ANY-KEY of the editor
would look something like the following:

DEFINE VARIABLE meth AS LOGICAL NO-UNDO. /* logical variable for
use with methods */
ON ANY-KEY OF EDITOR-1 IN FRAME f DO:

CASE LASTKEY:
WHEN KEYCODE("DEL") THEN DO: /* delete (typing backward) */
IF editor-1:CURSOR-OFFSET = 1 THEN.
ELSE DO:
editor-1:CURSOR-OFFSET = editor-1:CURSOR-OFFSET - 1.
meth = editor-1:DELETE-CHAR().
OPEN QUERY q FOR EACH sports.customer WHERE
customer.name BEGINS editor-1:SCREEN-VALUE NO-LOCK.
END.
RETURN NO-APPLY.
END.

WHEN KEYCODE("BACKSPACE") THEN DO: /* typing backward */
IF editor-1:CURSOR-OFFSET = 1 THEN.
ELSE DO:
editor-1:CURSOR-OFFSET = editor-1:CURSOR-OFFSET - 1.
meth = editor-1:DELETE-CHAR().
OPEN QUERY q FOR EACH sports.customer WHERE
customer.name BEGINS editor-1:SCREEN-VALUE NO-LOCK.
END.
RETURN NO-APPLY.
END.

WHEN KEYCODE("ENTER") THEN DO: /* Programmer should decide
whether to APPLY ENTRY to
next widget in tab order */
APPLY "ENTRY" TO fill-in-2 IN FRAME f.
RETURN NO-APPLY.
END.

OTHERWISE DO: /* any printable key (typing forward) */
APPLY LASTKEY.
OPEN QUERY q FOR EACH sports.customer WHERE
customer.name BEGINS editor-1:SCREEN-VALUE NO-LOCK.
RETURN NO-APPLY.
END.
END CASE.

END. /* end ON ANY-KEY trigger */

Progress Software Technical Support Note # 13590