KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Export a Collection to a array based List box column
PRODUCT: 4D | VERSION: 16 | PLATFORM: Mac & Win
Published On: March 26, 2018

Here is a utility method to export a given Collection to a Listbox column:

// ----------------------------------------------------------------------
// Name: EXPORT_COLLECTION_TO_LB_COL
// Description: Method will take a collection and export to a Listbox
// column Object type.
//
// Parameters:
// $1 (COLLECTION) - Collection to export
// $2 (POINTER) - Pointer Listbox column to export
// ----------------------------------------------------------------------
C_COLLECTION($1;$col)
C_POINTER($2;$lbCol)
C_LONGINT($i;$type;$noSetObj)
C_OBJECT($obj)

If (Count parameters=2)
  $col:=$1
  $lbCol:=$2

  CLEAR VARIABLE($lbCol->)

  If (Type($lbCol->)=Object array)

    For ($i;0;$col.length-1)
      $type:=Value type($col[$i])

      $noSetObj:=0

      Case of
        : ($type=Is text)
          OB SET($obj;"valueType";"text";"value";$col[$i])
        : ($type=Is integer)
          OB SET($obj;"valueType";"integer";"value";$col[$i])
        : ($type=Is boolean)
          OB SET($obj;"valueType";"boolean";"value";$col[$i])
        : ($type=Is real)
          OB SET($obj;"valueType";"real";"value";$col[$i])
        : ($type=Is float)
          OB SET($obj;"valueType";"real";"value";$col[$i])
        : ($type=Is date)
          OB SET($obj;"valueType";"date";"value";String(Date($col[$i])))
        : ($type=Is time)
          OB SET($obj;"valueType";"time";"value";String(Time($col[$i])))
        : ($type=Is object)
          OB SET($obj;"valueType";"text";"value";JSON Stringify($col[$i]))
        Else
          $noSetObj:=1
      End case

      APPEND TO ARRAY($lbCol->;$obj)
      CLEAR VARIABLE($obj)
     End for
    End if

End if


Here is an example of using the method:

C_COLLECTION($col)
C_OBJECT($obj)

OB SET($obj1;"prop";"123")

$col:=New collection("Text";254154;3.1416;Current date;false;$obj)

EXPORT_COLLECTION_TO_LB_COL($col;->Column1)


Here is a array based Listbox with a column that is an object array type: