Module:GenericSortable

From August Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:GenericSortable/doc

-- Module:GenericTable
local p = {}

-- Main function to generate a customizable table
function p.main(frame)
    local args = frame.args
    
    -- Get table settings
    local tableClass = args['tableClass'] or "wikitable sortable"
    local tableStyle = args['tableStyle'] or "text-align:center;"
    
    -- Get column count and headings
    local colCount = tonumber(args['columns'] or 3)
    local headings = {}
    for i = 1, colCount do
        headings[i] = args['heading' .. i] or 'Heading ' .. i
    end
    
    -- Start building the table HTML
    local html = '<table class="' .. tableClass .. '" style="' .. tableStyle .. '">\n'
    
    -- Add the header row
    html = html .. '  <tr>\n'
    for i = 1, colCount do
        local colspan = args['heading' .. i .. 'colspan'] or 1
        html = html .. '    <th'
        if tonumber(colspan) > 1 then
            html = html .. ' colspan="' .. colspan .. '"'
        end
        html = html .. ' class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">' 
            .. headings[i] .. '</th>\n'
    end
    html = html .. '  </tr>\n'
    
    -- Generate rows dynamically
    local rowIndex = 1
    while args['row' .. rowIndex .. 'cell1'] or args['row' .. rowIndex] do
        html = html .. '  <tr>\n'
        
        -- Add cells to this row
        for i = 1, colCount do
            local cellContent = args['row' .. rowIndex .. 'cell' .. i] or ''
            local cellClass = args['row' .. rowIndex .. 'cell' .. i .. 'class'] or ''
            local cellStyle = args['row' .. rowIndex .. 'cell' .. i .. 'style'] or ''
            
            html = html .. '    <td'
            if cellClass ~= '' then
                html = html .. ' class="' .. cellClass .. '"'
            end
            if cellStyle ~= '' then
                html = html .. ' style="' .. cellStyle .. '"'
            end
            html = html .. '>' .. cellContent .. '</td>\n'
        end
        
        html = html .. '  </tr>\n'
        rowIndex = rowIndex + 1
    end
    
    -- Close the table
    html = html .. '</table>'
    return html
end

-- Function to render HTML from template-style parameters
function p.render(frame)
    -- Get all arguments from parent template
    local parentArgs = frame:getParent().args
    return p.main({args = parentArgs})
end

return p