uf_lookup_display() to obtain DropDown's display value

Share this topic:



Link to this posting

Postby Ursego » 11 Apr 2013, 14:29

Obtaining the displayed value of a field, having a DropDown DW, is simple:

Code: Select all
ls_describe_expr = "Evaluate('LookUpDisplay(" + as_col + ")', " + String(al_row) + ")"
ls_display_value = dw_test.Describe(ls_describe_expr)

But that method works only for the Primary! buffer. PB has no built-in means to do the same with other buffers. It's looking ok - how do you want the displayed value of something which is not displayed? But, anyway, sometimes we need the value which would be displayed if the record would be in the Primary! buffer - for example, to be displayed in a message box in a pre-save validation when the validated row has been filtered out or even deleted.

Of course, it should be placed in your DW ancestor:

Code: Select all
/*----------------------------------------------------------------------------------------------------------------------
Acc:   public
-----------------------------------------------------------------------------------------------------------------------
Dscr:   returns the lookup-display value of a drop-down. Deals with all the buffers.
      To deal with the primary buffer only, use the overloaded function with 2 arguments.
------------------------------------------------------------------------------------------------------------------------
Arg:   al_row - long - row number
      a_buf   - DWBuffer - buffer
      as_col - string - name of column with drop-down
------------------------------------------------------------------------------------------------------------------------
Ret:   the lookup-display value (string)
----------------------------------------------------------------------------------------------------------------------*/
datetime            ldt_val
date               ld_val
time               lt_val
int               li_dwc_row
string            ls_describe_expr
string            ls_val
string            ls_serch_expr
string            ls_data_col
string            ls_data_col_type
string            ls_display_col
string            ls_display_col_type
string            ls_display_value
string            ls_format
DataWindowChild   ldwc

constant string FORMAT_D = 'dd/mm/yyyy' // date format
constant string FORMAT_T = 'hh:mm:ss' // time format

if a_buf = Primary! then
   // Oooh, it's easy!
   ls_describe_expr = "Evaluate('LookUpDisplay(" + as_col + ")', " + String(al_row) + ")"
   ls_display_value = this.Describe(ls_describe_expr)
   return ls_display_value
end if

this.GetChild(as_col, ref ldwc)
ls_format = this.GetFormat(as_col)

// Get the Data Column and Display Column and their types:
ls_data_col = this.Describe(as_col + '.dddw.datacolumn')
ls_display_col = this.Describe(as_col + '.dddw.displaycolumn')
ls_data_col_type = this.Describe(as_col + '.coltype')
ls_data_col_type = Lower(Left(ls_data_col_type, 5))
ls_display_col_type = ldwc.Describe(ls_display_col + '.coltype')
ls_display_col_type = Lower(Left(ls_display_col_type, 5))

// Prepare an expression to find the row in Child DW:
choose case ls_data_col_type
case 'decim', 'numbe', 'long', 'ulong', 'real', 'int' // numeric
   ls_val = String(this.GetItemNumber(al_row, as_col, a_buf, false))
   ls_serch_expr = ls_data_col + '=' + ls_val
case 'char', 'char(' // string
   ls_val = this.GetItemString(al_row, as_col, a_buf, false)
   ls_serch_expr = ls_data_col + '="' + ls_val + '"'
case 'datet', 'times' // datetime, timestamp
   ldt_val = this.GetItemDateTime(al_row, as_col, a_buf, false)
   ls_val = String(ldt_val, FORMAT_D + " " + FORMAT_T)
   ls_serch_expr = 'String(' + ls_data_col + ', "' + FORMAT_D + " " + FORMAT_T + '")="' + ls_val + '"'
case 'date' // date
   ld_val = this.GetItemDate(al_row, as_col, a_buf, false)
   ls_val = String(ld_val, FORMAT_D)
   ls_serch_expr = 'String(' + ls_data_col + ', "' + FORMAT_D + '")="' + ls_val + '"'
case 'time' // time
   lt_val = this.GetItemTime(al_row, as_col, a_buf, false)
   ls_val = String(lt_val, FORMAT_T)
   ls_serch_expr = 'String(' + ls_data_col + ', "' + FORMAT_T + '")="' + ls_val + '"'
end choose

// Find the row in Child DW:
li_dwc_row = ldwc.Find(ls_serch_expr, 1, ldwc.RowCount())
if IsNull(li_dwc_row) or li_dwc_row < 1 then return ''

// Get value of the Display Column in that row:
choose case ls_display_col_type
case 'decim', 'numbe', 'long', 'ulong', 'real', 'int' // numeric
   ls_display_value = String(ldwc.GetItemNumber(li_dwc_row, ls_display_col))
case 'char', 'char(' // string
   ls_display_value = ldwc.GetItemString(li_dwc_row, ls_display_col)
case 'datet', 'times' // datetime, timestamp
   ls_display_value = String(ldwc.GetItemDateTime(li_dwc_row, ls_display_col), ls_format)
case 'date' // date
   ls_display_value = String(ldwc.GetItemDate(li_dwc_row, ls_display_col), ls_format)
case 'time' // time
   ls_display_value = String(ldwc.GetItemTime(li_dwc_row, ls_display_col), ls_format)
end choose

return ls_display_value

You can also create an overload for the cases when you need to deal with the Primary! buffer only (anyway, it's more elegant then using the Describe() method):

Code: Select all
/*----------------------------------------------------------------------------------------------------------------------
Acc:   public
-----------------------------------------------------------------------------------------------------------------------
Dscr:   returns the lookup-display value of a drop-down. Deals with the primary buffer only.
      To deal also with the filter buffer, use the overloaded function with 3 arguments.
------------------------------------------------------------------------------------------------------------------------
Arg:   al_row - long - row number
      as_col - string - name of column with drop-down
------------------------------------------------------------------------------------------------------------------------
Ret:   the lookup-display value (string)
----------------------------------------------------------------------------------------------------------------------*/
return this.uf_lookup_display(al_row, Primary!, as_col)
User avatar
Ursego
Site Admin
 
Posts: 112
Joined: 19 Feb 2013, 20:33

Return to Tips and Tricks

Who is online

Users browsing this forum: No registered users and 1 guest


uf_lookup_display() to obtain DropDown's display value

Share this topic:


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





free counters

eXTReMe Tracker