B4A Library [B4X] B4XTableSelections - extended selection modes for B4XTable

Erel

Administrator
Staff member
Licensed User


B4XTableSelections extends B4XTable and adds new selection modes:

SINGLE_CELL_TEMP - this is the built-in selection mode. Unlike the other selection modes it disappears automatically.
SINGLE_CELL_PERMANENT - selection of a single cell.
SINGLE_LINE_PERMANENT - selection of a single line.
MULTIPLE_CELLS - selection of multiple cells.
MULTIPLE_LINES - selection of multiple lines

Usage:
Initialize B4XTableSelections and set the mode:
B4X:
XSelections.Initialize(B4XTable1)
XSelections.Mode = XSelections.MODE_SINGLE_CELL_TEMP
Implement these two events like this:
B4X:
Sub B4XTable1_CellClicked (ColumnId As String, RowId As Long)
    XSelections.CellClicked(ColumnId, RowId)
End Sub

Sub B4XTable1_DataUpdated
    XSelections.Refresh
End Sub
The example shows how to get the selected lines or cells.
In lines mode:
B4X:
For Each rowid As Long In XSelections.SelectedLines.Keys
       
Next
In cells mode:
B4X:
For Each rowid As Long In XSelections.SelectedLines.Keys
    Dim cols As List = XSelections.SelectedLines.Get(rowid)
    For Each col As String In cols
       'cell is defined by (rowid, col)
    Next
Next
In single selection modes (SINGLE_CELL_PERMANENT and SINGLE_LINE_PERMANENT) you can use FirstSelectedRowId and FirstSelectedColumnId to get the selected line and column.

You can also make selections programmatically by modifying SelectedLines collection and calling Refresh.

The class is included in the attached B4J example.

Updates

V1.04 - Fixes an issue with hidden columns.
V1.03 - New SelectedTextColor field. Note that it depends on B4XTable v1.21+ (https://www.b4x.com/android/forum/t...sortable-searchable-customizable-table.102322).
V1.02 - Bug fix. New IsSelected property.
New FirstSelectedRowId and FirstSelectedColumnId methods. Especially useful in the single mode selections.
Depends on B4XCollections.

V1.01 - New AutoRemoveInvisibleSelections field. When True selections are removed automatically when the selected lines or cells are not visible.
 

Attachments

Last edited:

William Lancee

Active Member
Licensed User
I really like this extension. Two minor comments:

1. To prevent selection (temporarily), one needs to set both SelectionColor = 0 and "MODE_SINGLE_CELL_TEMP".
Perhaps a "MODE_NONE" would be useful.

2. The SelectionColor is only the background, in some cases this can drown out the text, in my case I had to reduce opacity of the color.
Perhaps the text color could be a contrast color to SelectionColor.
 

William Lancee

Active Member
Licensed User
3. My app is unusual in that it requires at times responses to single line selection and at times responses to the cell selected.
I have solved it with specifying single line and setting a global for last ColumId clicked but this is not a solution in general.
There should be a FirstSelectedColumnId even for MODE_SINGLE_LINE_PERMANENT. In my solution I highlight the cell slightly more than the line.
All of this to allow for "delete a line", "insert a line", "edit a cell".
 
Last edited:

Erel

Administrator
Staff member
Licensed User
To prevent selection (temporarily), one needs to set both SelectionColor = 0 and "MODE_SINGLE_CELL_TEMP".
Perhaps a "MODE_NONE" would be useful.
Don't delegate the CellClicked event and nothing will be selected.

2. The SelectionColor is only the background, in some cases this can drown out the text, in my case I had to reduce opacity of the color.
Perhaps the text color could be a contrast color to SelectionColor.
This is a good idea. I will soon release a new version with this feature.

3. This is indeed not a "standard" use case. B4XTableSelections doesn't track the clicked cell when in line mode. This is something that you will need to implement yourself.
 

Mahares

Well Known Member
Licensed User
In Erel's example in the first post, if I change the combobox items order to this:
B4X:
XSelections.Mode = XSelections.MODE_MULTIPLE_LINES
 B4XComboBox1.SetItems(Array("MULTIPLE_LINES", "SINGLE_CELL_PERMANENT","SINGLE_CELL_TEMP", "SINGLE_LINE_PERMANENT", "MULTIPLE_CELLS"))
and run it, then click multiple lines, it works, but if I clear and select SINGLE_CELL_TEMP from the combbox drop down for instance, it ignores it and selects the MULTIPLE_LINES when I click on any cell.
 

Mahares

Well Known Member
Licensed User
It is like saying: I break the code and now it doesn't run.
We are not talking about breaking the code here. We are talking about changing the list of modes order. The user should be able to choose his/her default mode. But a warning or a note should be added that explicitly states that the order of the mode constants should not be changed as it will break the code.
 
Top