Seach Steps for Customizing the Search Field of a View Component

Mindwatering Incorporated

Author: Tripp Black

Created: 08/11/2010 at 12:55 AM

 

Category:
Notes Developer Tips
XPages

Issue:
The default search field just searches words/phrases. What if you want to search against a specific field value like the "classic" search allows?


Solution A:
Leverage your existing @Formula knowledge. Build a query just like you would for a "regular" web app only string together with a " AND " or " OR " without the web safe "+" symbols (not "+AND+" or "+OR+").

Overview of Steps:

1. Create a new (empty) custom control for your search "form" in a new empty panel.

2. Add the view container. Select the view data source. Modify the columns populated removing any not wanted. Make sure you set the columns to be links, otherwise your results are "read only".

3. Create the text box / combo controls desired for the search filter.
For example create one that might restrict by type and two date ones that restrict to a date range.

4. Create a computed field that calculates search string. The editable and computed "field" controls needs to have the following Data property choices:
- Advanced
- View Scope (or session Scope)
- Appropriate Variable names (e.g. srchType, srchDtStart, srchDtEnd)
- Compute Dynamically
- Set that all editable fields are required within the validation tab and any length restrictions.

5. For all the editable search fields, you want to go to the Events tab and program a server event for the onChange event. This will update the query and the results as changes are made. On the right select Partial Update and choose the panel that is this custom control. (This way the whole page including any header/footer, etc doesn't have a wasted update.)

6. For the computed field, enter code similar to the following depending on your actual search fields:
"FIELD docType CONTAINS " + viewScope.srchType + " AND FIELD docStart > " + viewScope.srchDtStart + " AND FIELD docEnd > " + viewScope.srchDtEnd + "".

7. Add a Submit/Search button that is bound to the same scope that has the Data type of Submit.
(It will automatically "get" the code to do the submit.)

8. Add the Custom Control to your XPage and work out any typos.

Option B:
Basically perform the steps above for the custom control structure. However, instead of building the query as a field, you adjust the actual XML code of the control similar to this:
(source: Somewhere linked off the forum in Notes.Net)

<xp:repeat id="repeat1" rows="999" var="filteredDocs"
indexVar="filteredIndex">
<xp:this.value><![CDATA[#{
var masterCollection = cview.getAllEntries();
if (viewScope.filterSelection != null) {
var xem:NotesViewEntryCollection = masterCollection.cloneCollection();
if (viewScope.filterSelection != null) {
if (viewScope.filterSelection.constructor == Array) {
for (i=0;i<viewScope.filterSelection.length;i++) {
xem.subtract(srchview.getAllEntriesByKey(viewScope.filterSelection[i], true))
}
} else {
xem.subtract(srchview.getAllEntriesByKey(viewScope.filterSelection, true));
}
}
if (xem != null)
if (xem.getCount() > 0) {
var nve:NotesViewEntry = xem.getFirstEntry();
while (nve != null) {
if (masterCollection.contains(nve))
masterCollection.subtract(nve);
nve = xem.getNextEntry(nve);
}
}
}
return masterCollection;}]]></xp:this.value>


___________________________________

Other Sample Search Queries / @Formulas:
tmp1:=@If(Customer !=""; "FIELD+Customer=" + Customer; "");
tmp2:=@If(PONo !=""; "FIELD+PONo=" + PONo; "");
tmp3:=@If(ORDNO !=""; "FIELD+ORDNO=" + ORDNO; "");
tmp4:=@If(InvNo !=""; "FIELD+InvNo=" + InvNo; "");
tmp5:=@If(CustID !=""; "FIELD+CustID=" + CustID; "");
tmp6:=@If(CustType !=""; "FIELD+CustType=" + CustType; "");
tmp7:=@If(City !=""; "FIELD+City=" + City; "");
tmp8:=@If(ZIP !=""; "FIELD+ZIP=" + ZIP; "");
tmpa:=@Trim(tmp1 : tmp2 : tmp3 : tmp4 : tmp5 : tmp6 : tmp7 : tmp8);
tmpb:=@Implode(tmpa; " AND ");

----

GL_FrmFld4 + "=\"" + GL_FrmFld4Val + "\"");
fld5tmp:=@If(GL_FrmFld5="" | GL_FrmFld5="- Select -" | GL_FrmFld5Val=""; ""; "FIELD " + GL_FrmFld5 + "=\"" + GL_FrmFld5Val + "\"");
fld6tmp:=@If(GL_FrmFld6="" | GL_FrmFld6="- Select -" | GL_FrmFld6Val=""; ""; "FIELD " + GL_FrmFld6 + "=\"" + GL_FrmFld6Val + "\"");
fld7tmp:=@If(GL_FrmFld7="" | GL_FrmFld7="- Select -" | GL_FrmFld7Val=""; ""; "FIELD " + GL_FrmFld7 + "=\"" + GL_FrmFld7Val + "\"");
fld8tmp:=@If(GL_FrmFld8="" | GL_FrmFld8="- Select -" | GL_FrmFld8Val=""; ""; "FIELD " + GL_FrmFld8 + "=\"" + GL_FrmFld8Val + "\"");
fld9tmp:=@If(GL_FrmFld9="" | GL_FrmFld9="- Select -" | GL_FrmFld9Val=""; ""; "FIELD " + GL_FrmFld9 + "=\"" + GL_FrmFld9Val + "\"");
fld10tmp:=@If(GL_FrmFld10="" | GL_FrmFld10="- Select -" | GL_FrmFld10Val=""; ""; "FIELD " + GL_FrmFld10 + "=\"" + GL_FrmFld10Val + "\"");

@If(fldtmp=""; "";
@Implode(@Trim(fldtmp : fld1tmp : fld2tmp : fld3tmp : fld4tmp : fld5tmp : fld6tmp : fld7tmp : fld8tmp : fld9tmp : fld10tmp); " AND "))


previous page