uf_filter() to replace Filter()

Share this topic:



Link to this posting

Postby Ursego » 05 Mar 2013, 15:55

Create one function uf_filter() (its code is provided below) instead of using the DW's built-in functions SetFilter() and Filter(). Create that function in the ancestor DW of your application.

That solution:

1. Decreases the number of code lines in the calling script - 1 instead of 2 (or one more if GroupCalc() required, or even one more if RowCount() is called just after filtering). So, the fragment

Code: Select all
dw_salary.SetFilter(ls_filter_expr)
dw_salary.Filter()
dw_salary.GroupCalc()
ll_row_count = dw_salary.RowCount()

shrinks to one line:

Code: Select all
ll_row_count = dw_salary.uf_filter(ls_filter_expr)

2. Alerts if the passed expression is NULL (it can nappen when the expression is built dynamically and a variable with NULL is concatenated). If an EMPTY STRING is passed it's NOT interpreted as error (it's used to remove filtering).

3. If the search expression is incorrect, displays a programmer-friendly message which:
A. Displays the incorrect expression.
B. Displays the DW's DataObject and suggests to check if all mentioned fields exist in it.

Here is the source of that function (it utilizes f_throw() to throw an exception of type n_ex - read here for details):

Code: Select all
/**********************************************************************************************************************
Acc:   public
-----------------------------------------------------------------------------------------------------------------------
Dscr:   Filters the DW using passed expression (see http://forum.powerbuilder.us/viewtopic.php?f=2&t=75).
-----------------------------------------------------------------------------------------------------------------------
Arg:   as_new_filter
-----------------------------------------------------------------------------------------------------------------------
Ret:   long (number of rows in Primary! buffer after filtering)
-----------------------------------------------------------------------------------------------------------------------
Thr:   n_ex
**********************************************************************************************************************/
int li_rc

if IsNull(as_new_filter) then f_throw(PopulateError(1, "Filter exression is NULL."))

li_rc = this.SetFilter(as_new_filter)
if li_rc <> 1 then f_throw(PopulateError(2, "this.SetFilter() failed.~r~n~r~nFilter Expression:~r~n" + as_new_filter + &
                                             "~r~n~r~nCheck if all fields, mentioned in expression, exist in DataObject '" + this.DataObject + "'."))
li_rc = this.Filter()
if li_rc <> 1 then f_throw(PopulateError(3, "this.Filter() failed.~r~n~r~nFilter Expression:~r~n" + as_new_filter + &
                                             "~r~n~r~nCheck if all fields, mentioned in expression, exist in DataObject '" + this.DataObject + "'."))

this.GroupCalc()

return this.RowCount()

If the task is to APPEND a filter (rather than to REPLACE the existing one) then create uf_append_filter() in your ancestor DW:

Code: Select all
/**********************************************************************************************************************
Acc:   public
-----------------------------------------------------------------------------------------------------------------------
Dscr:   Works as uf_filter() but new filter is added to existing instead of to replace it.
-----------------------------------------------------------------------------------------------------------------------
Arg:   as_filter_to_append
-----------------------------------------------------------------------------------------------------------------------
Ret:   long (number of rows in Primary! buffer after filtering)
-----------------------------------------------------------------------------------------------------------------------
Thr:   n_ex
**********************************************************************************************************************/
string ls_existing_filter
string ls_new_filter

ls_existing_filter = this.object.datawindow.table.filter
choose case ls_existing_filter
case "?", "!"
   ls_new_filter = as_filter_to_append
case else
   ls_new_filter = "(" + ls_existing_filter + ") AND (" + as_filter_to_append + ")"
end choose

return uf_filter(ls_new_filter)

Of course, both the given functions can also be added to the ancestor DataStore.
User avatar
Ursego
Site Admin
 
Posts: 112
Joined: 19 Feb 2013, 20:33

Link to this posting

Postby webgep » 07 Sep 2014, 03:01

Hey,

You could also implement 2 events - ue_PreFilter () and ue_PostFilter on the datawindow
- this should also be implemented in the PFC :)
/Georgios
webgep
 
Posts: 3
Joined: 07 Sep 2014, 02:56


Return to Tips and Tricks

Who is online

Users browsing this forum: No registered users and 1 guest


uf_filter() to replace Filter()

Share this topic:


If you think that this site is not too bad, please LIKE it in Facebook. Thanks!





free counters

eXTReMe Tracker