B4A Library [Class] Flexible Table

klaus

Expert
Licensed User
Longtime User
other question:
i used a old version (1.11) and now migrate to last version

old version when the text are long, go to a new line, le new version not
is it possible replicate this feature on last version?
Simply set Table1.SingleLine = Fasle
 

Kanne

Member
Licensed User
Longtime User
Hi Klaus,
I just went from V1.44 to V3.10 and have this problem:
B4X:
Table1.SetTypeFaces(Array As Typeface(Main.myFont))
does not set the font in the cells because "mNumberOfColumns" is set to 0 at this moment.
Well, in the old Initialize there was a parameter for column-count, so I tried
B4X:
Table1.InitializeTable(1,Bit.Or(Gravity.CENTER_HORIZONTAL, Gravity.CENTER_VERTICAL),False)
but this results in an error at line 725 (Table).

I could sove it by comparing "SetTypeFaces" with "SetHeaderTypeFaces" and changed "SetTypeFaces" from
B4X:
Public Sub SetTypeFaces(TypeFaces() As Typeface)
    If TypeFaces.Length <> mNumberOfColumns Then
        ToastMessageShow("Invalid number of columns", False)
        Return
    End If
    
    If TypeFaces.Length = 1 Then
        cTypeFace = TypeFaces(0)
        MultiTypeFace = False
    Else   
        cTypeFaces = TypeFaces
        MultiTypeFace = True
    End If
to
B4X:
Public Sub SetTypeFaces(TypeFaces() As Typeface)
    If TypeFaces.Length = 1 Then
        cTypeFace = TypeFaces(0)
        MultiTypeFace = False
    Else
        If TypeFaces.Length <> mNumberOfColumns Then
            ToastMessageShow("Invalid number of columns", False)
            Return
        Else
            cTypeFaces = TypeFaces
            MultiTypeFace = True
        End If
    End If
 

klaus

Expert
Licensed User
Longtime User
Where and how did you set the typefaces?
I know that there is a problem with version 3.10 and type faces.

Be aware that the parameter is an Array of TypeFaces, which means one TypeFace per column.
Table1.SetTypeFaces(Array As Typeface(Main.myFont))

I am currently working on an update.
In the mean time, try version 3.11 from post 933 not officially published.
 

Kanne

Member
Licensed User
Longtime User
I want to set all column to same font, so array is length of 1 and so differs from mNumberOfColumns.
At SetTypeFaces at first the number of columns is compared to the length of the array but not regarding that with a length of 1 I want to set all columns.
At SetHeaderTypeFaces at first there is a check of array-length = 1:
B4X:
Public Sub SetHeaderTypeFaces(HeaderTypeFaces() As Typeface)
    Private col As Int
    
    If HeaderTypeFaces.Length = 1 Then
        cHeaderTypeFace = HeaderTypeFaces(0)
        HeaderMultiTypeFace = False
        If Header.NumberOfViews > 0 Then
            For col = 0 To mNumberOfColumns - 1
                Private lbl As Label
                lbl = Header.GetView(col)
                Log(lbl.Text)
                lbl.Typeface = cHeaderTypeFace
            Next
        End If
    Else
        If HeaderTypeFaces.Length <> mNumberOfColumns Then
            ToastMessageShow("Invalid number of columns", False)
            Log("SetHeaderTypeFaces: Invalid number of columns")
            Return
 

klaus

Expert
Licensed User
Longtime User
@fishwolf and @Kanne and others of course.
Could you please test the new update, version 3.13

Now it is possible to fix, up to three, first columns, not only the first one. This is a new property which can be set either in the Designer or in the code: Table1.NumberOfFixedColumns = 2
I added the possibility to add other colors to rows and remove them, only in the code with given methods.
Table1.SetRowColorN(9, Colors.Red) and Color = Table1.GetRowColorN(9).
There is no specific text color for those rows, the text color is either black or white, calculated automatically depending on the darkness of the row color.
@fishwolf, I did it in a different way than you without properties in the Designer and not limited to several colors.
@Kanne, I modified the SetTypeFaces method.

If you test it with one of your projects you need to open the Designer and close it to activate the new property.

 

Attachments

  • TableV3_13.zip
    50.3 KB · Views: 241
  • Table.bas
    98.9 KB · Views: 220

martha

Member
Regarding the SetRowColorN method:

a) It works perfectly, without any other modification, as long as the row is not the last one
b) If it is the last row, it is necessary to add the line

Table1.RefreshTable

This new method is very useful for adding a row of totals (summaries) or FooterRow. Hopefully someday, you could add a similar method for text color. In this way the HeaderRow would coincide with the FooterRow
 

Kanne

Member
Licensed User
Longtime User
going better ... I could solve my problem with V3.x:
when adding table by code I had to change the order of initializing from
B4X:
Table1.Initialize(Me,"Table1",1,Gravity.CENTER_HORIZONTAL, False)
Table1.AddToActivity(pnlTable, 0, 0, pnlTable.Width, pnlTable.Height)
to
B4X:
Table1.Initialize(Me,"Table1")
Table1.AddToActivity(Activity, pnlTable.top,pnlTable.left, pnlTable.Width, pnlTable.Height)
Table1.InitializeTable(1,Gravity.CENTER_HORIZONTAL, False)
The statusline was reset when filling the table, so I had to make an
B4X:
    Table1.ShowStatusLine = False
after
B4X:
    Table1.ClearAll
    Table1.LoadSQLiteDB(...)

Seems to run now ... back to the sorting problem:
in V 3.x the "Header_Click" is regarding "cColumnDataType" .... but I have to set by myself via "SetColumnDataType(s)".
Would be nice, if "LoadSQLiteDB" could do it for me ... and if I could read them ("GetColumnDataType(s)")
 

klaus

Expert
Licensed User
Longtime User
I added 4 new methods:
LoadSQLiteDB4(SQLite As SQL, Query As String, AutomaticWidths As Boolean)
LoadSQLiteDB5(SQLite As SQL, Query As String, Values() As String, AutomaticWidths As Boolean)

which check if all values in a column are numbers, if yes it sets the column data type to NUMBER.
GetColumnDataType(Column As Int) As String returns the data type of the given column
GetColumnDataTypes As String() returns the data type of all columns as an Array

LoadSQLiteDB4 is the same as LoadSQLiteDB, but with the type check
LoadSQLiteDB5 is the same as LoadSQLiteDB3, but with the type check

Now with LoadSQLiteDB4 and LoadSQLiteDB5 the sorting of numbers is OK.

Can you please test the attached version 3.14?
 

Attachments

  • TableV3_14.zip
    50.8 KB · Views: 208

Kanne

Member
Licensed User
Longtime User
... it would be nice to add a method "GetColumnWidthsTotal" (I need it for centering the table).
In the old versions I changed the extended "table.bas" but now moved it to "my Code":

GetColumnWidthsTotal of a table:
Public Sub TableGetColumnWidthsTotal(pTable As table) As Int
    Dim iTotalWidth As Int
    Dim iColsWidth() As Int
    iTotalWidth = 0
    iColsWidth = pTable.GetColumnWidths
    For i = 0 To iColsWidth.Length - 1
        iTotalWidth = iTotalWidth + iColsWidth(i)
    Next
    Return iTotalWidth
End Sub
 

Kanne

Member
Licensed User
Longtime User
the new functions are great - thanks a lot.
That saves to always pass an array with the numeric columns, e.g. for changing the Alignment like

B4X:
    Dim arrCellAlignment(Table1.NumberOfColumns) As Int
    For i = 0 To Main.iColsNumeric.Length - 1
        If Main.iColsNumeric(i) <= Table1.NumberOfColumns - 1 Then
            Log("Setting TableColumns numeric: " & Table1.HeaderNames.Get(Main.iColsNumeric(i)))
            Table1.SetColumnDataType (Main.iColsNumeric(i),"NUMBER")
            arrCellAlignment(Main.iColsNumeric(i)) = Bit.Or(Gravity.RIGHT, Gravity.CENTER_VERTICAL)
        End If
    Next
    Table1.SetHeaderAlignments(arrCellAlignment)
    Table1.SetCellAlignments(arrCellAlignment)

Now I can do this by simply

B4X:
    Dim arrCellAlignment(Table1.NumberOfColumns) As Int
    For i = 0 To Table1.NumberOfColumns    - 1
        If Table1.GetColumnDataType(i) = "TEXT" Then
            arrCellAlignment(i) = Bit.Or(Gravity.LEFT, Gravity.CENTER_VERTICAL)
        Else
            arrCellAlignment(i) = Bit.Or(Gravity.RIGHT, Gravity.CENTER_VERTICAL)
        End If
    Next
    Table1.SetHeaderAlignments(arrCellAlignment)
    Table1.SetCellAlignments(arrCellAlignment)
 

Kanne

Member
Licensed User
Longtime User
hm - found a problem:
my SQL-values are changed (!) by the new functions:
B4X:
select printf(""%.2f"",ifnull(AVG(Platz),0)) as Platz_Schnitt from ...
always had 2 decimal digits, which now are gone if they are zero ... so it seems that the "numeric test" is changing the values :-(
 

Kanne

Member
Licensed User
Longtime User
I took a view on your code where you tested (and converted) the double-values ... so I could do it similar without changing the values and can use the normal LoadSQLiteDB:
B4X:
    Dim arrColDataType (Table1.NumberOfColumns) As String
    Dim arrColAlignment(Table1.NumberOfColumns) As Int
    For i = 0 To Table1.NumberOfColumns    - 1
        arrColDataType(i) = "NUMBER"
        arrColAlignment(i) = Bit.Or(Gravity.RIGHT, Gravity.CENTER_VERTICAL)
        For j = 0 To Table1.NumberOfRows - 1
            If Not(IsNumber(Table1.GetValue(i,j))) Then
                arrColDataType(i) = "TEXT"
                arrColAlignment(i) = Bit.Or(Gravity.LEFT, Gravity.CENTER_VERTICAL)
                Exit
            End If
        Next
        Log("Setting TableColumn " & Table1.HeaderNames.Get(i) & " : " & arrColDataType(i))
    Next

    Table1.SetColumnDataTypes(arrColDataType)
    Table1.SetHeaderAlignments(arrColAlignment)
    Table1.SetCellAlignments(arrColAlignment)
 

Kanne

Member
Licensed User
Longtime User
... 2 small optical issues left:
1) the sorting-indicators are looking reversed: when sorting from low to high values the corner of the triangle is set to the bottom and vice versa;
I don't know if this what is wanted, but normaly the corner indicated the sorting direction
2) when fixing one or more columns I get an col-offset in the heders against to the data; checked it against the demo (where I don't have this problem) but can't get rid of it;
for me it seems that the left padding of the first non-fixed header is missing.
may I send you the project ?
 

Attachments

  • 2020-04-13_151505.jpg
    48.6 KB · Views: 230
Last edited:

klaus

Expert
Licensed User
Longtime User
Thank you for the testing !

The two issues in your last post have been amended for the final version.
I had seen the second one and it was already on my to do list.

There were two problems with numbers.
1. Sorting, the routine for strings is not the same as for numbers.
100, 103, 50 50 came after 103 because of the first character of 5.
In your case, with the two fixed decimal digits, you solved it by setting the data types in the code and with LoadSQLiteDB.
With your solution, using the printf() SQLite method, solves also the second problem.
2. In SQLite REAL numbers are returned like Float numbers when they are read with GetString.
For example: 123456789 is returned as 1.23457e+08
And 1000000.5 is returned as 1000000, i discovered this during the last update of the class.

... it would be nice to add a method "GetColumnWidthsTotal" (I need it for centering the table).
What exactly do you use it for?
Which total width do you expect when there are fixed columns.
 
Last edited:

Kanne

Member
Licensed User
Longtime User
I need the total width of the table of all visible columns for centering the table if the total width is smaller than the view where the table is displayed, e.g.

B4X:
Table1.Left = Max((100%x - JK_Codes.TableGetColumnWidthsTotal(Table1))/2,0)

But as mentioned I can sum the values by myself if you find it to "specialzed" ...
 

Kanne

Member
Licensed User
Longtime User
Is it possible to change the colour of a single cell, e.g. if I want to mark negativ values in the table ?
 

klaus

Expert
Licensed User
Longtime User
I added the InnerTotalWidth as a property, read only.
I will not add the change of color of single cells, it would be a big change and probably slow down the filling of the Table.

The Table class has been updated to version 3.14.
Added the methods below
- LoadSQLiteDB4(SQLite As SQL, Query As String, AutomaticWidths As Boolean)
loads SQLite data with data type checking
- LoadSQLiteDB5(SQLite As SQL, Query As String, Values() As String, AutomaticWidths As Boolean).
loads SQLite data with data type checking , similar to LoadSQLiteDB4 but for parametrized queries.
- GetColumnDataTypes As String(), returns an Array with the data type for each column.
- GetColumnDataType(Column As Int) As String, returns the data type of the fiven column.
Added the InnerTotalWidth property, read only.
Added multiple first fiexed columns
Added line colors

First post updated.
 

Kanne

Member
Licensed User
Longtime User
! great ! great ! great !

The table now looks VERY nice with fixed columns ... but is it normal, that the grid of the fixed columns have different colors ?
I set the color with
B4X:
Table1.TableColor = Colors.Black
and could not find a separate property for the fixed columns
 

klaus

Expert
Licensed User
Longtime User
This is of course a bug!
I didn't test the property change in the code.

Amended in the first post.

Again, thank you for the 'deep' testing.
I like this kind of collaboration, that's the best and only way to make software better .
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…