B4A Library [Class] Flexible Table

lhbrito

Member
Licensed User
Longtime User
Hello! Here is a new version with some changes that I believe could be helpful to others as well.

To facilitate the description of the changes, imagine the following scenario:

The changes are:

- New event: OnLoadSQLite
This event is triggered before add a new line when loading the result of a query. Through this event you can modify the contents of any column before adding.

Considering the above scenario, the query would be: "select code, description, price, price_promotional, start_promotion, end_promotion from products order by description asc"

B4X:
Sub Table_OnLoadSQLite(aR() As String, Curs As Cursor)
  If Date.Now>=Curs.GetDouble("start_promotion") And Date.Now<=Curs.GetDouble("end_promotion") Then 'ignore this comparation - this is only example...
    aR(2) = Curs.GetDouble("price_promotional")
    aR(1) = aR(1)&"** PROMOTION"
  Else
    aR(2) = Curs.GetDouble("price")
  End If
End Sub

- New event: BeforeSort and AfterSort
When the table is large (eg, over 3000 lines), the sort of takes a few seconds. With these events, you can put a warning on the screen or manually control the way that the ordering is performed (can rerun the query using an "ORDER BY", as example)

Considering the above scenario, could have the following use:

B4X:
Sub Table_BeforeSort (col As Int, Ascend As Boolean) As Boolean
  If (col<>2) Then
    Panel_Processing.Visible = True
    DoEvents
    Return False 'can sort column
  Else
    Return True 'not sort this column
  End If
End Sub

Sub Table_AfterSort(col as Int, Ascend As Boolean)
  Panel_Processing.Visible = False
  DoEvents
End Sub

- New property: StatusLineColor, StatusLineTextColor, StatusLineAlignment and CaptionStausLine
To customize the visual of Status Line and customize text used in Automatic Status Line.

For the scenario, will be:
B4X:
Table.CaptionStatusLine = "product(s)"

- New subs: hideCols and unhideCols
If is necessary hide or unhide more than one column. Eg: Table.HideCols( Array as Int(2,3,4)

- SetSorted
Useful to adjust initial order status of table.

- Another code changes:
SetColumnsWidths: Correction to adjust size of column when exists hide cols (previous version show an additional space)

hideCol: Now really hide a column (not set size to '1', but set visible property to false)

unHideCol: Now is optional "newsize" (if is "0"), because the original size is preserved.

SelectionSort: optimizations, for big tables was significative.

LoadSQLiteDB2: New parameter: ResetNumberOfColumns and new datatype to column: "function"
If parameter "ResetNumberOfColumns" is true, the number columns of table will be adjust to same number of columns query result. If it is false, the result query can have more columns than coluns table. Same change made in LoadSQLiteDB.

For the scenario:
B4X:
Table.InitializeTable(3,Gravity.CENTER,False)
query = "select code, description, price, price_promotional, start_promotion, end_promotion from products order by description asc"
Table.LoadSQLiteDB2(SQL,query,False,False,Array As String("T","T","MaskPrice"))

Sub Table_MaskPrice(Value As String) As String
  If Value<>"" Then
    Value = "US$ "&Value
  End If
  Return Value
End Sub

P.S.: Sorry for any mistake in my english...
 

Attachments

  • Table.bas
    68.5 KB · Views: 272

lhbrito

Member
Licensed User
Longtime User
Thank you for sharing this.
But, I will not add these functions to the class because I don't want to make it more complicated with more specialized functions.
One comment, you used version 2.10 but the latest version is 2.12.

Hi Klaus! Thanks for reply. I didn't see that current version is 2.12, so I made again the changes on this version.

Forgive me for insisting: One of changes is to fix a "little bug" on hide columns. See the image original.png, I drawed in red. The "bug" is: When hide many columns, stay in screen a little space after last visible column. This 'bug' occurs because current version set column size to '1' when hide it. With my changes, the class don't make any changes on size of columns, just set visible property to 'false'.

This new version that I send is full compatible with current version, no change is required to used.

See these projects, they are based on your original example, i just put 3 checkbox to hide or unhide columns and increase the size of sql database to 3000 records (my customers has tables with more 6000 records):

original.zip : Table with version 2.12. Problems:

suggest.zip: Any changes make in project, i just put my version of Table.pas
- No more "bug" when hide columns
- The sort operation is more fast
- Is possible resolve all previous comments, but to this is necessary a bit changes in code (see newfeatures.zip).

newfeatures.zip: Same version of Table.pas that used in suggest.zip, but I changes to show the news features:
- When "open" table, it show correctly status of the sort on Header
- When touch on header to sort show a warning, is more "friendly" to user.
- When unhide a column the original size is restored automatically

Thank for your time and for this class, it's great!!!
 

Attachments

  • original.png
    263.9 KB · Views: 260
  • suggest.png
    258.4 KB · Views: 246
  • NewFeatures.png
    77 KB · Views: 261
  • original.zip
    104.5 KB · Views: 241
  • suggest.zip
    105.3 KB · Views: 239
  • newfeatures.zip
    106 KB · Views: 267

klaus

Expert
Licensed User
Longtime User
You can set two row colors.
Table1.RowColor1 = Colors.Red 'for odd rows
Table1.RowColor2 = Colors.Blue 'for even rows
Then you have:
Table1.SelectedRowColor = Colors.Yellow
Table1.SelectedCellColor = Colors.Cyan

Setting different colors for each row will not be added, same for different cell colors, I don't want to make the class more and more complicated.
The source code is available, so every one can adapt the class to its own needs.
 

orynkanov

Member
Licensed User
Hello!
I added the ability to set the color of cells and rows.
Also found an error in the code and fix it in innerClearAll procedure -
B4X:
If SV.IsInitialized = False Then SV.Initialize (0, 0, "SV")
. It seems so right.
 

Attachments

  • Table.bas
    69.5 KB · Views: 229

advansis

Active Member
Licensed User
Longtime User
Am I wrong or should it be:

#Event: CellClick (ColNum as int,RowNum as int)
#Event: CellLongClick (ColNum as int,RowNum as int)
#Event: HeaderClick (ColNum as int)
#Event: HeaderLongClick (ColNum as int)

?
 

klaus

Expert
Licensed User
Longtime User
If you are speaking of the event routines in the class, you are wrong!
These event routines are internal to the Table Class.
Outsides the Table class you can define event routines which are called from event routines in the class.
These need to be like:
Table1_CellClick(ColumIndex As Int, RowIndex As Int)
 

klaus

Expert
Licensed User
Longtime User
Try to comment these two lines:
B4X:
visibleRows.Initialize
'    SV.VerticalScrollPosition = 0
'    DoEvents
SV.VerticalScrollPosition = 0
Commenting the two lines above works with simple test examples.
I don't remember when this was added and by whom.
ScrollToNow doesn't exist in ScrollView2D.
A SmoothScrollTo method does exist, perhaps try with this one.
 

rad

Member
Licensed User
Longtime User
Hi all...
How to set the cursor to go (select) last row?
or how do we know the last number row of table?

Regards
 

klaus

Expert
Licensed User
Longtime User
I will add the NumberOfRows as a property in the next edition.
In the meantime you can add this routine to the Table class:
B4X:
'gets the number of row, read only
Public Sub getNumberOfRows As Int
    Return Data.Size
End Sub
And call it in the code with:
Table1.NumberOfRows
 

rad

Member
Licensed User
Longtime User

Thank you so much Klaus...
I have try it, and it's run like I expected...

Regards
RAD
 

FrenchDeveloper

Member
Licensed User
Longtime User
Hello and thanks for this great view !

Am I wrong or this line is missing ?
B4X:
    NumberOfColumns = Props.Get("NumberOfColumns")
 

FrenchDeveloper

Member
Licensed User
Longtime User
Thank you for your quick reply !

mNumberOfColumns is neither declared nor used so I keep NumberOfColumns until your update

In case it will be usefull for anybody, I share a search function :
B4X:
' Search for a text in a column and return the row where it was found
' Return -1 if text was not found
Public Sub Search_text(Text As String, Column As Long) As Long
    Dim Row() As String, Row_index As Long
    For Row_index = 0 To Data.Size- 1
        Row = Data.Get(Row_index)
        If Row(Column) = Text Then
            Return Row_index
        End If
    Next
    Return -1
End Sub
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…