2012-07-21 14:15:29 +02:00
|
|
|
-- @docclass
|
2012-07-22 16:02:01 +02:00
|
|
|
--[[
|
|
|
|
TODO:
|
|
|
|
* Make table headers more robust.
|
|
|
|
* Get dynamic row heights working with text wrapping.
|
|
|
|
* Every second row different background color applied.
|
|
|
|
]]
|
|
|
|
UITable = extends(UIWidget)
|
|
|
|
|
|
|
|
local HEADER_ID = 'row0'
|
2012-07-21 14:15:29 +02:00
|
|
|
|
|
|
|
function UITable.create()
|
|
|
|
local table = UITable.internalCreate()
|
2012-07-22 16:02:01 +02:00
|
|
|
table.headerRow = nil
|
|
|
|
table.dataSpace = nil
|
2012-07-21 14:15:29 +02:00
|
|
|
table.rows = {}
|
2012-07-21 19:17:03 +02:00
|
|
|
table.rowBaseStyle = nil
|
2012-07-22 16:02:01 +02:00
|
|
|
table.columns = {}
|
2012-07-21 19:17:03 +02:00
|
|
|
table.columBaseStyle = nil
|
2012-07-22 16:02:01 +02:00
|
|
|
table.headerRowBaseStyle = nil
|
|
|
|
table.headerColumnBaseStyle = nil
|
2012-07-21 19:17:03 +02:00
|
|
|
table.selectedRow = nil
|
2012-07-21 14:15:29 +02:00
|
|
|
return table
|
|
|
|
end
|
|
|
|
|
2012-07-22 16:02:01 +02:00
|
|
|
function UITable:onDestroy()
|
2012-07-21 14:15:29 +02:00
|
|
|
for k,row in pairs(self.rows) do
|
|
|
|
row.onClick = nil
|
|
|
|
end
|
|
|
|
self.rows = {}
|
2012-07-22 16:02:01 +02:00
|
|
|
self.columns = {}
|
|
|
|
self.headerRow = {}
|
|
|
|
self.selectedRow = nil
|
2012-07-28 15:41:10 +02:00
|
|
|
self.dataSpace:destroyChildren()
|
2012-07-22 16:02:01 +02:00
|
|
|
self.dataSpace = nil
|
2012-07-21 14:15:29 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function UITable:onStyleApply(styleName, styleNode)
|
|
|
|
for name, value in pairs(styleNode) do
|
2012-07-22 16:02:01 +02:00
|
|
|
if name == 'table-data' then
|
|
|
|
addEvent(function()
|
|
|
|
self:setTableData(self:getParent():getChildById(value))
|
|
|
|
end)
|
|
|
|
elseif name == 'column-style' then
|
2012-07-21 14:15:29 +02:00
|
|
|
addEvent(function()
|
2012-07-21 19:17:03 +02:00
|
|
|
self:setColumnStyle(value)
|
2012-07-21 14:15:29 +02:00
|
|
|
end)
|
|
|
|
elseif name == 'row-style' then
|
|
|
|
addEvent(function()
|
2012-07-21 19:17:03 +02:00
|
|
|
self:setRowStyle(value)
|
2012-07-21 14:15:29 +02:00
|
|
|
end)
|
2012-07-22 16:02:01 +02:00
|
|
|
elseif name == 'header-column-style' then
|
|
|
|
addEvent(function()
|
|
|
|
self:setHeaderColumnStyle(value)
|
|
|
|
end)
|
|
|
|
elseif name == 'header-row-style' then
|
|
|
|
addEvent(function()
|
|
|
|
self:setHeaderRowStyle(value)
|
|
|
|
end)
|
2012-07-21 14:15:29 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-22 16:02:01 +02:00
|
|
|
function UITable:hasHeader()
|
|
|
|
return self.headerRow ~= nil
|
|
|
|
end
|
|
|
|
|
|
|
|
function UITable:clearData()
|
|
|
|
if not self.dataSpace then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
self.dataSpace:destroyChildren()
|
2012-07-28 15:41:10 +02:00
|
|
|
self.selectedRow = nil
|
|
|
|
self.columns = {}
|
|
|
|
self.rows = {}
|
2012-07-22 16:02:01 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function UITable:addHeaderRow(data)
|
|
|
|
if not data or type(data) ~= 'table' then
|
|
|
|
g_logger.error('UITable:addHeaderRow - table columns must be provided in a table')
|
|
|
|
return
|
2012-07-21 19:17:03 +02:00
|
|
|
end
|
|
|
|
|
2012-07-22 16:02:01 +02:00
|
|
|
-- build header columns
|
|
|
|
local columns = {}
|
|
|
|
for _, column in pairs(data) do
|
|
|
|
local col = g_ui.createWidget(self.headerColumnBaseStyle)
|
|
|
|
for type, value in pairs(column) do
|
|
|
|
if type == 'width' then
|
|
|
|
col:setWidth(value)
|
|
|
|
elseif type == 'height' then
|
|
|
|
col:setHeight(value)
|
|
|
|
elseif type == 'text' then
|
|
|
|
col:setText(value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
table.insert(columns, col)
|
|
|
|
end
|
2012-07-21 19:17:03 +02:00
|
|
|
|
2012-07-22 16:02:01 +02:00
|
|
|
-- create a new header
|
|
|
|
local headerRow = g_ui.createWidget(self.headerRowBaseStyle, self)
|
|
|
|
local newHeight = (self.dataSpace:getHeight()-headerRow:getHeight())-self.dataSpace:getMarginTop()
|
|
|
|
self.dataSpace:applyStyle({ height = newHeight })
|
2012-07-21 14:15:29 +02:00
|
|
|
|
2012-07-22 16:02:01 +02:00
|
|
|
headerRow:setId(HEADER_ID)
|
2012-07-21 19:17:03 +02:00
|
|
|
for _, column in pairs(columns) do
|
2012-07-22 16:02:01 +02:00
|
|
|
headerRow:addChild(column)
|
|
|
|
self.columns[HEADER_ID] = column
|
|
|
|
end
|
|
|
|
|
|
|
|
headerRow.onClick = function(headerRow) self:selectRow(headerRow) end
|
|
|
|
self.headerRow = headerRow
|
|
|
|
return headerRow
|
|
|
|
end
|
|
|
|
|
|
|
|
function UITable:removeHeaderRow()
|
|
|
|
self.headerRow:destroy()
|
|
|
|
self.headerRow = nil
|
|
|
|
end
|
|
|
|
|
2012-07-26 12:19:34 +02:00
|
|
|
function UITable:addRow(data, ref, height)
|
2012-07-22 16:02:01 +02:00
|
|
|
if not self.dataSpace then
|
|
|
|
g_logger.error('UITable:addRow - table data space has not been set, cannot add rows.')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if not data or type(data) ~= 'table' then
|
|
|
|
g_logger.error('UITable:addRow - table columns must be provided in a table.')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local row = g_ui.createWidget(self.rowBaseStyle, self.dataSpace)
|
2012-07-26 12:19:34 +02:00
|
|
|
if ref then row.ref = ref end
|
2012-07-22 16:02:01 +02:00
|
|
|
if height then row:setHeight(height) end
|
2012-07-26 12:19:34 +02:00
|
|
|
|
2012-07-22 16:02:01 +02:00
|
|
|
local rowId = #self.rows
|
2012-07-26 12:19:34 +02:00
|
|
|
row:setId('row'..(rowId < 1 and 1 or rowId))
|
2012-07-22 16:02:01 +02:00
|
|
|
|
|
|
|
for _, column in pairs(data) do
|
2012-07-21 19:17:03 +02:00
|
|
|
local col = g_ui.createWidget(self.columBaseStyle, row)
|
2012-07-22 16:02:01 +02:00
|
|
|
for type, value in pairs(column) do
|
|
|
|
if type == 'width' then
|
|
|
|
col:setWidth(value)
|
|
|
|
elseif type == 'height' then
|
|
|
|
col:setHeight(value)
|
|
|
|
elseif type == 'text' then
|
|
|
|
col:setText(value)
|
|
|
|
end
|
2012-07-21 19:17:03 +02:00
|
|
|
end
|
2012-07-22 16:02:01 +02:00
|
|
|
self.columns[rowId] = col
|
2012-07-21 14:15:29 +02:00
|
|
|
end
|
2012-07-26 12:19:34 +02:00
|
|
|
|
|
|
|
row.onFocusChange = function(row, focused)
|
|
|
|
if focused then self:selectRow(row) end
|
|
|
|
end
|
2012-07-21 14:15:29 +02:00
|
|
|
table.insert(self.rows, row)
|
|
|
|
return row
|
|
|
|
end
|
|
|
|
|
|
|
|
function UITable:removeRow(row)
|
|
|
|
if self.selectedRow == row then
|
|
|
|
self:selectRow(nil)
|
|
|
|
end
|
|
|
|
row.onClick = nil
|
|
|
|
table.removevalue(self.rows, row)
|
|
|
|
end
|
|
|
|
|
|
|
|
function UITable:selectRow(selectedRow)
|
|
|
|
if selectedRow == self.selectedRow then return end
|
|
|
|
|
|
|
|
local previousSelectedRow = self.selectedRow
|
|
|
|
self.selectedRow = selectedRow
|
|
|
|
|
|
|
|
if previousSelectedRow then
|
|
|
|
previousSelectedRow:setChecked(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
if selectedRow then
|
|
|
|
selectedRow:setChecked(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
signalcall(self.onSelectionChange, self, selectedRow, previousSelectedRow)
|
|
|
|
end
|
|
|
|
|
2012-07-22 16:02:01 +02:00
|
|
|
function UITable:setTableData(tableData)
|
|
|
|
self.dataSpace = tableData
|
|
|
|
self.dataSpace:applyStyle({ height = self:getHeight() })
|
|
|
|
end
|
|
|
|
|
2012-07-21 14:15:29 +02:00
|
|
|
function UITable:setRowStyle(style)
|
2012-07-21 19:17:03 +02:00
|
|
|
self.rowBaseStyle = style
|
2012-07-22 16:02:01 +02:00
|
|
|
for _, row in pairs(self.rows) do
|
2012-07-21 14:15:29 +02:00
|
|
|
row:setStyle(style)
|
2012-07-22 16:02:01 +02:00
|
|
|
end
|
2012-07-21 14:15:29 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function UITable:setColumnStyle(style)
|
2012-07-21 19:17:03 +02:00
|
|
|
self.columBaseStyle = style
|
2012-07-22 16:02:01 +02:00
|
|
|
for _, col in pairs(self.columns) do
|
2012-07-21 14:15:29 +02:00
|
|
|
col:setStyle(style)
|
2012-07-22 16:02:01 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function UITable:setHeaderRowStyle(style)
|
|
|
|
self.headerRowBaseStyle = style
|
|
|
|
if self.headerRow then
|
|
|
|
self.headerRow:setStyle(style)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function UITable:setHeaderColumnStyle(style)
|
|
|
|
self.headerColumnBaseStyle = style
|
|
|
|
if table.hasKey(HEADER_ID) then
|
|
|
|
self.columns[HEADER_ID]:setStyle(style)
|
|
|
|
end
|
2012-07-25 01:02:56 +02:00
|
|
|
end
|