Module:Tablebuilder

-- Meta-Module Creates a new html table using the functions and mw.html library Variables prefixed with h are mw.html nodes. e.g. hTable is created using mw.html.create('table') -- local p = {} local tableModel = {} local libraryUtil = require( 'libraryUtil' ) -- //Lazy load methods local createCol,appendRow,appendCol,createRow -- Creates table model self.arrTable - a Two dimensional array of the table ,e.g. {{'header','header2'},{"cell1","cell2"}} sTableDescription - Caption of the table (e.g."green") sStyle - Table style ( e.g. sStyle="color:green") tClass - Table classes (e.g. {"wikitable","sortable"}) -- function tableModel.new(arrExtData,sTableDescription, sStyle,tClass) local self = {} local checkSelf = libraryUtil.makeCheckSelfFunction( 'tableModel', 'obj', self, 'tableModel object' ) self.tableFormatting = {caption="",class={},style=""} self.arrTable = {} function isSelfCheck(self) if not self then error("Syntax error, use colon, e.g. self:addcol not self.addcol") end end function self:getAttribs(iRow,iCol) checkSelf(self,"getAttribs") if (self.arrTable[iRow] and self.arrTable[iRow][iCol]) then return self.arrTable[iRow][iCol] end end function self:setCellAttr(iRow,iCol,oCellAttributes,oAttrVal) checkSelf(self,"setCellAttr") if (self.arrTable[iRow] and self.arrTable[iRow][iCol] ) then local oTableData = self:getAttribs(iRow,iCol) local sCellVal = oTableData.sValue if oCellAttributes and type (oCellAttributes)=="string" then oTableData.oFormatting = {[oCellAttributes]=oAttrVal} end if (type(oCellAttributes)=="table" and next(oCellAttributes)) then oTableData.oFormatting = oCellAttributes end end end function self:setCell (sValue,iRow,iCol,oFormatting,bHeader) checkSelf(self,"setCell") if self.arrTable[iRow] and (self.arrTable[iRow][iCol]) then local oTableData = self:getAttribs(iRow,iCol) oTableData.sValue = sValue oTableData.bHeader = bHeader if (not(self.arrTable[iRow]["rowdata"])) then self.arrTable[iRow]["rowdata"]={} end self:setCellAttr(iRow,iCol,oFormatting) end end function self:setTable(arrInput) checkSelf(self,"setTable") local bHeader if arrInput and type(arrInput) =="table" then for iRow,tCols in pairs(arrInput) do               if type(tCols)=="table" then self.arrTable[iRow] = {} for iCol,sValue in ipairs(tCols) do                       bHeader = (iRow==1) self.arrTable[iRow][iCol]={} self:setCell (sValue,iRow,iCol,{},bHeader) end else bHeader = true self.arrTable[1] = self.arrTable[1] or {} local iCol = #self.arrTable[1]+1 self.arrTable[1][iCol] = {} self:setCell (tCols,1,iCol,{},bHeader) end end end end -- Sets a field in arrtable function self:setData(iRow,iCol,sField, sNewValue) checkSelf(self,"self") if (self.arrTable[iRow] and self.arrTable[iRow][iCol]           and self.arrTable[iRow][iCol][sField]) then local arrMetaData = self.arrTable[iRow][iCol] arrMetaData[sField] = sNewValue end end function self:getCell(iRow,iCol) checkSelf(self,"getCell") if self.arrTable[iRow] and self.arrTable[iRow][iCol] then return self.arrTable[iRow][iCol].sValue end end function self:setCellStyle(iRow,iCol,sCellStyle,sAttrVal) self:setCellAttr(iRow,iCol,sCellStyle,sAttrVal) end function self:setCellHeader(iRow,iCol,bHeader) checkSelf(self,"setCellHeader") if self.arrTable[iRow] and self.arrTable[iRow][iCol] then local oTableData = self:getAttribs(iRow,iCol) local sCellVal = oTableData.sValue if (type(bHeader)=="boolean") then oTableData["bHeader"] = bHeader end end end -- Creates a row -- tFormatting (e.g. {["style"]="color:green"}) function createRow(tFormatting) local hTableRow= mw.html.create('tr') if(tFormatting and type(tFormatting)=="table" and next(tFormatting)) then hTableRow:attr(tFormatting) hTableRow:node(sHeadingCol) end hTableRow:done return hTableRow end -- Appends a row to a table --(hTable - mw.html table node) --e.g. mw.html.create('table'))    --(hRow - mw.html row node - e.g. mw.html.create('tr'))     -- tFormatting (e.g. {["style"]="color:green"} function appendRow(hTable, hRow,tFormatting) if(tFormatting and type(tFormatting)=="table" and next(tFormatting)) then hRow:attr(tFormatting) end if (hRow) and (hTable) then hTable:node(hRow) else return "Syntax error: Row and table cannot be nil!" end return hTable:done end --Creates a new column --Params(sColValue(string), table with formatting (e.g. {["style"]="color:green"}, boolean testing for col header)   function createCol(sColValue,tFormatting,bHeader)        local sTagCol = "td"        if (bHeader) then             sTagCol ="th"        end        local hHeadingCol =mw.html.create(sTagCol)        if  (sColValue and type(sColValue)=="string") or  sColValue =="" or type(sColValue)=="number"  then            hHeadingCol:wikitext(sColValue)        end        if (tFormatting and type(tFormatting)=="table") then            local sAttribute, sValues = next(tFormatting)             if (type(sAttribute)=="string" and sValues) then                 hHeadingCol :attr(tFormatting )             else                if (sAttribute) then                    return "Error: Attributes need Key and value ({[key]='value'}), e.g. {['style']='color:blue'}"                end             end        end hHeadingCol:done return hHeadingCol end -- Appends a column cell to a row -- tFormatting (e.g. {["style"]="color:green"}   function appendCol(hTableRow,hColumn,tFormatting)        local sColType = type (hColumn)        if sColType =="string" or  sColType =="number" or  sColType =="boolean" then            hColumn = createCol(hColumn)        end        if(tFormatting and type(tFormatting)=="table" and next(tFormatting)) then            local sAttribute, sValues = next(tFormatting)            if (type(sAttribute)=="string" and sValues) then                 hHeadingCol :attr(tFormatting )            else                if (sAttribute) then                    return "Error: Attributes need Key and value ({[key]='value'}), e.g. {['style']='color:blue'}"                end            end        end        if (hTableRow and hColumn and type(hColumn)=="table" ) then            hTableRow = hTableRow:node(hColumn)        else            return "Syntax error: Table row and table column cannot be nil!" end end function self:getTable checkSelf(self,"getTable") local hRow,hCell local hTable = mw.html.create("table") hTable:tag("caption") :wikitext(self.tableFormatting.caption) hTable:cssText(self.tableFormatting.style) for i,sClassName in pairs(self.tableFormatting.class) do           if type(sClassName) == "string" then hTable:addClass(sClassName) end end for iRow,tRow in pairs(self.arrTable) do           if (self.arrTable[iRow]) then tRowFormat = self.arrTable[iRow]["rowdata"] end hRow = createRow(tRowFormat) for iCol,oCell in ipairs(tRow) do                if (oCell) then hCell = createCol(oCell.sValue,oCell.oFormatting,oCell.bHeader) appendCol(hRow,hCell) end end appendRow(hTable, hRow) end return hTable:done end function self:setGrid(iRows,iColumns) checkSelf(self,"setGrid") local bHeader for iRowCount=0,iRows do            self.arrTable[iRowCount] = {} bHeader = false for iColCount=0, iColumns do               bHeader = (iRowCount==1) self.arrTable[iRowCount][iColCount] ={["sValue"]= "",["oFormatting"] = {},["bHeader"] =  bHeader} end end end function self:getGrid checkSelf(self,"getGrid") local sGrid = "\tCol1\tCol2\n" local sValue for iRow =1,#self.arrTable do           sGrid = sGrid ..iRow.."\t|" for iCol =1, #self.arrTable[iRow] do               sValue = self:getCell(iRow,iCol) or "" sGrid = sGrid .."\t" .. sValue .. "\t|" end sGrid = sGrid .."\n" end return sGrid end function self:setTableFormat(sTableDescription, sStyle,tClass) checkSelf(self,"setTableFormat") if sTableDescription and type(sTableDescription)=="string" then self.tableFormatting.caption = sTableDescription end if (tClass) then if type(tClass)=="table" and next(tClass) then self.tableFormatting.class= tClass end end if(sStyle and type(sStyle)=="string" and sStyle~="") then self.tableFormatting.style =sStyle end end function self:setRowFormat(iRow,tFormatting) checkSelf(self,"setRowFormat") if (iRow and self.arrTable[iRow] and self.arrTable[iRow]["rowdata"]) then self.arrTable[iRow]["rowdata"] = tFormatting end end function self:addRow(iRow) checkSelf(self,"addRow") local sValue ="" local tColumns ={} local iRowToAdd = tonumber(iRow) or 1 isSelfCheck(self) if iRow and (iRow>=(#self.arrTable +2) or iRow<1) then return end if self.arrTable and self.arrTable[1] then iRowToAdd = iRow or #self.arrTable+1 for i=1,#self.arrTable[1] do               tColumns[i] = {} end end table.insert(self.arrTable,iRowToAdd,tColumns) end function self:removeRow(iRow) checkSelf(self,"removeRow") if self.arrTable[iRow] then table.remove(self.arrTable,iRow) end end function self:setRow(iRow,tTable) checkSelf(self,"setRow") if iRow and self.arrTable[iRow] and tTable then for iCol,sValue in pairs(tTable) do               self:setCell(sValue,iRow,iCol) end end end -- adds a new column to the table function self:addCol(sColName,iCol) checkSelf(self,"addCol") if iCol and self.arrTable and self.arrTable[1] and (iCol>=#self.arrTable[1] +2 or iCol<1) then return end if self.arrTable and self.arrTable[1] and not(iCol) then iCol = #self.arrTable[1]+1 end if self.arrTable[1] then table.insert(self.arrTable[1],iCol,{}) self:setCell(sColName,1,iCol,{},true) end end -- removes a new column to the table function self:removeCol(iCol) checkSelf(self,"removeCol") if iCol then for i,v in pairs(self.arrTable) do               if self.arrTable[i] and self.arrTable[i][iCol] then table.remove(self.arrTable[i],iCol) end end end end function self:getRowCount checkSelf(self,"getRowCount") return #self.arrTable end function self:getColCount(iRow) checkSelf(self,"getColCount") iRow = iRow or 1 if self.arrTable[iRow] then return #self.arrTable[iRow] end return 0 end function initialize self.setTableFormat(self,sTableDescription, sStyle,tClass) self:setTable(arrExtData) end -- //Constructor initialize return self end function p.new(arrExtData,sTableDescription, sStyle,tClass) return tableModel.new(arrExtData,sTableDescription, sStyle,tClass) end -- Test code function p.test(frame) -- Array consisting of rows and columns local tRowData = { -- Row - Columns {"1","2"},       {"44","3"},         {"6","8"},     }    local c = tableModel.new(tRowData,"Table","",{"wikitable"}) local row1 = 4 local col1 = 1 local celltext = "green" local cellFormat = {["style"]="color:green"} c:addRow c:setCell("7",row1,col1) c:setCell("2",row1,2) local row2 = 1 local col2 = 1 c:setCell(celltext,row2,col2,cellFormat) return c:getTable end -- End Test code return p