模块:Infobox/item/multidata:修订间差异

来自Cookie☆Wiki
无编辑摘要
无编辑摘要
第31行: 第31行:
while (true) do
while (true) do
if i > majorPart then break end
if i > majorPart then break end
if i % itemsPerRow == 1 then result = result .. '<div style="display: flex">' end
result = result .. frame:expandTemplate{ title = 'Infobox/item/data', args = {
result = result .. frame:expandTemplate{ title = 'Infobox/item/data', args = {
width = widthPercent, data = unnamed[i], bgcolor = p.getBackgroundColor(i)
width = widthPercent, data = unnamed[i], bgcolor = p.getBackgroundColor(i)
} }
} }
if i % itemsPerRow == 0 then result = result .. '</div>' end
i = i + 1
i = i + 1
end
end
第40行: 第42行:
if remain ~= 0 then
if remain ~= 0 then
widthPercent = string.format('%.2f', 100/(remain)) .. '%'
widthPercent = string.format('%.2f', 100/(remain)) .. '%'
result = result .. '<div style="display: flex">'
while (true) do
while (true) do
if i > count then break end
if i > count then break end
第47行: 第50行:
i = i + 1
i = i + 1
end
end
result = result .. '</div>'
end
end



2019年1月13日 (日) 04:28的版本

可在模块:Infobox/item/multidata/doc创建此模块的帮助文档

local p = {}

function p.getBackgroundColor(i)
    if i % 2 == 1 then return nil end
    return '#e5ded6'
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
    
    local count = 0
    local unnamed = {}
    for k, v in pairs(parent.args) do
        if tonumber(k) ~= nil and v ~= nil and v ~= '' then
            table.insert(unnamed, v)
            count = count + 1

        end
    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 % itemsPerRow == 1 then result = result .. '<div style="display: flex">' end
        result = result .. frame:expandTemplate{ title = 'Infobox/item/data', args = {
            width = widthPercent, data = unnamed[i], bgcolor = p.getBackgroundColor(i)
        } }
        if i % itemsPerRow == 0 then result = result .. '</div>' end
        i = i + 1
    end

    local remain = count - majorPart
    if remain ~= 0 then
        widthPercent = string.format('%.2f', 100/(remain)) .. '%'
        result = result .. '<div style="display: flex">'
        while (true) do
            if i > count then break end
            result = result .. frame:expandTemplate{ title = 'Infobox/item/data', args = {
                width = widthPercent, data = unnamed[i], bgcolor = p.getBackgroundColor(i)
            } }
            i = i + 1
        end
        result = result .. '</div>'
    end

    return result
end

return p