Module:Alternating rows table section

From Chalo Chatu, Zambia online encyclopedia
Jump to navigationJump to search

This module emits table rows with alternating classes/styles (zebra striping). It is a lightweight fallback compatible with templates that expect a function named "main" (and alias "section").

Overview

Invoke inside a wikitable to output multiple rows. Each positional parameter (1, 2, 3, …) should contain one or more table cells (e.g., `!` or `|` lines).

Basic form:


Parameters

All parameters are optional unless noted.

1..N
Positional row definitions. Each should contain complete cell markup for that row (e.g., `!` and `|` cells).
start
First positional index to read. Default 1.
limit
Maximum rows to process. Default 200.

Styling hooks:

rowstyle
Inline CSS applied to every row.
oddclass / odd
CSS class for odd rows (1st, 3rd, …).
evenclass / even
CSS class for even rows (2nd, 4th, …).
oddstyle
Inline CSS appended on odd rows.
evenstyle
Inline CSS appended on even rows.

Examples

Minimal

Party Votes %

With classes

Assuming site CSS defines .row-odd and .row-even:

Party Votes %

With inline styles

Party Votes %

Notes

  • This module does not build cells; each positional parameter should supply full cell markup for its row.
  • If your template used a function named "section", this module provides an alias: works the same as "main".
  • To change the visual look, define classes in MediaWiki:Common.css and pass them via oddclass/evenclass.

Troubleshooting

  • Lua error: attempt to call field 'main' (a nil value)
Ensure the code page at Module:Alternating rows table section contains a function `p.main` and returns `p`. The /doc subpage should not contain Lua.
  • Rows appear but no zebra effect
Provide oddclass/evenclass and define those classes in site CSS, or use oddstyle/evenstyle inline.
  • Nothing renders
Check that each positional parameter includes valid table cell markup (e.g., starts with `|` or `!`).

-- Minimal Alternating rows table section
-- Purpose: emit table rows with alternating classes. Safe fallback that
--          works even if callers pass fully formatted cell content.
local p = {}

local function getArgs(frame)
	-- prefer parent args, then direct args
	local parent = frame:getParent()
	return parent and parent.args or frame.args
end

function p.main(frame)
	local args = getArgs(frame)

	-- Config (all optional)
	local rowstyle   = args.rowstyle or ""              -- applied to every row
	local oddclass   = args.oddclass or args.odd or ""  -- class for 1st,3rd,...
	local evenclass  = args.evenclass or args.even or ""-- class for 2nd,4th,...
	local oddstyle   = args.oddstyle or ""              -- inline style for odd rows
	local evenstyle  = args.evenstyle or ""             -- inline style for even rows
	local startIndex = tonumber(args.start) or 1        -- first positional param to read
	local limit      = tonumber(args.limit) or 200      -- max rows to scan

	local out = {}
	local i = 0

	for n = startIndex, startIndex + limit - 1 do
		local v = args[n]
		if v and v ~= "" then
			i = i + 1
			local cls = (i % 2 == 1) and oddclass or evenclass
			local sty = (i % 2 == 1) and oddstyle or evenstyle

			local pieces = { "|-" }
			if cls ~= "" then table.insert(pieces, ' class="' .. cls .. '"') end

			local combinedStyle = {}
			if rowstyle ~= "" then table.insert(combinedStyle, rowstyle) end
			if sty ~= "" then table.insert(combinedStyle, sty) end
			if #combinedStyle > 0 then
				table.insert(pieces, ' style="' .. table.concat(combinedStyle, "; ") .. '"')
			end

			table.insert(pieces, "\n")
			table.insert(pieces, tostring(v))
			table.insert(pieces, "\n")

			table.insert(out, table.concat(pieces))
		end
	end

	return table.concat(out)
end

-- Back-compat alias some templates might call
p.section = p.main

return p