%+cbi/valueheader%> <% -- Template Developers: -- - lwb1978 -- - snowie2000 -- Copyright: copyright(c)2025–2027 -- Description: Passwall(2) UI template local json = require "luci.jsonc" local api = self.map.api -- 读取值(keylist/vallist/group) local values = {} for i, key in pairs(self.keylist) do values[#values + 1] = { key = key, label = self.vallist[i] or key, group = self.group and self.group[i] or nil } end -- 获取当前配置值(单值) local current_key = nil local cval = self:cfgvalue(section) if type(cval) == "table" then -- 若意外为table,取第一个 for k,_ in pairs(cval) do current_key = k break end elseif type(cval) == "string" then current_key = (cval:match("%S+")) -- 取第一个 token end -- 分离无组节点(ungrouped)与有组节点(grouped),并保持原有顺序 local ungrouped = {} local groups = {} local group_order = {} for _, item in ipairs(values) do if not item.group or item.group == "" then table.insert(ungrouped, item) else local g = item.group if not groups[g] then groups[g] = {} table.insert(group_order, g) end table.insert(groups[g], item) end end -- 仅共享组名、节点 key/label 及顺序完全相同的分组,保留各控件的候选限制 local group_cache = self.map._lv_group_cache if not group_cache then group_cache = {} self.map._lv_group_cache = group_cache end local group_refs = {} local new_groups = {} for i, g in ipairs(group_order) do -- 使用有序数组生成完整签名,不依赖 JSON 对象的字段顺序或哈希值 local signature = {g} for _, item in ipairs(groups[g]) do signature[#signature + 1] = {item.key, item.label} end local cache_key = json.stringify(signature) local ref = group_cache[cache_key] if not ref then ref = cbid .. ".group." .. i group_cache[cache_key] = ref new_groups[ref] = groups[g] end group_refs[i] = ref end -- 如果没有配置值,默认取第一个(优先无组第一项,否则第一组第一项) if not current_key then if #ungrouped > 0 then current_key = ungrouped[1].key else -- 找到 group_order 第一个有节点的项 for _, g in ipairs(group_order) do if groups[g] and #groups[g] > 0 then current_key = groups[g][1].key break end end end end -- 获取当前 label(用于主控件显示) local function find_label_by_key(k) if not k then return "" end for _, v in ipairs(values) do if v.key == k then return v.label end end return "" end local current_label = find_label_by_key(current_key) or "" -- 控件只携带分组引用,重复的候选数据在页面中仅输出一次 local dropdown_data = { appname = api.appname, current_key = current_key, current_label = current_label, ungrouped = ungrouped, group_refs = group_refs, group_order = group_order, cbid = cbid, } %>