Formspec table fields

Post Reply
User avatar
Tmanyo
Member
Posts: 196
Joined: Mon Sep 29, 2014 01:20
GitHub: Tmanyo
IRC: Tmanyo
In-game: tmanyo
Location: United States
Contact:

Formspec table fields

by Tmanyo » Post

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://www.rrhmsservers.ml
Servers I Host:
Tmanyo-Realism
Mods of mine that I don't totally hate:
Bank Accounts
T-Rating
Tmusic Player

User avatar
kaeza
Moderator
Posts: 2162
Joined: Thu Oct 18, 2012 05:00
GitHub: kaeza
IRC: kaeza diemartin blaaaaargh
In-game: kaeza
Location: Montevideo, Uruguay
Contact:

Re: Formspec table fields

by kaeza » Post

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:

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:

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

User avatar
Tmanyo
Member
Posts: 196
Joined: Mon Sep 29, 2014 01:20
GitHub: Tmanyo
IRC: Tmanyo
In-game: tmanyo
Location: United States
Contact:

Re: Formspec table fields

by Tmanyo » Post

Thank you very much kaeza.
Tmanyo
http://www.rrhmsservers.ml
Servers I Host:
Tmanyo-Realism
Mods of mine that I don't totally hate:
Bank Accounts
T-Rating
Tmusic Player

Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests