Quantcast
Channel: Rainmeter Forums
Viewing all articles
Browse latest Browse all 1538

Lua Scripting • Generate an HTML Table

$
0
0
This function generates HTML tables from given lua tables.

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("&", "&amp;"):gsub("<", "&lt;"):gsub(">", "&gt;"):gsub('"', "&quot;")end
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 Structure

Code:

function generateHTMLTable(data, headerOrder, headerLabels, class, caption, customHeader, placeholder, extraAttributes)
The function automatically escapes HTML special characters in data values and attributes to prevent rendering issues


htmlTableGenerator_1.0.rmskin
Click on the box to open the generated html file on your browser
Scroll to check the code on the skin.

Parameters
data (table, required)

A non-empty Lua table containing the data to display in the HTML table.

Format: Each row is represented as a Lua table with key-value pairs corresponding to column names and cell values.

Example:

Code:

local data = {{name = "Alice", age = 30, city = "New York"},{name = "Bob", age = 25},{name = "Charlie", city = "San Francisco"}}
headerOrder (table, optional)

A Lua table specifying the column order for the table headers. If not provided or empty, the headers are auto-generated from the keys in the data table.

Example:

Code:

local headerOrder = {"name", "age", "city"}
headerLabels (table, optional)

A lua table specifying custom labels for each key for the data table. If not provided or empty, the keys on the data table are used a headers.

Example:

Code:

headerLabels={name='Student Name',age='Student Age',city='Student City'}
class (string, optional)

A CSS class name to apply to the <table> element.

Example:

Code:

local class = "students-table"
caption (string, optional)

A caption to add to the table. The caption is displayed above the table.

Example:

Code:

local caption = "Student Information"
customHeader (string, optional)

A single header row spanning all columns. This header replaces the standard column headers.

Example:

Code:

local customHeader = "Students"
placeholder (string, optional)

A placeholder string to use for missing or nil values in the data.

Default: "-"

Example:

Code:

local placeholder = "N/A"
extraAttributes (table, optional)

Additional attributes to include in the <table> tag.

Example:

Code:

local extraAttributes = {border = "1", id = "studentTable"}

Statistics: Posted by RicardoTM — Today, 1:42 am



Viewing all articles
Browse latest Browse all 1538

Trending Articles