Repository navigation
|
I have a macro that has a hard coded database name. I want to make it more general to allow the user to choose the database from a list of those available. Therefore I have added a form with a field of type "choice" and want to populate it with the databases returned by opensak.databases. However I cannot see how to use set_options on the field. In all the example macros field contents seem to be statically defined. |
Replies: 1 comment 1 reply
|
You don’t need set_options for this. A field’s options is an ordinary Lua table, so you can build the list before you open the form. opensak.databases() returns one table per database (name, path, active, size_mb), not plain strings, so collect the names first: local names, current = {}, nil local v = opensak.form{ print("Selected: " .. v.db) f:set_options(id, options) is meant for changing the list while the form is open. You call it on the form handle that callbacks get: on_open, on_change or a button’s on_click. For example, to fill the list when the form opens: on_open = function(f) or to refill a second choice field when the first one changes. The current value is kept if it’s still one of the new options. Docs: opensak.databases, opensak.form and “Form handle” in docs/macros/api.md. You can also see these in the Help in the macro editor. |
You don’t need set_options for this. A field’s options is an ordinary Lua table, so you can build the list before you open the form. opensak.databases() returns one table per database (name, path, active, size_mb), not plain strings, so collect the names first:
local names, current = {}, nil
for _, db in ipairs(opensak.databases()) do
names[#names + 1] = db.name
if db.active then current = db.name end
end
local v = opensak.form{
title = "Choose database",
fields = {
{ type = "choice", id = "db", label = "Database",
options = names, default = current },
},
}
if not v then return end -- cancelled
print("Selected: " .. v.db)
-- e.g. read it without switching:
-- for c in opensak.caches{ data…