Module:Aligned table: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 18: Line 18:
colstyle[2] = 'text-align:right;'
colstyle[2] = 'text-align:right;'
end
end
-- create the root table
 
local root = mw.html.create('table')
local root = mw.html.create('table')
-- add table style for fullwidth
if fullwidth ~= '' then
if fullwidth ~= '' then
root
root
Line 28: Line 28:
:css('border', 'none')
:css('border', 'none')
end
end
-- add table classes
if class ~= '' then
if class ~= '' then
root:addClass(class)
root:addClass(class)
end
end
-- add table style
if style ~= '' then
if style ~= '' then
root:cssText(style)
root:cssText(style)
end
end
-- build arrays with the column styles and classes
for i = 1,cols do
for i = 1,cols do
colclass[ i ] = colclass[ i ] or ''
colclass[ i ] = colclass[ i ] or ''
Line 53: Line 50:
end
end
end
end
-- compute the maximum cell index
local cellcount = 0
for k, v in pairs( args ) do
for k, v in pairs( args ) do
if type( k ) == 'number' then
if type( k ) == 'number' then
cellcount = math.max(cellcount, k)
i = math.fmod(k-1,cols) + 1
end
local j = (k - i) / cols + 1
end
local cell = mw.html.create('td')
-- compute the number of rows
local rows = math.ceil(cellcount / cols)
-- build the table content
for j=1,rows do
-- start a new row
local row = root:tag('tr')
row:css('vertical-align', 'top')
-- loop over the cells in each row
for i=1,cols do
local cell = row:tag('td')
if args['class' .. tostring(j) .. '.' .. tostring(i)] then
if args['class' .. tostring(j) .. '.' .. tostring(i)] then
cell:addClass(args['class' .. tostring(j) .. '.' .. tostring(i)])
cell:addClass(args['class' .. tostring(j) .. '.' .. tostring(i)])
Line 85: Line 70:
cell:cssText(colstyle[i])
cell:cssText(colstyle[i])
end
end
cell:wikitext(args[rows*(j - 1) + i] or '')
cell:wikitext(v)
entries[ k ] = tostring(cell)
if i == 1 then
entries[ k ] = '<tr style="vertical-align:top">' .. entries[ k ]
end
if i == cols then
entries[ k ] = entries[k] .. '</tr>'
end
         end
         end
     end
     end
-- return the root table  
    root:wikitext(table.concat( entries, '\n' ))
 
     return tostring(root)
     return tostring(root)
  end
  end


return p
return p
Anonymous user