From a8fc4bf73eb00191e99d594b3a98535db8a83a3e Mon Sep 17 00:00:00 2001 From: Ted Trask Date: Thu, 20 Feb 2020 16:59:24 +0000 Subject: Add optgroup support to select input --- lib/htmlviewfunctions.lua | 46 ++++++++++++++++++++++++---------------------- lib/modelfunctions.lua | 20 ++++++++++++++++++-- 2 files changed, 42 insertions(+), 24 deletions(-) diff --git a/lib/htmlviewfunctions.lua b/lib/htmlviewfunctions.lua index b18e35c..253563c 100644 --- a/lib/htmlviewfunctions.lua +++ b/lib/htmlviewfunctions.lua @@ -3,21 +3,29 @@ local mymodule = {} html = require("acf.html") session = require("session") +local function searchoption(option, value) + for x,val in ipairs(option) do + local v,l + if type(val) == "string" then + v = val + l = val + elseif type(val.value) == "string" then + v = val.value + l = val.label + elseif type(val.value) == "table" then + l = searchoption(val.value, value) + if l then return l end + end + if v == value then + return l + end + end +end + local function getlabel(myitem, value) if myitem and (myitem.type == "select" or myitem.type == "multi") then - for x,val in ipairs(myitem.option) do - local v,l - if type(val) == "string" then - v = val - l = val - else - v = val.value - l = val.label - end - if v == value then - return l - end - end + local label = searchoption(myitem.option, value) + if label then return label end end return tostring(value) end @@ -197,7 +205,7 @@ function mymodule.displayformitem(myitem, name, header_level, group) if type(val) == "string" then v = val l = val - else + elseif type(val.value) == "string" then v = val.value l = val.label myitem.disabled = val.disabled @@ -240,14 +248,8 @@ function mymodule.displayformitem(myitem, name, header_level, group) myitem.value = tempval elseif myitem.type == "select" and myitem.readonly then local tempval = myitem.value - if myitem.option and myitem.option[1] and type(myitem.option[1]) == "table" then - for i,o in ipairs(myitem.option) do - if tempval == o.value then - myitem.value = o.label - break - end - end - end + local label = searchoption(myitem.option, myitem.value) + if label then myitem.value = label end io.write((html.form.text(myitem) or "")) myitem.value = tempval elseif html.form[myitem.type] then diff --git a/lib/modelfunctions.lua b/lib/modelfunctions.lua index b72e610..230c15c 100644 --- a/lib/modelfunctions.lua +++ b/lib/modelfunctions.lua @@ -155,8 +155,16 @@ function mymodule.validateselect(select) for i,option in ipairs(select.option) do if type(option) == "string" and option == select.value then return true - elseif type(option) == "table" and option.value == select.value then + elseif type(option.value) == "string" and option.value == select.value then return true + elseif type(option.value) == "table" then + for j,opt in ipairs(option.value) do + if type(opt) == "string" and opt == select.value then + return true + elseif type(opt.value) == "string" and opt.value == select.value then + return true + end + end end end select.errtxt = "Invalid selection" @@ -168,8 +176,16 @@ function mymodule.validatemulti(multi) for i,option in ipairs(multi.option) do if type(option) == "string" then reverseoption[option] = i - else + elseif (type(option.value) == "string") then reverseoption[option.value] = i + elseif (type(option.value) == "table") then + for j,opt in ipairs(option.value) do + if type(opt) == "string" then + reverseoption[opt] = i + elseif (type(opt.value) == "string") then + reverseoption[opt.value] = i + end + end end end for i,value in ipairs(multi.value) do -- cgit v1.2.3