Imagine that your database contains several tables of types or categories. These tables are very simple, often with only an identifier and a text field. For demonstration purposes, let’s say it’s a shoe management database. To qualify the products, we need to define their type, color, pattern, material, season and so on.
It’s cumbersome to manage each of these tables in a list form and an input form and associated methods. In this case, the use of a generic form is essential to avoid duplication and simplify maintenance.
And that’s where dynamic forms and JSON pointers are come in handy!
In this blog post, you’ll see one possible way to build generic forms.
With this database, you can open five different dialogs, as shown below. But if you go back to design mode, you’ll notice that you can’t find any forms in the 4D Explorer…
So, how are these forms created?
In the three steps described below:
1. First, we create a list form that we save in a JSON file. For the specific information on table and fields, we use JSON pointers. For example, let’s say we want to write the table name in the window title. In the dynamic form, we just have to enter:
"windowTitle": { "$ref": "#/info/table" }
Here’s another example, in the list box, we want to define the column data source, so we write:
"dataSource": { "$ref": "#/info/field1" }
2. Then, at runtime, when the user clicks on the menu item, we inject an object defining the table, with the fields the user has chosen.
C_OBJECT($o;$info)
$info:=New object("table";$table; "field1";"["+$table+"]"+"ID"; "field2";"["+$table+"]"+"Name")
$o:=JSON Parse(Document to text("RESOURCES/Form/CategoryOutputForm.json"))
$o.info:=$info
3. To open the form, nothing exceptional is required, just use the Open form window command. The only thing to remember is that 4D resolves the JSON pointers when calling the Open form window command:
$ref:=Open form window($o;Plain form window)
The same can be done with the input form. And now try it for yourself!