-- Number of columns in our table.
local NCOLUMNS = 3
-- Form name.
local FORMNAME = "tabletest:form"
-- Our table data. An array of sub-tables representing rows.
local my_table = {
{ "foo", "Samuel", "lalalala" },
{ "bar", "Samantha", "asdf" },
}
-- Convert a table row to a string.
local function build_table_row(row)
local tmp = { }
for i, cell in ipairs(row) do
tmp[i] = minetest.formspec_escape(cell)
end
return table.concat(tmp, ",")
end
local function build_table_contents(tbl)
local tmp = { }
for i, row in ipairs(tbl) do
tmp[i] = build_table_row(row)
end
return table.concat(tmp, ",")
end
local function build_table_header(ncols)
local tmp = { }
for i = 1, ncols do
-- You can probably associate the type with the column above.
-- Won't go over that here.
tmp[i] = "text"
end
return table.concat(tmp, ";")
end
local function build_table(tbl, x, y, w, h, name, selidx)
local fshdr = "tablecolumns["..build_table_header(#tbl[1]).."]"
local fstbl = "table["..x..","..y..";"..w..","..h..";"..name
..";"..build_table_contents(tbl)..";"..(selidx or "").."]"
return fshdr..fstbl
end
local function build_form(tbl)
local fssize = "size[12, 9]"
local fstbl = build_table(tbl, 0, 0, 12, 9, "somefield")
return fssize..fstbl
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= FORMNAME then return end -- Only handle our form
if fields.somefield then
local ev = minetest.explode_table_event(fields.somefield)
if ev.type == "INV" then
print("Element deselected: row="..ev.row..", column="..ev.column)
elseif ev.type == "CHG" then
print("Selection changed: row="..ev.row..", column="..ev.column)
elseif ev.type == "DCL" then
print("Double clicked: row="..ev.row..", column="..ev.column)
end
local row, column = tonumber(ev.row) or 0, tonumber(ev.column) or 0
if row > 0 and column > 0 then
local text = my_table[row][column]
print("Text: "..(text or "NO TEXT???"))
end
end
end)
minetest.register_chatcommand("foo", {
description = "foo",
params = "",
func = function(name)
minetest.show_formspec(name, FORMNAME,
build_form(my_table))
end,
})
Element deselected: row=0, column=0
Selection changed: row=2, column=0
Selection changed: row=2, column=3
Text: asdf
Selection changed: row=2, column=2
Text: Samantha
Selection changed: row=1, column=2
Text: Samuel
Selection changed: row=1, column=3
Text: lalalala
Users browsing this forum: No registered users and 2 guests