- Print
- DarkLight
A record component holds whole data sets in a tabular form. The structure of a record component can be defined by the user, i.e. any number/names of rows or columns can be added.
Typical application cases for a record component are:
- mapping of the possible characteristics of a product
- color tables
- everywhere where the user can make a selection from several possibilities
A record component consists of so-called records, where one record represents a row in the record component. There is always one selected record, which is defined by the selected key. Therefore each record must have a unique key.
The first column in a record component always serves as a key column and, as already mentioned, must be unique. The column does not necessarily have to be called "key" and the type does not necessarily have to be of type number - but in most cases, it makes sense to keep this scheme.
The value of a record component is the selected record, i.e. a row including all column information.
Properties
Name | Description |
Records | the table of records |
selectedKey | the key of the reocrd to choose as value |
value | contains the selected record |
Records
The table of records, from which one can be chosen as the Value.
A table looks like this:
{
{ Key=Number, Name=Text, Price=Number }
{ 1, "Apples", 10 }
{ 2, "Oranges", 20 }
{ 3, "Bananas", 30 }
}
The first row of this table determines which columns it has, all subsequent rows contain values for these columns. A column definition must always consist of a name (like "Key" or "Price") and a type (like "Number" or "Text").
Note that the first column in a table is special: it is used to look up a record with the get function. While generally referred to as the "Key" column, you can give it any name you want. Because it is used to look up records, it must not contain a value that can change over time. In fact, you can only use literal values in a "Key" column, such as Numbers or Text. If you try to use a different value, you will get an error.
For a better overview keep your table well structured. You can use the shortcut strg+b
to automatically format your table.
The examples below assume that there is a record component with the name "fruits" that looks like the one above.
To access the Records of a record component in a different component, you need to use the escape operator:
// Note the # at the beginning of the next line
// Without it, you can't access the 'Records' property
#fruits.Records.GetFirst()
// Without the #, 'fruits' refers to the selected record
// You can access the columns of the selected record directly
fruits.Price
Tipp
You have a product with a top color and a bottom color. Both have the same selection of colors. You create the record components for both. You fill the first record component with your color table. In the second record component, instead of writing the whole table again, you just write #TopColor.Records
and you have the same color table in there. If a new color needs to be added, you only have to add it in the first record component and not in both. This way the data is more maintainable in the editor.
Selected Key
The key of the record that should be selected as the Value. It is always of the same type as the Key column. In many cases, you will want to use a rule like the following (assuming the Key is of type number)
Input or 1
You can connect controls like the dataview to a record component, which will automatically write the Key of a selected item to the selected key property.
Note that by itself, the selected key property has no effect on which record will be chosen – it's not even necessary to use it at all in some situations. In order for it to have an effect, you need to use it in the Value rule, which we will look at next.
Value
Contains the selected record. The default rule for this property looks like the following:
Records.Get(SelectedKey) or Records.GetFirst()
This rule does two things: it first uses the get function to look up the record that has the same key as the selected key. If no such record can be found, it uses the getFirst function to return the first record from the records property.
The Table Type
The value produced by the Records property is called a Table.
Functions
Name | Description |
get(key) | gets the record with the specified key |
getFirst | gets the first record of the table |
add(table) | produces the union of two tables (unique rows from both tables) |
intersect(table) | produces the intersection of two tables (only rows that appear in both tables) |
get(key)
Returns the record with the specified key, or empty if none of the records has that key.
Example | Result |
#fruits.Records.get(1) | [1, "Apples", 10] |
#fruits.Record.get(3) | [3, "Bananas", 30] |
#fruits.Record.get(9) | Empty |
Use the getFirst() function to provide a fallback record if none was found by get:
#fruits.Recods.get(9) or fruits.Records.getFirst()
getFirst
Returns the first record of a table, or empty if there are no records.
#fruits.Records.getFirst() // returns [ 1,"Apples", 10]
add(table)
Produces the union of two tables: returns a table that contains all records from both tables.
#fruits.Records.add(#vegetables.Records)
A few things to keep in mind when using add:
- Both tables must be of the same type, which means they must have the same columns
- If both tables contain a record with the same key, only the first one will be taken. The "first" record is that of the table on which you call the add function – in the example above, that would be #fruits.Records.
intersect(table)
Produces the intersection of two tables: returns a table that contains only records that appear in both tables.
#fruits.Records.intersect(#vegetables.Records)
A few things to keep in mind when using intersect:
- Records are identified by their key (the first column of the record), the other columns do not affect which records are picked
- All records that make it into the result are taken from the first table (fruits in the example above).
- If one of the two tables is empty, the resulting table will also be empty
- The second table (vegetables in the example above) only has to contain the key column, all other columns will be ignored
- it can be of the same type as the first table, but it doesn't have to be