-- ********************************************************************** -- * Html table renderer -- * -- * version 0.1 -- * -- * (c) 2005 Hi-Project Ltd. -- ********************************************************************** -- sampletable = { -- {caption="Name", width="200", align="left", data="username", attrstart="", attrend="" }, -- {caption="Date", width="100", align="center", data="paymentdate", format=todatefunc }, -- {caption="Amount", width="100", align="right", data="amount", format=tocurrfuc, add=" {$curr$}"} }, -- {caption="Ready", width="30", align="center", data="flag", format=toyesnostr}, -- {caption="Selected", width="30", align="center", data=nil, add=[[]]} -- } require "common.languages" require "webserver.htmlparser" module "htable" local TABLE_ROW_DEFAULT = [[ ]] local DEFAULT_CELL_SIZE = 100 local DEFAULT_CELL_ALIGN = "left" function rendertableheader(tbl_desc, twidth) -- making table header local tbl_hdr = [[]] table.foreachi(tbl_desc, function(i,tt) local caption = tt.caption or "" local width = tt.width or tostring(DEFAULT_CELL_SIZE) local align = tt.align or DEFAULT_CELL_ALIGN tbl_hdr = tbl_hdr.. [[]].."\n" end) return tbl_hdr end function rendertablerow(tbl_desc, row_data, tbl_tr) -- making table row -- local tbl_row = [[]] if tbl_tr then tbl_row = htemplate.parsebuffer(tbl_tr,row_data) else tbl_row = htemplate.parsebuffer(TABLE_ROW_DEFAULT,row_data) end table.foreachi(tbl_desc, function(i,tt) local caption = tt.caption or "" local width = tt.width or "100" -- default local align = tt.align or "left" -- default local attrstart = tt.attrstart or "" local attrend = tt.attrend or "" local addthis = tt.add or "" if tt.data==nil or tt.data=="" then addthis = htemplate.parsebuffer(addthis,row_data) tbl_row = tbl_row..[[" else local data = tt.data data = row_data[tt.data] or "" if tt.format then data = tt.format(data) end if tt.add then addthis = htemplate.parsebuffer(addthis,row_data) end tbl_row = tbl_row..[[" end end) tbl_row = tbl_row.."" return tbl_row end function rendertabledata(tbl_desc, tbl_data, tbl_tr) local tblcontent = "" if tbl_data then table.foreachi(tbl_data, function(i,tt) tblcontent = tblcontent .. rendertablerow(tbl_desc, tt, tbl_tr).."\n" end) return tblcontent else return "" end end function rendertablefooter(tbl_desc, tbl_data) return "
]].. [[

]]..TRANSLANG(tt.caption)..[[

]]..attrstart..addthis..attrend.."]]..attrstart..data..attrend..addthis.."
" end function gettablewidth(tbl_desc) local width = DEFAULT_CELL_SIZE table.foreachi(tbl_desc, function(i,tt) width = width + tonumber(tt.width) end) return width end