模块

模块:词典表格

来自卡厄思梦境WIKI

律Rhyme留言 | 贡献2025年10月9日 (四) 14:02的版本

此模块的文档可以在模块:词典表格/doc创建

local p = {}
local data = mw.loadData("模块:词典/data")

function p.makeTable(frame)
    local html = mw.html.create()
    
    -- 创建表格
    local table = html:tag('table')
        :addClass('wikitable sortable')
        :css('width', '100%')
        :attr('id', 'CardSelectTr')
    
    -- 创建表头
    local thead = table:tag('thead'):tag('tr'):attr('id', 'CardSelectTabHeader')
    thead:tag('th'):css('width', '80px'):wikitext('图标'):addClass('dataHeader')
    thead:tag('th'):css('width', '150px'):wikitext('名称'):addClass('dataHeader headerSort')
    thead:tag('th'):css('width', '150px'):wikitext('类型'):addClass('dataHeader headerSort')
    thead:tag('th'):wikitext('描述'):addClass('dataHeader')
    
    -- 创建表格主体
    local tbody = table:tag('tbody')
    
    -- 遍历数据
    for name, entries in pairs(data.dictionary) do
        for _, entry in ipairs(entries) do
            local row = tbody:tag('tr')
            
            -- 使用 cssText 方法添加 data 属性
            local dataType = entry['类型'] or ''
            local dataColor = entry['颜色'] or ''
            row:attr('data-paramType', dataType)  -- 改用英文属性名
            row:attr('data-paramColor', dataColor)
            
            -- 图标列
            if entry['icon'] and entry['icon'] ~= '' then
                row:tag('td'):wikitext('[[File:' .. entry['icon'] .. '|50px]]')
            else
                row:tag('td'):wikitext('暂无图标')
            end
            
            -- 名称列
            row:tag('td'):wikitext(name)
            
            -- 类型列(添加颜色)
            local typeCell = row:tag('td')
            local typeText = entry['类型'] or ''
            local color = entry['颜色'] or ''
            
            if color == '橙' then
                typeCell:css('color', '#ff8c00'):wikitext(typeText)
            elseif color == '蓝' then
                typeCell:css('color', '#4169E1'):wikitext(typeText)
            elseif color == '红' then
                typeCell:css('color', '#DC143C'):wikitext(typeText)
            else
                typeCell:wikitext(typeText)
            end
            
            -- 描述列
            row:tag('td'):wikitext(entry['描述'] or '')
        end
    end
    
    return tostring(html)
end

return p