Page 1 of 1
Create fields dynamically
Posted: Wed Jan 27, 2021 5:12 pm
by CesarJ
The web edit control (#PRIM_MD.Edit) does not have the dataclass property.
I'd like to create fields dynamically in a panel but the field must have/show the field type/length for better usability.
For instance:
- Numeric field 14 with 2 decimals
- Numeric field 7 with 4 decimals
- Numeric field no decimals
- Alphanumeric 20
In VL, I would just use the dataclass and set the Maxlength property accordingly. any ideas?
Code: Select all
Define_Com Class(#PRIM_EDIT) Name(#Edit2) Displayposition(3) Left(49) Parent(#COM_OWNER) Showselection(False) Showselectionhilight(False) Tabposition(3) Top(263) Maxlength(10)
Define_Com Class(#u_dec4) Name(#Dec4)
Set Com(#MyField) Maxlength(#std_num) Dataclass(#Dec4)
Re: Create fields dynamically
Posted: Sun Jan 31, 2021 11:03 am
by Dino
While a code like this one can create the input fields and store the information you want in the "spare" property ComponentTag, so you can adjust things like maxlength, alignment accordingly, the #PRIM_MD.Edit does not really restricts the value to be numbers or characters or to handle the decimals on it properly......
Code: Select all
Begin_Com Role(*EXTENDS #PRIM_WEB) Height(465) Width(881)
Define_Com Class(#STD_NUM.Visual) Name(#STD_NUM) Caption('Number of Fields') Displayposition(1) Height(19) Labeltype(Caption) Left(24) Parent(#COM_OWNER) Tabposition(1) Top(16) Width(193) Usepicklist(False)
Define_Com Class(#PRIM_PHBN) Name(#CREATE_FIELDS) Caption('Create Fields') Displayposition(2) Left(232) Parent(#COM_OWNER) Tabposition(2) Top(16) Width(113)
* Typical PRIM_MD.Edit definition... Componenttag is a "Generic space allowing a value to be stored for the instance" we can reuse it
Define_Com Class(#PRIM_MD.Edit) Name(#Edit1) Caption('Caption') Displayposition(3) Left(432) Parent(#COM_OWNER) Tabposition(3) Top(24) Componenttag('A(5)')
Define_Com Class(#STD_TEXT.EditField) Name(#STD_TEXT) Displayposition(4) Left(441) Parent(#COM_OWNER) Tabposition(4) Top(228)
Define_Com Class(#STD_AMNT.EditField) Name(#STD_AMNT) Displayposition(5) Left(446) Parent(#COM_OWNER) Tabposition(5) Top(129)
* Create one collection of PRIM_MD.Edit
Define_Com Class(#PRIM_KCOL<#PRIM_MD.Edit #STD_NUM>) Name(#EditFields) Reference(*DYNAMIC) Style(Collection)
Evtroutine Handling(#CREATE_FIELDS.Click)
If_Ref Com(#EditFields) Is_Not(*null)
Set_Ref Com(#EditFields) To(*null)
Endif
* Create the collection
Set_Ref Com(#EditFields) To(*Create_as #PRIM_KCOL<#PRIM_MD.Edit #STD_NUM>)
#std_int := 0
Begin_Loop To(#STD_NUM)
* Set the attributes for each one, lets say, alternate between A(20) and S(9)
#std_int += 1
Set_Ref Com(#EditFields<#std_int>) To(*CREATE_AS #PRIM_MD.Edit)
If (#STD_int.mod( 2 ) = 0)
Set Com(#EditFields<#std_int>) Width(200) Displayposition(#std_int) Tabposition(#std_int) Parent(#COM_OWNER) Caption('Field A(20)') Height(55) Top((60 * #std_int)) Componenttag('A(20)') Maxlength(20)
Else
Set Com(#EditFields<#std_int>) Width(200) Displayposition(#std_int) Tabposition(#std_int) Parent(#COM_OWNER) Caption('Field S(9,4)') Height(55) Top((60 * #std_int)) Componenttag('S(9,4)') Maxlength(9) Displayalignment(Right) Editalignment(Right)
Endif
End_Loop
Endroutine
End_Com
.... So, I would be more inclined to use fields like STD_ or my own set of standard fields (kind like Data Types in the Data Modeller) and create collections using those fields. That way, you can have not just control of the content of the field, but even field visualizations of all kind at your disposition, even dates, dropdowns, etc:

- fields0130.jpg (26.09 KiB) Viewed 4598 times
Code: Select all
Begin_Com Role(*EXTENDS #PRIM_WEB) Height(465) Width(881)
Define_Com Class(#STD_NUM.Visual) Name(#STD_NUM) Caption('Number of Fields') Displayposition(1) Height(19) Labeltype(Caption) Left(24) Parent(#COM_OWNER) Tabposition(1) Top(16) Width(193) Usepicklist(False)
Define_Com Class(#PRIM_PHBN) Name(#CREATE_FIELDS) Caption('Create Fields') Displayposition(2) Left(232) Parent(#COM_OWNER) Tabposition(2) Top(16) Width(113)
* Typical PRIM_MD.Edit definition... Componenttag is a "Generic space allowing a value to be stored for the instance" we can reuse it
Define_Com Class(#PRIM_MD.Edit) Name(#Edit1) Caption('Caption') Displayposition(3) Left(432) Parent(#COM_OWNER) Tabposition(3) Top(24) Componenttag('A(5)')
Define_Com Class(#STD_TEXT.EditField) Name(#STD_TEXT) Displayposition(4) Left(441) Parent(#COM_OWNER) Tabposition(4) Top(228)
Define_Com Class(#STD_AMNT.EditField) Name(#STD_AMNT) Displayposition(5) Left(446) Parent(#COM_OWNER) Tabposition(5) Top(129)
Define_Com Class(#STD_DATEX.DateTimeField) Name(#STD_DATEX) Displayposition(6) Left(452) Parent(#COM_OWNER) Tabposition(6) Top(319)
* Create one collection of fields Amounts, another of fields Strings
Define_Com Class(#PRIM_KCOL<#STD_TEXT.EditField #STD_NUM>) Name(#EditFieldsText) Reference(*DYNAMIC) Style(Collection)
Define_Com Class(#PRIM_KCOL<#STD_AMNT.EditField #STD_NUM>) Name(#EditFieldsAmnt) Reference(*DYNAMIC) Style(Collection)
Define_Com Class(#PRIM_KCOL<#STD_DATEX.DateTimeField #STD_NUM>) Name(#EditFieldsDate) Reference(*DYNAMIC) Style(Collection)
Evtroutine Handling(#CREATE_FIELDS.Click)
If_Ref Com(#EditFieldsText) Is_Not(*null)
Set_Ref Com(#EditFieldsText) To(*null)
Endif
If_Ref Com(#EditFieldsAmnt) Is_Not(*null)
Set_Ref Com(#EditFieldsAmnt) To(*null)
Endif
If_Ref Com(#EditFieldsDate) Is_Not(*null)
Set_Ref Com(#EditFieldsDate) To(*null)
Endif
* Create the collection
Set_Ref Com(#EditFieldsText) To(*Create_as #PRIM_KCOL<#STD_TEXT.EditField #STD_NUM>)
Set_Ref Com(#EditFieldsAmnt) To(*Create_as #PRIM_KCOL<#STD_AMNT.EditField #STD_NUM>)
Set_Ref Com(#EditFieldsDate) To(*Create_as #PRIM_KCOL<#STD_DATEX.DateTimeField #STD_NUM>)
#std_int := 0
Begin_Loop To(#STD_NUM)
#std_int += 1
If (#STD_int.mod( 3 ) = 0)
* We create a text field
Set_Ref Com(#EditFieldsText<#std_int>) To(*CREATE_AS #STD_TEXT.EditField)
Set Com(#EditFieldsText<#std_int>) Width(200) Displayposition(#std_int) Tabposition(#std_int) Parent(#COM_OWNER) Caption('Field A(20)') Height(55) Top((60 * #std_int)) Componenttag('A(20)')
Else
If (#STD_int.mod( 3 ) = 1)
* We create a numeric field
Set_Ref Com(#EditFieldsAmnt<#std_int>) To(*CREATE_AS #STD_AMNT.EditField)
Set Com(#EditFieldsAmnt<#std_int>) Width(200) Displayposition(#std_int) Tabposition(#std_int) Parent(#COM_OWNER) Caption('Field S(9,4)') Height(55) Top((60 * #std_int)) Componenttag('S(9,4)') Displayalignment(Right) Editalignment(Right)
Else
* We create a date field
Set_Ref Com(#EditFieldsDate<#std_int>) To(*CREATE_AS #STD_DATEX.DateTimeField)
Set Com(#EditFieldsDate<#std_int>) Displayposition(#std_int) Tabposition(#std_int) Parent(#COM_OWNER) Caption('Date Field') Height(55) Top((60 * #std_int)) Componenttag('DATE()')
Endif
Endif
End_Loop
Endroutine
End_Com