This function generates HTML tables from given lua tables.
The generateHTMLTable function generates an HTML table string from a Lua table of data. It provides customization options for headers, captions, CSS classes, placeholders, and other table attributes.
Function StructureThe function automatically escapes HTML special characters in data values and attributes to prevent rendering issues
Click on the box to open the generated html file on your browser
Scroll to check the code on the skin.
Parameters
Code:
function generateHTMLTable(data, headerOrder, headerLabels, class, caption, customHeader, placeholder, extraAttributes)-- Validate inputif type(data) ~= "table" or #data == 0 thenerror("Invalid input: 'data' must be a non-empty table.")end-- Automatically generate headerOrder if not providedif not headerOrder or #headerOrder == 0 thenheaderOrder = {}for key in pairs(data[1]) dotable.insert(headerOrder, key)endendplaceholder = placeholder or "-" -- Default placeholder for missing data-- Generate HTML attributeslocal attributes = {}if class thentable.insert(attributes, 'class="' .. escapeHTML(class) .. '"')endif type(extraAttributes) == "table" thenfor key, value in pairs(extraAttributes) dotable.insert(attributes, key .. '="' .. escapeHTML(value) .. '"')endendlocal attrString = " " .. table.concat(attributes, " ")-- Start building the HTML tablelocal html = {"<table" .. attrString .. ">\n"}-- Add table caption if providedif caption thentable.insert(html, " <caption>" .. escapeHTML(caption) .. "</caption>\n")end-- Add theadtable.insert(html, " <thead>\n")if customHeader then-- Custom header spans all columnslocal colCount = #headerOrdertable.insert(html, " <tr>\n")table.insert(html, ' <th colspan="' .. colCount .. '">' .. escapeHTML(customHeader) .. "</th>\n")table.insert(html, " </tr>\n")else-- Generate standard header row with custom labels if providedtable.insert(html, " <tr>\n")for _, header in ipairs(headerOrder) dolocal headerLabel = headerLabels and headerLabels[header] or headertable.insert(html, " <th>" .. escapeHTML(headerLabel) .. "</th>\n")endtable.insert(html, " </tr>\n")endtable.insert(html, " </thead>\n")-- Add table rowstable.insert(html, " <tbody>\n")for _, row in ipairs(data) dotable.insert(html, " <tr>\n")for _, header in ipairs(headerOrder) dolocal cell = row[header] or placeholdertable.insert(html, " <td>" .. escapeHTML(cell) .. "</td>\n")endtable.insert(html, " </tr>\n")endtable.insert(html, " </tbody>\n</table>")table.insert(html, " <br>") --End with a line break.-- Return the concatenated HTMLreturn table.concat(html)end-- Function to escape HTMLfunction escapeHTML(str) return tostring(str):gsub("&", "&"):gsub("<", "<"):gsub(">", ">"):gsub('"', """)end
Function Structure
Code:
function generateHTMLTable(data, headerOrder, headerLabels, class, caption, customHeader, placeholder, extraAttributes)
Click on the box to open the generated html file on your browser
Scroll to check the code on the skin.
Parameters
Statistics: Posted by RicardoTM — Today, 1:42 am