更多操作
此模块的文档可以在模块:Infobox/item/multidata/doc创建
local p = {}
-- 获取每一格的背景颜色, 规则为:
-- 在奇数行: 深、浅交错; 在偶数行: 浅、深交错
function p.getBackgroundColor(i, perRow)
local isEvenColumn = ((math.floor((i-1)/perRow)%2)==0)
local delta = 0
if not isEvenColumn then delta = 1 end
if (((i-1)%perRow)+delta) % 2 == 1 then return nil end
return '#e5ded6'
end
-- 构建一个数据项
function p.makeDataItem(frame, label, data, bgcolor)
return frame:expandTemplate{ title = 'Infobox/item/data', args = {
label = label,
data = data,
-- width = widthPercent,
bgcolor = bgcolor
} }
end
-- 构建信息框中多行多列的数据项
function p.make()
local frame = mw.getCurrentFrame()
local parent = frame:getParent()
local itemsPerRow = tonumber(parent.args['row-items'])
if itemsPerRow == nil then
error('"row-items" is not a number')
end
-- 将非空的未命名参数 (数据项) 的编号存入 unnamed 表中
local count = 0
local unnamed = {}
for k, v in pairs(parent.args) do
if tonumber(k) ~= nil and v ~= nil and string.gsub(v, '%s+', '') ~= '' then
table.insert(unnamed, k)
count = count + 1
end
end
if count == 0 then return '' end
local result = ''
-- 把所有数据项拼起来
-- local widthPercent = string.format('%.2f', 100/itemsPerRow) .. '%'
local majorPart = math.floor(count / itemsPerRow) * itemsPerRow
local i = 1
while (true) do
-- if i > majorPart then break end
if i > count then
result = result .. '</div>'
break
elseif i % itemsPerRow == 1 then
if i ~= 1 then
result = result .. '</div>'
end
result = result .. '<div style="display: flex">'
end
result = result .. p.makeDataItem(frame, parent.args['label' .. unnamed[i]],
parent.args[unnamed[i]], p.getBackgroundColor(i, itemsPerRow))
i = i + 1
end
return result
end
return p