### Sort Columns in TableView

To sort Columns in a TableView, various options possible.
Option 1 Sort by Column usign the TableView Header Sort
Option 2 Sort by Column usign List.SortType (1)
`Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private TableView1 As TableView
Type TableHelper (SortColumn As Object, TableRow() As Object)
End Sub`
`Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
'Layout with a table called TableView1
MainForm.RootPane.LoadLayout("1")
MainForm.Show
TableView1.SetColumns(Array As String("col1", "col2", "col3"))
For i = 1 To 500
TableView1.Items.Add(Array As Object("Item " & i, Rnd(0, 100), Rnd(0, 100)))
Next
SortTable(TableView1, 1)
End Sub`
`Sub SortTable(tv As TableView, ColumnIndex As Int)
Dim list1 As List
list1.Initialize
For Each row() As Object In tv.Items
Dim th As TableHelper
th.SortColumn = row(ColumnIndex)
th.TableRow = row
list1.Add(th)
Next
list1.SortType("SortColumn", True)
tv.Items.Clear
For Each th As TableHelper In list1
tv.Items.Add(th.TableRow)
Next
End Sub`
Option 3 Sort by Column usign List.SortType (2)
A tableview has 5 columns. Each column has a customized cell with a Label control.
Define a Type first
Example
Type LibEntry(Name As String, Type As String, Comment As String, ReturnType As String, Parameter As String)
Sort the tableview based on the columnname
using the previous defined Type LibEntry. This sub uses the defined control tvwMain.
'example SortTableView("Name")
`Sub SortTableView(Column As String)
Dim LibEntries As List
LibEntries.Initialize
For Each row() As Object In tvwMain.Items
Dim le As LibEntry
le.Name = tableviewGetLabel(row, 0)
le.Type = tableviewGetLabel(row, 1)
le.Comment = tableviewGetLabel(row, 2)
le.ReturnType = tableviewGetLabel(row, 3)
le.Parameter = tableviewGetLabel(row, 4)
LibEntries.Add(le)
Next
LibEntries.SortType(Column, True)
tvwMain.Items.Clear
For i = 0 To LibEntries.Size - 1
Dim le As LibEntry
le = LibEntries.Get(i)
Dim row(5) As Object
row(0) = tableviewSetLabel(le.Name)
row(1) = tableviewSetLabel(le.Type)
row(2) = tableviewSetLabel(le.Comment)
row(3) = tableviewSetLabel(le.ReturnType)
row(4) = tableviewSetLabel(le.Parameter)
tvwMain.Items.Add(row)
Next
End Sub`
' Set the text for a customized cell - idc a label
' Parameter: Tableview row as array of object
`Sub tableviewSetLabel(s As String) As Label
Dim lbl As Label
lbl.Initialize("")
lbl.Text = s.Trim
lbl.Style = "-fx-alignment: top-left;"
lbl.WrapText = True
Return lbl
End Sub`
' Get the text of a customized cell - idc a label
' Parameter: Tableview row as array of object
' Parameter: Column number
' Returns the content of the customized cell
`Sub tableviewGetLabel(row() As Object, col As Int) As String
Dim result As String = ""
If col < 0 Then Return result
Dim lbl As Label = row(col)
result = lbl.Text
Return result
End Sub`

Changed: 2018-03-16 18:45, Category: TableView, File: T6C259M1907.htm, ID: 1907