Formspec table fields

User avatar
Tmanyo
Member
 
Posts: 151
Joined: Mon Sep 29, 2014 01:20
GitHub: Tmanyo
IRC: Tmanyo
In-game: tmanyo

Formspec table fields

by Tmanyo » Wed Oct 26, 2016 00:18

I am trying to get the text from the row selected in a table element on a formspec. I thought that it is like every other field, where it returns the text. It returns if it was selected, row, and column. All I want is the text, I don't care anything about the row or column. Is there any way to do this?
Tmanyo
http://rrhmsservers.weebly.com/
Servers I Host:
Xtremetest
Vault-81
 

User avatar
kaeza
Member
 
Posts: 2141
Joined: Thu Oct 18, 2012 05:00
GitHub: kaeza
IRC: kaeza diemartin blaaaaargh
In-game: kaeza

Re: Formspec table fields

by kaeza » Wed Oct 26, 2016 01:19

You need to keep track of the strings yourself.

Bah, wanted to show an easy example but it ended being not so simple. In any case, here's an implementation:

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
-- 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,
})


Example output:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
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


The code you're looking for is in the `on_receive_fields` callback.

HTH
Your signature is not the place for a blog post. Please keep it as concise as possible. Thank you!

Check out my stuff! | Donations greatly appreciated! PayPal | BTC: 1DFZAa5VtNG7Levux4oP6BuUzr1e83pJK2
 

User avatar
Tmanyo
Member
 
Posts: 151
Joined: Mon Sep 29, 2014 01:20
GitHub: Tmanyo
IRC: Tmanyo
In-game: tmanyo

Re: Formspec table fields

by Tmanyo » Wed Oct 26, 2016 21:28

Thank you very much kaeza.
Tmanyo
http://rrhmsservers.weebly.com/
Servers I Host:
Xtremetest
Vault-81
 


Return to Modding Discussion

Who is online

Users browsing this forum: No registered users and 2 guests

cron