模块:Html
来自乐园数据管理室
wikitable
wikitable(schema, data)
- schema: 表头
- data: 表格数据
Example
数组方式
wikitable(
{"col1", "col2", "col3"},
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
}
)
字典方式
wikitable(
{
{
["scope"] = "a",
["label"] = "col1"
},
{
["scope"] = "b",
["label"] = "col2"
},
{
["scope"] = "c",
["label"] = "col3"
}
},
{
{["a"] = 1, ["b"] = 2, ["c"] = 3},
{["a"] = 4, ["b"] = 5, ["c"] = 6},
{["a"] = 7, ["b"] = 8, ["c"] = 9}
}
)
local p = {}
function p.wikitable(schema, data)
local out = ""
if schema[1]["scope"] then
-- 数据为dict形式
for i, field in ipairs(schema) do
out = out .. "<th>" .. field["label"] .. "</th>"
end
out = "<tr>" .. out .. "</tr>"
for i, row in ipairs(data) do
local rowHtml = ""
for i, field in ipairs(schema) do
rowHtml = rowHtml .. "<td>" .. row[field["scope"]] .. "</td>"
end
rowHtml = "<tr>" .. rowHtml .. "</tr>"
out = out .. rowHtml
end
else
-- 数据为array形式
for i, label in ipairs(schema) do
out = out .. "<th>" .. label .. "</th>"
end
out = "<tr>" .. out .. "</tr>"
for i, row in ipairs(data) do
local rowHtml = ""
for i, label in ipairs(schema) do
rowHtml = rowHtml .. "<td>" .. row[i] .. "</td>"
end
rowHtml = "<tr>" .. rowHtml .. "</tr>"
out = out .. rowHtml
end
end
out = '<table class="wikitable">' .. out .. "</table>\n"
return out
end
return p