Tables - guiled/LRE GitHub Wiki
Tables
Introduction
LRE allows you to create tables in the script. These table can be defined with static data, or dynamic (changing) data with a function.
Specific methods
register
Tables.register(tableId: string, data: Array | Object | function)
This method will save in the Tables definitions a new Table with the given data.
Data can be one one following type :
- Array of Objects containing at least an
id
key. Example :Tables.register("Skills", [ { id: "lockpicking", name: "Lock picking", baseAttribute: "AGI" }, { id: "negotiation", name: "Negotiation", baseAttribute: "CHA" }, ]);
- Object whose keys are the record id and values are objects representing the record data. Example :
Tables.register("Skills", { lockPicking: { name: "Lock picking", baseAttribute: "AGI" }, negotiation: { name: "Negotiation", baseAttribute: "CHA" }, });
- A function that returns one of the types above. This function can also get the data from a sheet component. Then the table data will be automatically refreshed in the code (see DataProvider) Example :
// Consider there is a repeater for all the character spells. They are described by their name and level. // The table CharacterSpells will dynamically have all the character spells. Tables.register("CharacterSpells", function () { const spells = sheet.get("spellRepeater"); const data = []; spells.each(function (entry, entryData, entryId) { data.push({ id: entryId, name: entryData.spellName, level: entryData.spellLevel, }); }) return data; });
A registered table can be accessed like a classic table Tables.get("Skills")
. They can also be used as DataProviders for other component's methods. Example :
// The following code will fill a Choice with the names of the character spells
const spellChoice = sheet.get("spellToCast");
const characterSpells = Tables.get("CharacterSpells");
spellChoice.setChoices(characterSpells.select("name"));
Table as DataProvider
Table returned by Tables.get()
is a DataProvider. SO you can use methods like select()
, where()
, join()
, etc.
autonum, autotransl
Data from a table can be automatically converted in number, or translated.
table.get(id) can be dynamic
The parameter given to table.get() can be a component, a function, etc.