Module:TableGenerator: Difference between revisions

From Deadlock Wiki
Jump to navigation Jump to search
Sur (talk | contribs)
mNo edit summary
Sur (talk | contribs)
mNo edit summary
Line 2: Line 2:


function p.generateTable(frame)
function p.generateTable(frame)
     local args = frame:getParent().args
    -- Get the arguments passed to the template
     local cols = tonumber(args["cols"]) or 1 -- Default to 1 column if no value is passed
     local args = frame.args
     local output = {}
     local cols = tonumber(args['cols']) or 3 -- Default number of columns is 3
     local row = {}
 
   
     -- Initialize the table structure
    table.insert(output, '{| class="wikitable"')  -- Start of the table
     local result = '{| class="wikitable"\n'
      
      
     for i, v in ipairs(args) do
    local count = 0
         -- Add the value to the current row
     for i, value in ipairs(args) do
        table.insert(row, string.format('| %s', v))
         -- If the first argument is 'cols', skip it
        if i > 1 then
            -- If it's the start of a new row
            if count % cols == 0 then
                result = result .. '|-\n'
            end


        -- If we have enough columns, close the row and add it to the output
            -- Add the value as a table cell
        if #row == cols then
            result = result .. '| ' .. value .. '\n'
            table.insert(output, '|-\n' .. table.concat(row, ' || '))
             count = count + 1
             row = {}  -- Reset the row
         end
         end
     end
     end
   
    -- Close any unfilled row
    if count % cols ~= 0 then
        result = result .. '|-\n'
    end
    -- Close the table
    result = result .. '|}'


    -- Add any remaining data (in case the last row is incomplete)
     return result
    if #row > 0 then
        table.insert(output, '|-\n' .. table.concat(row, ' || '))
    end
   
    table.insert(output, '|}')  -- End of the table
   
     return table.concat(output, '\n')
end
end


return p
return p

Revision as of 21:28, 14 September 2024

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

local p = {}

function p.generateTable(frame)
    -- Get the arguments passed to the template
    local args = frame.args
    local cols = tonumber(args['cols']) or 3  -- Default number of columns is 3

    -- Initialize the table structure
    local result = '{| class="wikitable"\n'
    
    local count = 0
    for i, value in ipairs(args) do
        -- If the first argument is 'cols', skip it
        if i > 1 then
            -- If it's the start of a new row
            if count % cols == 0 then
                result = result .. '|-\n'
            end

            -- Add the value as a table cell
            result = result .. '| ' .. value .. '\n'
            count = count + 1
        end
    end
    
    -- Close any unfilled row
    if count % cols ~= 0 then
        result = result .. '|-\n'
    end

    -- Close the table
    result = result .. '|}'

    return result
end

return p