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

java_pBh7rhOHqQ.png


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.
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

  • B4XTableSelections.zip
    54 KB · Views: 1,935
Last edited:

William Lancee

Well-Known Member
Licensed User
Longtime 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

Well-Known Member
Licensed User
Longtime 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

B4X founder
Staff member
Licensed User
Longtime 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

Expert
Licensed User
Longtime 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

Expert
Licensed User
Longtime 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.
 

vmag

Active Member
Hello. In this example, an unwanted change in the color of the selected row occurs after the table is reloaded:
1. Open the form.
2. Select a line and remember its color.
3. Click the ReLoad button.
4. Select a line and the color is already different
 

Attachments

  • B4XTableSelections.zip
    54.1 KB · Views: 618

ddefrain

Member
Licensed User
Longtime User
Note
java_pBh7rhOHqQ.png


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.
:


Awesome, for newbies like me you can apply this to B4A Apps, just add the B4XTableSelections to your App and include the B4XCollections lib
Note: that the sample included is for B4J
 
Last edited:

pipocas

Member
Hello. I suggest a small change in the FirstSelectedColumnId sub
B4X:
'Gets the first selected column id. Only relevant in MODE_SINGLE_CELL_PERMANENT and MODE_MULTIPLE_CELLS modes.
Public Sub FirstSelectedColumnId As String
    If SelectedLines.Size > 0 Then
        If LineMode Then   
            Return ""
        Else
            Dim l As List = SelectedLines.Values.Get(0)
            Return l.Get(0)
        End If   
    Else
        Return ""
    End If
End Sub
 

techart

New Member
Licensed User
When I use xselections, set it to SINGLE_CELL_PERMANENT, I found that when using search, the matching content is no longer highlighted
 
Top