uf_lookup_display() to obtain DropDown's display value


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_expr = "Evaluate('LookUpDisplay(" + as_col + ")', " + String(al_row) + ")"
ls_display_value = dw_test.Describe(ls_expr)

Unfortunately, that method works only for the Primary! buffer :( . PB has no built-in means to do the same with other buffers. You can ask: how do you want to get the displayed value of a field which is not displayed :lol: ? Sometimes, we need the value which would be displayed if the record would be in the Primary! buffer - for example, to appear in an error message in a pre-save validation when the failed row is in the Filter! or Delete! buffer.

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

Code: Select all
/**********************************************************************************************************************
Dscr:         Returns the lookup-display value of a drop-down. Deals with any buffer of the DW.
            To deal with the primary buffer only, use the overloaded function with 2 arguments.
            More details: http://code.intfast.ca/viewtopic.php?f=4&t=89
***********************************************************************************************************************
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)
***********************************************************************************************************************
Developer:   Michael Zuskin -  http://linkedin.com/in/zuskin | http://code.intfast.ca/
**********************************************************************************************************************/
datetime            ldt_val
date               ld_val
time               lt_val
int               li_dwc_row
string            ls_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
   ls_expr = "Evaluate('LookUpDisplay(" + as_col + ")', " + String(al_row) + ")"
   ls_display_value = this.Describe(ls_expr)
   return ls_display_value
end if

// For Filter! & Delete! buffers, we need to work harder since LookUpDisplay doesn't work with them...

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 the 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. That will not shorten your scripts, but, anyway, it's slightly more elegant then using the Describe() method):

Code: Select all
/**********************************************************************************************************************
Dscr:         Returns the lookup-display value of a drop-down. Deals with the Primary! buffer only.
            To deal also with other buffers, use the overloaded function with 3 arguments.
            More details: http://code.intfast.ca/viewtopic.php?f=4&t=89
***********************************************************************************************************************
Arg:         al_row - long - row number
            as_col - string - name of column with drop-down
***********************************************************************************************************************
Ret:         the lookup-display value (string)
***********************************************************************************************************************
Developer:   Michael Zuskin -  http://linkedin.com/in/zuskin | http://code.intfast.ca/
**********************************************************************************************************************/
return this.uf_lookup_display(al_row, Primary!, as_col)
User avatar
Ursego
Site Admin
 
Posts: 143
Joined: 19 Feb 2013, 20:33



Ketones are a more high-octane fuel for your brain than glucose. Become a biohacker and upgrade yourself to version 2.0!



cron
Traffic Counter

eXTReMe Tracker