B4A Library [Class] Flexible Table

Sergio Castellari

Active Member
Licensed User
I never loaded data from MySQL to B4x but I think that there should also be a MySql-builtin-function to format an internal datetime value into a string.

Hi @Kanne,

Of course it does.
But when using jRDC2 to get the query, jRDC2 returns the DATE fields as TICKS. So this routine simply formats the date as we wish.

Cheers
 

artsoft

Active Member
Licensed User
Longtime User
Hey Klaus!
Hey Kanne!

Sorry for the late response.... I was very busy the last days.
All my problems are fixed now - thanks for all your tips and the new versions of Table.bas.

________________________________________

The current issue I have is the choice of the correct text size - based on the given smart device or tablet.

Using this structure:

B4X:
    Dim lv As LayoutValues
    lv = GetDeviceLayoutValues

I have the width, the height and the scale of the device's resolution.

________________________________________

My row height (the header height as well) are based on the given %y:

B4X:
    Table1.RowHeight = Ceil(4%y)
    Table1.HeaderHeight = Table1.RowHeight

This works great and looks perfect on each test device.


This works also on each device:

B4X:
Table1.RowHeight = Ceil(Table1.Height / 10)

So I can see always 10 rows on each device


_____________________________

BUT...

The question is now on how to calculate the correct Text size:

B4X:
    Dim ts As Int
    ts = Table1.RowHeight * 0.4
    Table1.TextSize = ts

My problem:
This works ok on one device and after switching to another device (in example to tablet) I see that this calculation is really wrong!



Therefore my question:
Is there a better method which can be used to calculate the correct text size - together with scale value or something like this??

Your answer is highly appreciated.

Best regards
ARTsoft

P.S.: The cell alignment is set to left and vertical center (with Bit.OR method).
 
Last edited:

Kanne

Member
Licensed User
Longtime User
you may search the forum by something like "calculation textsize".
I found Calculate textsize depending on label height and tested it with setting rowheight to 1/5 of table:
B4X:
    tableDes.RowHeight = Floor((tableDes.Height / 5))
    tableDes.TextSize =  Floor(tableDes.RowHeight * 500 / 1000dip)
In this case the font will fill about 50% of the cellheight which looks ok in my example - but you may test with other values.

@klaus:
setting the textsize after filling does not change the textsize for the fixed columns
 

artsoft

Active Member
Licensed User
Longtime User

Thanks Kanne for the tip!

Yes I also found out that the property textsize can only set before loading the data - after loading there is no chance to "resize" the text in order to adapt it to the given device resolution. This might be a really good feature.

Best regards
ARTsoft
 

Kanne

Member
Licensed User
Longtime User
Hi,

within my project it was possible in conjunction with .RefreshTable (only the bug with fixed columns).

I got an error when setting RowHeight before filling, but you can set Textsize before and Rowheight after - you know all values:
B4X:
tableDes.TextSize =  Floor(Floor((tableDes.Height / 5)) * 500 / 1000dip)
... (filling table)
tableDes.RowHeight = Floor((tableDes.Height / 5))
 
Last edited:

Kanne

Member
Licensed User
Longtime User
Hi @Kanne,

Of course it does.
But when using jRDC2 to get the query, jRDC2 returns the DATE fields as TICKS. So this routine simply formats the date as we wish.

Cheers

I can't imagine that jRDC2 will return ticks if you use
DATE_FORMAT(docdate, '%d/%m/%Y') AS Date_DMY
wihtin the SQL-statement.
The datefield will be formated as string and the middleware will give you a string.
Could you please test it for me, because I don't have a mySQL-DB und webservice to do that ... it only reflects my knowlegde of SQL.
 

artsoft

Active Member
Licensed User
Longtime User

@Kanne: Hi, I see that the factor (or better: the divisor) with value 5 is ok when I test the table on a smartphone. But on a tablet, this factory is too small. 5 entries on a smartphone provides a good text size - but on a test tablet .... OMG .... the table has a big height (the table covers about 80% of the screen and therefore I have a big height) and divided by 5 ... the row height is too high indeed.

I guess that we have to calculate this divisor as well ... while using the scale value of a screen.

Regards
ARTsoft
 

Kanne

Member
Licensed User
Longtime User
sure ... 5 rows is what you posted and asking how to fit the textsize according to the new rowheight.
You may calculate and set rowheight and the corresponding textsize as you want, e.g. to a readable size for you on the specific device.
 

Sergio Castellari

Active Member
Licensed User

Hi @Kanne !!

What you show is correct. In that case, deliver the date in a string.
My APP is sharing information with an ERP system in real time. This is why I don't want to modify queries (if not necessary) to maintain a certain order.
Also, I don't have that much knowledge. But I love B4A as it is helping me to get my APP.
Cheers!
 

Kanne

Member
Licensed User
Longtime User
Hi Klaus,
I tested a little with adjustment of the textheight / rowheight and found some minor bugs:
- "ScaleTable" seems not to scale the headerheight; could be solved by setting HeaderHeight to RowHeight after filling
- changing RowHeight after filling does not adjust panelheight so I have a black area at the end (when reducing) or can't scroll to the end (when increasing)
 

klaus

Expert
Licensed User
Longtime User
I was also looking at the ScaleTable method and improved it.
The ScaleTable method must be called before filling the Table.
Can you please test the attached Table.bas file.
 

Attachments

  • Table.bas
    122.3 KB · Views: 202

Kanne

Member
Licensed User
Longtime User
I made some test with my Table-Test-project:
V1: showing default layout (upper table runtime, lower table designer)
V2: tableRun.ScaleTable(1.5,1.5,False): scale is bigger than 1.5, also I don't want to change width / height of table, only labelsize / textsize
V3: tableRun.ScaleTable(1.5,1.5,False) & reset table width & height (before filling): scale is bigger than 1.5
V4: tableRun.ScaleTable(1.5,1.5,true): new header-hight not calculated for top of the rows
V5: tableRun.ScaleTable(1.5,1.5,true) & HeaderHeight = RowHeight (after filling): that is what I want

I don't know what the 3rd boolean-param is used for: when do I have to set it to which value ?
 

Attachments

  • V1.jpg
    58.8 KB · Views: 208
  • V2.jpg
    67.8 KB · Views: 217
  • V3.jpg
    63.8 KB · Views: 225
  • V4.jpg
    63 KB · Views: 215
  • V5.jpg
    61.7 KB · Views: 229
  • B4A Test Table.zip
    30.9 KB · Views: 178

klaus

Expert
Licensed User
Longtime User
Thank you for testing !
I added the ScaleTable method a long time ago, when anchors didn't exist.
At that time I wrote the Scale module, shortly after AutoScale was added in the Designer, allowing AutoScale also for views added in the code. This is mostly no more used.
The ScaleTable method was added after the Scale module to allow scaling Tables in conjunction with the Scale module, the third parameter depends if the ScaleAll method from the Scale module was used or not.
In your cases always False.

But, the ScaleTable method rescales all views in the Table!
Not sure that it will work OK with anchors.
Therfore, if you want to change only the heights and text sizes you must use the Height and TextSize properties or methods and not the ScaleTable method.
 

Diceman

Active Member
Licensed User
Do you think a column in FlexibleTable could handle wordwrapped text? When displaying data from databases I often have to display the first 'n' lines of paragraph text in a grid. It is usually 2 or 3 lines and if that is not enough then the text is truncated in the cell to 'n' lines and the usual "..." is appended to the end of the displayed text so the user knows there is more text.

Without wordwrap then the paragraph text would occupy too much horizontal space. I'd rather sacrifice a little vertical space instead. I would specify the cell property to display up to '3' rows and the max column width is 300dip across.

Is a future version of FlexibleTable capable of doing this?
TIA
 
If anybody will have the need to load JSON Data in the Table, I wrote a Sub that does it:
(Based on flexible Table Version 3.21)

LoadFromJson:
'load data from a JSON String

'load data from a JSON String
'JsnStr = String
'FieldArr = Array of shown Fields (of String), maybe less than fields in the JSON
'AutomaticWidths  True > set the column widths automaticaly

Public Sub LoadFromJson(JsnStr As String, FieldArr() As String, AutomaticWidths As Boolean)

    cAutomaticWidths = AutomaticWidths
    mNumberOfColumns = FieldArr.Length

    Dim Jsnpsr As JSONParser
    Dim JsnObjList As List
    Dim Headers(mNumberOfColumns) As String
    Dim ColumnWidths(mNumberOfColumns) As Int
    Dim HeaderWidths(mNumberOfColumns) As Int
    Dim DataWidths(mNumberOfColumns) As Int
    Dim cColumnDataType(mNumberOfColumns) As String
    Dim col, row As Int
    Dim str As String
    

    innerClearAll(mNumberOfColumns, True)
    Jsnpsr.Initialize(JsnStr)
    Try
        'assuming that the top level value is an array - everything is okay
        JsnObjList = Jsnpsr.NextArray
    Catch
        Try
            'assuming that the top level value is an object, then make it to an array
            JsnObjList.Initialize
            JsnObjList.Add(Jsnpsr.NextObject)
        Catch
            Log(LastException)
            Return
        End Try
    End Try
    
    For col = 0 To mNumberOfColumns - 1
        cColumnDataType(col) = "TEXT"
        Headers(col) = FieldArr(col)
        If AutomaticWidths = False Then
            ColumnWidths(col) = 130dip
            HeaderWidths(col) = 130dip
            DataWidths(col) = 130dip
        Else
            If MultiTypeFace = True Then
                HeaderWidths(col) = cvs.MeasureStringWidth(Headers(col), cHeaderTypeFaces(col), cTextSize) + ExtraWidth
            Else
                HeaderWidths(col) = cvs.MeasureStringWidth(Headers(col), cHeaderTypeFace, cTextSize) + ExtraWidth
            End If
            DataWidths(col) = 0
            For row = 0 To JsnObjList.Size - 1
                str = FieldArr(col)
                If str <> Null Then
                    If MultiTypeFace = True Then
                        DataWidths(col) = Max(DataWidths(col), cvs.MeasureStringWidth(str, cTypeFaces(col).DEFAULT, cTextSize) + ExtraWidth)
                    Else
                        DataWidths(col) = Max(DataWidths(col), cvs.MeasureStringWidth(str, cTypeFace, cTextSize) + ExtraWidth)
                    End If
                End If
            Next
            ColumnWidths(col) = Max(HeaderWidths(col), DataWidths(col))
        End If
    Next
    SetHeader(Headers)
    SetColumnsWidths(ColumnWidths)

    'For row = 0 To JsnObjList.Size - 1
    For Each colroot As Map In JsnObjList
        Dim R(mNumberOfColumns), str As String
        For col = 0 To mNumberOfColumns - 1
            str = colroot.Get(FieldArr(col))
            If str <> Null Then
                R(col) = str
            Else
                R(col) = ""
            End If
        Next
        AddRow(R) 
    Next
'    Log(Data.Size)
End Sub

End Sub
 
Last edited:
.. and here is a little example
(Don't forget to include the JSON Library in your project)

Example for LoadFromJson:
Sub Activity_Create(FirstTime As Boolean)
   
   
    Dim jsnstr As String = "[{""ArtNr"":""12345"",""Desc"":""Chair"",""Material"":""Wood"",""Color"":""Green""},{""ArtNr"":""22331"",""Desc"":""Table"",""Material"":""Wood"",""Color"":""Yellow""},{""ArtNr"":""98765"",""Desc"":""Lamp"",""Material"":""Steel"",""Color"":""None""},{""ArtNr"":""77777"",""Desc"":""Window"",""Material"":""Glass"",""Color"":""transparent""}]"
    Dim FieldNames() As String = (Array As String("ArtNr", "Desc", "Material", "Color"))
   
    Activity.LoadLayout("MyNewLayout")
    Table1.LineWidth = 3dip
    Table1.FastScroll = True
    Table1.FastScrollMinItems = 15
    Table1.FastScrollColumnIndex = 2
    Table1.LoadFromJson(jsnstr, FieldNames, True)
    Table1.SetColumnsWidths(Array As Int(150dip, 300dip, 300dip, 300dip))
End Sub
 
Last edited:

Diceman

Active Member
Licensed User
That looks quite useful. Are you planning on writing a "Table.SaveToJson" method so the user can also store the contents?
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…