模块

词典表格:修订间差异

来自卡厄思梦境WIKI

律Rhyme留言 | 贡献
无编辑摘要
律Rhyme留言 | 贡献
无编辑摘要
第3行: 第3行:


function p.makeTable(frame)
function p.makeTable(frame)
     local html = mw.html.create()
     local result = {}
      
      
     -- 创建表格
     -- 表格开始
     local table = html:tag('table')
     table.insert(result, '<table class="wikitable sortable" style="width:100%" id="CardSelectTr">')
        :addClass('wikitable sortable')
        :css('width', '100%')
        :attr('id', 'CardSelectTr')
      
      
     -- 创建表头
     -- 表头
     local thead = table:tag('thead'):tag('tr'):attr('id', 'CardSelectTabHeader')
     table.insert(result, '<thead>')
     thead:tag('th'):css('width', '80px'):wikitext('图标'):addClass('dataHeader')
    table.insert(result, '<tr id="CardSelectTabHeader">')
     thead:tag('th'):css('width', '150px'):wikitext('名称'):addClass('dataHeader headerSort')
     table.insert(result, '<th style="width:80px" class="dataHeader">图标</th>')
     thead:tag('th'):css('width', '150px'):wikitext('类型'):addClass('dataHeader headerSort')
     table.insert(result, '<th style="width:150px" class="dataHeader headerSort">名称</th>')
     thead:tag('th'):wikitext('描述'):addClass('dataHeader')
     table.insert(result, '<th style="width:150px" class="dataHeader headerSort">类型</th>')
     table.insert(result, '<th class="dataHeader">描述</th>')
    table.insert(result, '</tr>')
    table.insert(result, '</thead>')
      
      
     -- 创建表格主体
     -- 表格主体
     local tbody = table:tag('tbody')
     table.insert(result, '<tbody>')
      
      
     -- 遍历数据
     -- 先收集所有数据并排序
    local allEntries = {}
     for name, entries in pairs(data.dictionary) do
     for name, entries in pairs(data.dictionary) do
         for _, entry in ipairs(entries) do
         for _, entry in ipairs(entries) do
             local row = tbody:tag('tr')
             table.insert(allEntries, {
           
                 name = name,
            -- 使用 cssText 方法添加 data 属性
                entry = entry
            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
     end
     end
      
      
     return tostring(html)
     -- 按类型排序
    table.sort(allEntries, function(a, b)
        local typeA = a.entry['类型'] or ''
        local typeB = b.entry['类型'] or ''
        if typeA == typeB then
            return a.name < b.name
        end
        return typeA < typeB
    end)
   
    -- 遍历排序后的数据
    for _, item in ipairs(allEntries) do
        local name = item.name
        local entry = item.entry
        local dataType = entry['类型'] or ''
        local dataColor = entry['颜色'] or ''
       
        table.insert(result, string.format('<tr data-param类型="%s" data-param颜色="%s">',
            mw.text.encode(dataType), mw.text.encode(dataColor)))
       
        -- 图标列
        if entry['icon'] and entry['icon'] ~= '' then
            table.insert(result, '<td>[[File:' .. entry['icon'] .. '|50px]]</td>')
        else
            table.insert(result, '<td>暂无图标</td>')
        end
       
        -- 名称列
        table.insert(result, '<td>' .. mw.text.encode(name) .. '</td>')
       
        -- 类型列(添加颜色)
        local typeText = entry['类型'] or ''
        local color = entry['颜色'] or ''
        local colorStyle = ''
       
        if color == '橙' then
            colorStyle = ' style="color:#ff8c00"'
        elseif color == '蓝' then
            colorStyle = ' style="color:#4169E1"'
        elseif color == '红' then
            colorStyle = ' style="color:#DC143C"'
        end
       
        table.insert(result, '<td' .. colorStyle .. '>' .. mw.text.encode(typeText) .. '</td>')
       
        -- 描述列
        table.insert(result, '<td>' .. (entry['描述'] or '') .. '</td>')
       
        table.insert(result, '</tr>')
    end
   
    table.insert(result, '</tbody>')
    table.insert(result, '</table>')
   
    return frame:preprocess(table.concat(result, '\n'))
end
end


return p
return p

2025年10月9日 (四) 14:05的版本

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

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

function p.makeTable(frame)
    local result = {}
    
    -- 表格开始
    table.insert(result, '<table class="wikitable sortable" style="width:100%" id="CardSelectTr">')
    
    -- 表头
    table.insert(result, '<thead>')
    table.insert(result, '<tr id="CardSelectTabHeader">')
    table.insert(result, '<th style="width:80px" class="dataHeader">图标</th>')
    table.insert(result, '<th style="width:150px" class="dataHeader headerSort">名称</th>')
    table.insert(result, '<th style="width:150px" class="dataHeader headerSort">类型</th>')
    table.insert(result, '<th class="dataHeader">描述</th>')
    table.insert(result, '</tr>')
    table.insert(result, '</thead>')
    
    -- 表格主体
    table.insert(result, '<tbody>')
    
    -- 先收集所有数据并排序
    local allEntries = {}
    for name, entries in pairs(data.dictionary) do
        for _, entry in ipairs(entries) do
            table.insert(allEntries, {
                name = name,
                entry = entry
            })
        end
    end
    
    -- 按类型排序
    table.sort(allEntries, function(a, b)
        local typeA = a.entry['类型'] or ''
        local typeB = b.entry['类型'] or ''
        if typeA == typeB then
            return a.name < b.name
        end
        return typeA < typeB
    end)
    
    -- 遍历排序后的数据
    for _, item in ipairs(allEntries) do
        local name = item.name
        local entry = item.entry
        local dataType = entry['类型'] or ''
        local dataColor = entry['颜色'] or ''
        
        table.insert(result, string.format('<tr data-param类型="%s" data-param颜色="%s">', 
            mw.text.encode(dataType), mw.text.encode(dataColor)))
        
        -- 图标列
        if entry['icon'] and entry['icon'] ~= '' then
            table.insert(result, '<td>[[File:' .. entry['icon'] .. '|50px]]</td>')
        else
            table.insert(result, '<td>暂无图标</td>')
        end
        
        -- 名称列
        table.insert(result, '<td>' .. mw.text.encode(name) .. '</td>')
        
        -- 类型列(添加颜色)
        local typeText = entry['类型'] or ''
        local color = entry['颜色'] or ''
        local colorStyle = ''
        
        if color == '橙' then
            colorStyle = ' style="color:#ff8c00"'
        elseif color == '蓝' then
            colorStyle = ' style="color:#4169E1"'
        elseif color == '红' then
            colorStyle = ' style="color:#DC143C"'
        end
        
        table.insert(result, '<td' .. colorStyle .. '>' .. mw.text.encode(typeText) .. '</td>')
        
        -- 描述列
        table.insert(result, '<td>' .. (entry['描述'] or '') .. '</td>')
        
        table.insert(result, '</tr>')
    end
    
    table.insert(result, '</tbody>')
    table.insert(result, '</table>')
    
    return frame:preprocess(table.concat(result, '\n'))
end

return p