この演習では、ページにラジオ・ボタン・グループを追加して、ユーザーが AND 検索条件と OR 検索条件のいずれかを選択できるようにします。このページは、実行時には次のようになります。
function NameAndStateSearch_Or(func int, lname char(30) in, state char(2) in, Customers Customer[]) get Customers with #sql{ select CUSTOMER_ID, FIRST_NAME, LAST_NAME, PASSWORD, PHONE, EMAIL_ADDRESS, STREET, APARTMENT, CITY, STATE, POSTALCODE, DIRECTIONS from EGL.CUSTOMER where LAST_NAME LIKE :lname OR STATE = :state }; if (sqlcode != 0) func = 0; end end
この関数は、where 文で AND ではなく OR が使用されている点以外は、前の演習で追加した NameAndStateSearch_And 関数と同じです。
AND
AND
OR
プロパティー・ビューは次のようになります。
package pagehandlers;
import data.*;
PageHandler customersearch { handleHardIOErrors = no, throwNrfEofExceptions = yes }
{view="customersearch.jsp", onPageLoadFunction="onPageLoad"}
searchTerms Customer; //Customer record (search) values
searchResults Customer[]; //Customers results rows
pagemsgRec msgRec; //pagemsgRec variable of type MsgRec
func int; //func - a flag to pass the state of the results
andOr char(3); //A char field that will be bound to the radio button group
Function onPageLoad()
if (func == 0) //Either no rows, or 1st time into page
pagemsgRec.msg="No customer(s) found or no search criteria entered.";
End
end
function searchFunction()
func = 1; //Initialize func before calling database
searchTerms.Last_Name = searchTerms.Last_Name+"%"; //Add wildcard (%)
if (andOr == "AND")
CustomerLib.NameAndStateSearch_And(func, searchTerms.Last_Name,
searchTerms.State, searchResults); //Call search function
pagemsgRec.msg = "Customer(s) found. Search again?"; //Assign msg text
pagemsgRec.nbr = sysLib.size(searchResults); //Get row count
else
CustomerLib.NameAndStateSearch_Or(func, searchTerms.Last_Name,
searchTerms.State, searchResults); //Call search function
pagemsgRec.msg = "Customer(s) found. Search again?"; //Assign msg text
pagemsgRec.nbr = sysLib.size(searchResults); //Get row count
end
end
End
Record msgRec //Custom EGL record type definition
//used to combine numeric and string data
10 nbr int; //Integer - numeric field
10 msg char(222); //Character data
end
このコードは、以下の点を除き、前の演習で追加したコードと類似しています。
ページは次のようになります。
ページをテストする際には、AND 関数と OR 関数の両方を使用してみてください。検索ページのいずれかのラジオ・ボタンを選択して、正常に機能することを確認します。
この検索ページも、まだ使いやすいとはいえません。サンプル・データベースには多数のレコードがなく、推測される状態の種類も多くはないからです。次の演習では、「STATE」入力フィールドをコンボ・ボックスに変更し、データベースで使用されている状態がすべてリストとして表示されるようにします。
これで、「演習 2.4: コンボ・ボックスの動的取り込み」を開始する準備ができました。