summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTed Trask <ttrask01@yahoo.com>2020-02-20 16:58:02 +0000
committerTed Trask <ttrask01@yahoo.com>2020-02-20 16:58:02 +0000
commit569c929cd2a452f8d44b1f7e02e95f10e0d34dc8 (patch)
treeed4cce775116417171532ee15159f31f546c5b83
parent9c6f375cb398e089e0acb3c7404bd6d9733e4eaa (diff)
downloadacf-lib-569c929cd2a452f8d44b1f7e02e95f10e0d34dc8.tar.bz2
acf-lib-569c929cd2a452f8d44b1f7e02e95f10e0d34dc8.tar.xz
Add optgroup support to select input
-rw-r--r--html.lua40
1 files changed, 27 insertions, 13 deletions
diff --git a/html.lua b/html.lua
index 425fd0b..5bb46a6 100644
--- a/html.lua
+++ b/html.lua
@@ -193,7 +193,8 @@ end
-- v.value is the selected item (or an array if multiple)
--- v.option is an array of valid options (or an array of value, label)
+-- v.option is an array of valid string options or an array of {value, label}
+-- v.option can also contain optgroups (table containing above)
-- NOTE use of value and values (plural)
function mymodule.form.select ( v )
if ( v.name == nil ) then
@@ -224,21 +225,34 @@ function mymodule.form.select ( v )
else
reverseval[v.value]=1
end
- for i, k in ipairs ( v.option ) do
+ local displayoption = function(opt)
local val, label
- if type(k) == "string" then
- val = k
- label = k
- else
- val = k.value
- label = k.label
+ if type(opt) == "string" then
+ val = opt
+ label = opt
+ elseif type(opt.value) == "string" then
+ val = opt.value
+ label = opt.label
+ end
+ if val then
+ str = str .. "<option "
+ if reverseval[val] then
+ str = str .. " selected"
+ reverseval[val] = nil
+ end
+ str = str .. nv_pair("value", val) .. ">" .. mymodule.html_escape(label) .. "</option>"
end
- str = str .. "<option "
- if reverseval[val] then
- str = str .. " selected"
- reverseval[val] = nil
+ end
+ for i,opt in ipairs ( v.option ) do
+ if type(opt) == "table" and type(opt.value) == "table" then
+ str = str .. "<optgroup label=\"" .. mymodule.html_escape(opt.label) .. "\">"
+ for k,opt2 in ipairs( opt.value ) do
+ displayoption(opt2)
+ end
+ str = str .. "</optgroup>"
+ else
+ displayoption(opt)
end
- str = str .. nv_pair("value", val) .. ">" .. mymodule.html_escape(label) .. "</option>"
end
for val in pairs(reverseval) do
str = str .. '<option selected value="' .. mymodule.html_escape(val) ..'">[' .. mymodule.html_escape(val) .. ']</option>'