B4J Question Add a column to a tableView b4j

david13

Member
Licensed User
Hi All,
I wana know how to add a column to my tableview(the head of the column is taken from a variable, thanks
 

DonManfred

Expert
Licensed User
Longtime User
I already asked Erel to move the thread... He´ll do it later i expect
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
It may be possible to use a method to add a column, but once I needed such and I didn't have enough time to search for it.

So, I had two possibilities:

1) reconstruct the table (ie initialize a new tableView and repopulating adding the new column). This was not my prefered one, since I had a very large table unfortunately, and it would take some time to reload,

and

2) insert new columns from the beginning and set them hidden. This way, once the user needed an extra column, all I did was to change the visible state of a hidden column.
This code is an example of how I did it:

B4X:
Sub Process_Globals
   
   Private fx As JFX
   
   Private MainForm As Form
   
   Private tblView As TableView
   Private btnAddColumn As Button
   
   Private lstOfColumns As List
   Private lstOfVisibleState As List
   
   Private numOfVisibleColumns As Int
   Private maxNumOfCols As Int=5
   
   Private defaultColWidth As Int=100
   Private initTblWidth As Int
   
End Sub

Sub AppStart (Form1 As Form, Args() As String)
   
   lstOfColumns.Initialize
   lstOfVisibleState.Initialize
   
   For i=0 To maxNumOfCols-1
       Private visibility As Boolean
       Private colText As String="Column " &(i+1)
       If i<2 Then
           visibility=True
           numOfVisibleColumns=numOfVisibleColumns+1
           initTblWidth=initTblWidth+defaultColWidth
       End If
       lstOfColumns.Add(colText)
       lstOfVisibleState.Add(visibility)
   Next
   
   tblView.Initialize("")
   
   tblView.SetColumns(lstOfColumns)
   
   For i=0 To maxNumOfCols-1
       tblView.SetColumnWidth(i,defaultColWidth)
       tblView.SetColumnVisible(i,lstOfVisibleState.Get(i))
   Next
   
   btnAddColumn.Initialize("btnAddColumn")
   
   btnAddColumn.Text="add column"
   
   MainForm = Form1
   
   MainForm.RootPane.AddNode(tblView,0,0,initTblWidth,300)
   MainForm.RootPane.AddNode(btnAddColumn,0,310,100,50)
   
   MainForm.Show
   
   
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
   Return True
End Sub


Sub btnAddColumn_Click
   
   Private numOfCols As Int=tblView.ColumnsCount
   If numOfVisibleColumns<numOfCols Then
       tblView.SetColumnVisible(numOfVisibleColumns,True)
       tblView.SetColumnHeader(numOfVisibleColumns,"another column")
       numOfVisibleColumns=numOfVisibleColumns+1
       initTblWidth=initTblWidth+defaultColWidth
       tblView.PrefWidth=initTblWidth
   Else
       fx.Msgbox(MainForm,"Reached max col num","Warning")
       btnAddColumn.Enabled=False
   End If
   
End Sub
 
Upvote 0

david13

Member
Licensed User
I moved it.

Do you want to add a new column after the table was already filled with data?

I have a list of costumers that i get from a data base ( the liste changes depending on the user), the number of the columns will depend on the number of costumers. I tried this code but it does not work since the comma is inside the string!
B4X:
Dim str="Vendeur" As String
                    For i=1 To TableView_ch.Items.Size-1
                        str=str&","&m.Get("Nom")
                    Next
                    TableView2.SetColumns(Array As String(str))
 
Last edited:
Upvote 0

david13

Member
Licensed User
It may be possible to use a method to add a column, but once I needed such and I didn't have enough time to search for it.

So, I had two possibilities:

1) reconstruct the table (ie initialize a new tableView and repopulating adding the new column). This was not my prefered one, since I had a very large table unfortunately, and it would take some time to reload,

and

2) insert new columns from the beginning and set them hidden. This way, once the user needed an extra column, all I did was to change the visible state of a hidden column.
This code is an example of how I did it:

B4X:
Sub Process_Globals
  
   Private fx As JFX
  
   Private MainForm As Form
  
   Private tblView As TableView
   Private btnAddColumn As Button
  
   Private lstOfColumns As List
   Private lstOfVisibleState As List
  
   Private numOfVisibleColumns As Int
   Private maxNumOfCols As Int=5
  
   Private defaultColWidth As Int=100
   Private initTblWidth As Int
  
End Sub

Sub AppStart (Form1 As Form, Args() As String)
  
   lstOfColumns.Initialize
   lstOfVisibleState.Initialize
  
   For i=0 To maxNumOfCols-1
       Private visibility As Boolean
       Private colText As String="Column " &(i+1)
       If i<2 Then
           visibility=True
           numOfVisibleColumns=numOfVisibleColumns+1
           initTblWidth=initTblWidth+defaultColWidth
       End If
       lstOfColumns.Add(colText)
       lstOfVisibleState.Add(visibility)
   Next
  
   tblView.Initialize("")
  
   tblView.SetColumns(lstOfColumns)
  
   For i=0 To maxNumOfCols-1
       tblView.SetColumnWidth(i,defaultColWidth)
       tblView.SetColumnVisible(i,lstOfVisibleState.Get(i))
   Next
  
   btnAddColumn.Initialize("btnAddColumn")
  
   btnAddColumn.Text="add column"
  
   MainForm = Form1
  
   MainForm.RootPane.AddNode(tblView,0,0,initTblWidth,300)
   MainForm.RootPane.AddNode(btnAddColumn,0,310,100,50)
  
   MainForm.Show
  
  
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
   Return True
End Sub


Sub btnAddColumn_Click
  
   Private numOfCols As Int=tblView.ColumnsCount
   If numOfVisibleColumns<numOfCols Then
       tblView.SetColumnVisible(numOfVisibleColumns,True)
       tblView.SetColumnHeader(numOfVisibleColumns,"another column")
       numOfVisibleColumns=numOfVisibleColumns+1
       initTblWidth=initTblWidth+defaultColWidth
       tblView.PrefWidth=initTblWidth
   Else
       fx.Msgbox(MainForm,"Reached max col num","Warning")
       btnAddColumn.Enabled=False
   End If
  
End Sub
Thanks mc73 for the solutions you proposed, I also can work with, but let's see if we can find a better ones :)
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
I have a list of costumers that i get from a data base ( the liste changes depending on the user), the number of the columns will depend on the number of costumers. I tried this code but it does not work since the comma is inside the string!
B4X:
Dim str="Vendeur" As String
                    For i=1 To TableView_ch.Items.Size-1
                        str=str&","&m.Get("Nom")
                    Next
                    TableView2.SetColumns(Array As String(str))
From what I see, you need to set columns based on customers. That is, you don't want to add things after the creation of the table.
Your code does not work because you 're trying to pass a string, although a list is required.
Example:
B4X:
tbl.Initialize("")
Private lstOfCustomers As List
lstOfCustomers.Initialize
Private numOfCustomers As Int=10
For i=0 To numOfCustomers-1
       lstOfCustomers.Add("customer " & i)
Next
tbl.SetColumns(lstOfCustomers)
 
Upvote 0
Top