Hi Hector,
I do this almost everywhere in my system. It works well, is simple, and the interface seems to be something general users are fine with.
Have just read Bruce's reply fully. Below is just a longer more boring version of what he is saying!
1. Create a memory form (don't forget to turn off the save/cancel button, unless you want a "close" button for mobile).
2. Create two tabs in the memory form, first one named "Search" the second named "Browse". Obviously you don't HAVE to do this, but I find it separates it out interface-wise nicely for the brain.
3. Decide if you need to have a RANGE functionality. So if you are searching on an Invoices browse, for example. You want to have an "Invoiced Date" filter. So you probably want a range (From and To).
4. Assuming you want the range, create a new field in the "Search" tab. Add it as a new local variable, and call it "search:Invoice_Date_Range" and make it a STRING(20). Make the field itself a Radio button. Add a few records straight into the Radio button options. I'd go with (just to get this going) "Today", "This Week", "This Month", "Custom". Save the field. We'll come back to it in a moment.
5. Add another local field, call it "search:Invoice_Date_From". Make it a DATE field. Add the following into the "Hide" condition .. p_web.GSV('search:Invoice_Date_Range')~=<39>Custom<39>. Make sure that it is NOT the last on the line. Make sure it's ticked to "Send new value to server" in the "Client-Side" tab.
6. Add another local (Date) field, call it "search:Invoice_Date_To". Give it the same hide condition as above. Make sure it's ticked to "Send to server" also.
7. Then go back to the Range field. In the Client-Side tab make sure it's ticked to "Send new value to server" AND then add both the From and To date fields into the "Reset" mini-browse.
7.5 Click on the "Server Code" button in the Client-Side tab of the Range field. Add the following code:
case p_web.GSV('search:Invoice_Date_Range')
of 'Today'
p_web.SSV('search:Invoice_Date_From',today())
p_web.SSV('search:Invoice_Date_To',today())
of 'This Week'
p_web.SSV('search:Invoice_Date_From',today()-today()%7)
p_web.SSV('search:Invoice_Date_To',today()-today()%7+6)
of 'This Month'
p_web.SSV('search:Invoice_Date_From',date(month(today()),1,year(today())))
p_web.SSV('search:Invoice_Date_To',date(month(today())+1,1,year(today()))-1)
of 'Custom'
! you don't really do anything here at all
end
8. Now we are ALMOST done with the search form.
9. Go into the "Browse" tab you created above and add the browse procedure (as a Procedure type).
10. Now you need to put in some embed code at the START of the form (whenever it loads). I use the first embed within the "Generate Form" embeds, but Bruce will probably send me to the dunce corner. Whatever place you use, use it to put the following code:
if ~p_web.IfExistsSessionValue('search:Invoice_Date_Range')
p_web.SSV('search:Invoice_Date_Range','This Month')
p_web.SSV('search:Invoice_Date_From',date(month(today()),1,year(today())))
p_web.SSV('search:Invoice_Date_To',date(month(today())+1,1,year(today()))-1)
end
11. Now save all the way out of the form.
12. In the browse (if you've already created it then cool, otherwise CREATE THE BROWSE!) go to the "Set Filter" embed under the "Before Browse Loop" parent embed.
13. Add the following code:
!* Custom Filter *
loc:FilterWas = ''
if (p_web.GSV('search:Invoice_Date_From') > 0)
loc:FilterWas = 'invrec:Invoiced_Date>='&p_web.GSV('search:Invoice_Date_From')&choose(loc:FilterWas='','',' and '&clip(loc:FilterWas)) !* this &choose bit at the end is just what I put at the end of most custom filter lines to make sure it's getting the right AND/OR structures. There might be brackets involved too, depending on how intense the filter gets. *
end
if (p_web.GSV('search:Invoice_Date_To') > 0)
loc:FilterWas = 'invrec:Invoiced_Date<='&p_web.GSV('search:Invoice_Date_To')&choose(loc:FilterWas='','',' and '&clip(loc:FilterWas))
end
!* Set Filter *
ThisView{PROP:Filter} = loc:FilterWas
!* Debug filter if you want *
p_web._trace('(<procedure name>, Custom Filter) Filter: '&clip(loc:FilterWas))
14. Save.
15. Compile.
16. Boom.