@@ -23,7 +23,13 @@ Table.defineProperty(Table, "columns", {default = {}, type = "table", canTrigger
2323 return t
2424end })
2525--- @property data table {} The table data as array of row arrays
26- Table .defineProperty (Table , " data" , {default = {}, type = " table" , canTriggerRender = true })
26+ Table .defineProperty (Table , " data" , {default = {}, type = " table" , canTriggerRender = true , setter = function (self , value )
27+ self .set (" scrollOffset" , 0 )
28+ self .set (" selectedRow" , nil )
29+ self .set (" sortColumn" , nil )
30+ self .set (" sortDirection" , " asc" )
31+ return value
32+ end })
2733--- @property selectedRow number? nil Currently selected row index
2834Table .defineProperty (Table , " selectedRow" , {default = nil , type = " number" , canTriggerRender = true })
2935--- @property headerColor color blue Color of the column headers
@@ -33,9 +39,9 @@ Table.defineProperty(Table, "selectedColor", {default = colors.lightBlue, type =
3339--- @property gridColor color gray Color of grid lines
3440Table .defineProperty (Table , " gridColor" , {default = colors .gray , type = " color" })
3541--- @property sortColumn number? nil Currently sorted column index
36- Table .defineProperty (Table , " sortColumn" , {default = nil , type = " number" })
42+ Table .defineProperty (Table , " sortColumn" , {default = nil , type = " number" , canTriggerRender = true })
3743--- @property sortDirection string "asc" Sort direction ("asc" or "desc")
38- Table .defineProperty (Table , " sortDirection" , {default = " asc" , type = " string" })
44+ Table .defineProperty (Table , " sortDirection" , {default = " asc" , type = " string" , canTriggerRender = true })
3945--- @property scrollOffset number 0 Current scroll position
4046Table .defineProperty (Table , " scrollOffset" , {default = 0 , type = " number" , canTriggerRender = true })
4147
@@ -144,17 +150,11 @@ function Table:mouse_click(button, x, y)
144150 if relY > 1 then
145151 local rowIndex = relY - 2 + self .get (" scrollOffset" )
146152 if rowIndex >= 0 and rowIndex < # self .get (" data" ) then
147- local newIndex = rowIndex + 1
148- self .set (" selectedRow" , newIndex )
149- self :fireEvent (" select" , newIndex , self .get (" data" )[newIndex ])
153+ self .set (" selectedRow" , rowIndex + 1 )
150154 end
151155 end
152- return true
153- end
154156
155- function Table :onSelect (callback )
156- self :registerCallback (" select" , callback )
157- return self
157+ return true
158158end
159159
160160--- @shortDescription Handles scrolling through the table data
@@ -168,7 +168,7 @@ function Table:mouse_scroll(direction, x, y)
168168 local data = self .get (" data" )
169169 local height = self .get (" height" )
170170 local visibleRows = height - 2
171- local maxScroll = math.max (0 , # data - visibleRows + 1 )
171+ local maxScroll = math.max (0 , # data - visibleRows - 1 )
172172 local newOffset = math.min (maxScroll , math.max (0 , self .get (" scrollOffset" ) + direction ))
173173
174174 self .set (" scrollOffset" , newOffset )
@@ -189,14 +189,31 @@ function Table:render()
189189 local height = self .get (" height" )
190190 local width = self .get (" width" )
191191
192+ local totalWidth = 0
193+ local lastVisibleColumn = # columns
194+ for i , col in ipairs (columns ) do
195+ if totalWidth + col .width > width then
196+ if i == 1 then
197+ col .visibleWidth = width
198+ else
199+ col .visibleWidth = width - totalWidth
200+ lastVisibleColumn = i
201+ end
202+ break
203+ end
204+ col .visibleWidth = col .width
205+ totalWidth = totalWidth + col .width
206+ end
207+
192208 local currentX = 1
193209 for i , col in ipairs (columns ) do
210+ if i > lastVisibleColumn then break end
194211 local text = col .name
195212 if i == sortCol then
196213 text = text .. (self .get (" sortDirection" ) == " asc" and " \30 " or " \31 " )
197214 end
198- self :textFg (currentX , 1 , text :sub (1 , col .width ), self .get (" headerColor" ))
199- currentX = currentX + col .width
215+ self :textFg (currentX , 1 , text :sub (1 , col .visibleWidth ), self .get (" headerColor" ))
216+ currentX = currentX + col .visibleWidth
200217 end
201218
202219 for y = 2 , height do
@@ -208,17 +225,18 @@ function Table:render()
208225 local bg = (rowIndex + 1 ) == selected and self .get (" selectedColor" ) or self .get (" background" )
209226
210227 for i , col in ipairs (columns ) do
228+ if i > lastVisibleColumn then break end
211229 local cellText = tostring (rowData [i ] or " " )
212- local paddedText = cellText .. string.rep (" " , col .width - # cellText )
213- if i < # columns then
214- paddedText = string.sub (paddedText , 1 , col .width - 1 ) .. " "
230+ local paddedText = cellText .. string.rep (" " , col .visibleWidth - # cellText )
231+ if i < lastVisibleColumn then
232+ paddedText = string.sub (paddedText , 1 , col .visibleWidth - 1 ) .. " "
215233 end
216- local finalText = string.sub (paddedText , 1 , col .width )
217- local finalForeground = string.rep (tHex [self .get (" foreground" )], # finalText )
218- local finalBackground = string.rep (tHex [bg ], # finalText )
234+ local finalText = string.sub (paddedText , 1 , col .visibleWidth )
235+ local finalForeground = string.rep (tHex [self .get (" foreground" )], col . visibleWidth )
236+ local finalBackground = string.rep (tHex [bg ], col . visibleWidth )
219237
220238 self :blit (currentX , y , finalText , finalForeground , finalBackground )
221- currentX = currentX + col .width
239+ currentX = currentX + col .visibleWidth
222240 end
223241 else
224242 self :blit (1 , y , string.rep (" " , self .get (" width" )),
0 commit comments