NewTable creates and returns a pointer to a new Table with the specified width, height and columns
(width, height float32, cols []TableColumn)
| 191 | // NewTable creates and returns a pointer to a new Table with the |
| 192 | // specified width, height and columns |
| 193 | func NewTable(width, height float32, cols []TableColumn) (*Table, error) { |
| 194 | |
| 195 | t := new(Table) |
| 196 | t.Panel.Initialize(t, width, height) |
| 197 | t.styles = &StyleDefault().Table |
| 198 | t.rowCursor = -1 |
| 199 | |
| 200 | // Initialize table header |
| 201 | t.header.Initialize(&t.header, 0, 0) |
| 202 | t.header.cmap = make(map[string]*tableColHeader) |
| 203 | t.header.cols = make([]*tableColHeader, 0) |
| 204 | |
| 205 | // Create column header panels |
| 206 | for ci := 0; ci < len(cols); ci++ { |
| 207 | cdesc := cols[ci] |
| 208 | // Column id must not be empty |
| 209 | if cdesc.Id == "" { |
| 210 | return nil, fmt.Errorf("Column with empty id") |
| 211 | } |
| 212 | // Column id must be unique |
| 213 | if t.header.cmap[cdesc.Id] != nil { |
| 214 | return nil, fmt.Errorf("Column with duplicate id") |
| 215 | } |
| 216 | // Creates a column header |
| 217 | c := new(tableColHeader) |
| 218 | c.Initialize(c, 0, 0) |
| 219 | t.applyHeaderStyle(&c.Panel, false) |
| 220 | c.label = NewLabel(cdesc.Header) |
| 221 | c.Add(c.label) |
| 222 | c.id = cdesc.Id |
| 223 | c.minWidth = cdesc.Minwidth |
| 224 | if c.minWidth < tableColMinWidth { |
| 225 | c.minWidth = tableColMinWidth |
| 226 | } |
| 227 | c.width = cdesc.Width |
| 228 | if c.width < c.minWidth { |
| 229 | c.width = c.minWidth |
| 230 | } |
| 231 | c.align = cdesc.Align |
| 232 | c.format = cdesc.Format |
| 233 | c.formatFunc = cdesc.FormatFunc |
| 234 | c.expand = cdesc.Expand |
| 235 | c.sort = cdesc.Sort |
| 236 | c.resize = cdesc.Resize |
| 237 | // Adds optional sort icon |
| 238 | if c.sort != TableSortNone { |
| 239 | c.ricon = NewIcon(string(tableSortedNoneIcon)) |
| 240 | c.Add(c.ricon) |
| 241 | c.ricon.Subscribe(OnMouseDown, func(evname string, ev interface{}) { |
| 242 | t.onRicon(evname, c) |
| 243 | }) |
| 244 | } |
| 245 | // Sets default format and order |
| 246 | if c.format == "" { |
| 247 | c.format = "%v" |
| 248 | } |
| 249 | c.order = ci |
| 250 | c.SetVisible(!cdesc.Hidden) |
no test coverage detected