B4J Question [Web App] Fill datatable from text or array

ziovec

Member
Licensed User
Hello there, again :)

Always experimentig around datatables and ingenous ways on how to populate them.

I'm writing a web app in wich the user select some items from a table, it adds other parameters to it (such as quantity, package, customization etc.) and all the choosen items are safely stored in a txt file in this way:

B4X:
[AGRUMI MISTI VASO 18, 1, 6, null]
[GERANIO PARIGINO V.27 COLONNA, 1, 6, null]
[ROSA VASO 18 PIRAMIDE, 1, 6, null]
[CICLAMINO MINI V. 11 PL., 1, 6, null]
[GERANIO PARIGINO V.27 COLONNA, 1, 6, null]
(this is just a test, forget about the null value).

I would like to grab those lines and populare another datatable where the user could review their choices.
Each line inside [ ] should be a row, and each column is separated by the comma.

I was studying this function as a reference:
B4X:
Sub FillStudentsTable(jq As JQueryElement, rs As ResultSet)
    DateTime.DateFormat = "yyyy/MM/dd" 'sortable format
    Dim data As List
    data.Initialize
    Do While rs.NextRow
        Dim row As List
        row.Initialize
        For c = 0 To rs.ColumnCount - 1
            Dim val As String = rs.GetString2(c)
            If c = 3 Then
                'convert the birthday ticks value to a date string
                val = DateTime.Date(val)
            End If
            row.Add(val)
        Next
        data.Add(row)
    Loop
    rs.Close
    jq.RunMethod("dataTable", Array As Object(CreateMap("aaData": data, "bFilter": False, _
        "bPaginate": True)))
    'bind the selection changed event to the students table
    ws.RunFunction("addSelectionToTable", Array As Object(tblStudents.Id, "TableView1_SelectedRowChanged"))
End Sub

I know this works grabbing data from a ResultSet (wich is AWESOME!), but I can't find a way to work out how to proper store a list of arrays (wich is how the txt file is written).
Any suggestion are kindly welcome ^^
 

ziovec

Member
Licensed User
You need to create a List (data) where each item in this list (row) is a List by itself. It doesn't matter where the data come from.

B4X:
    Dim ordine As List
    ordine.Initialize
    ordine.AddAll(Array As String(txtProduct.GetVal.Value, txtQta.GetVal.Value, selPack.GetVal.Value, selVarianti.GetVal.Value))
    Dim ordine2 As List
    ordine2.Initialize
    ordine2.Add(ordine)
    TW.Initialize(File.OpenOutput(File.DirApp, "tmp_ordine.txt", True))
    TW.WriteList(ordine2)
    TW.Close

(TW is a Text Writer)

This is how the txt file is written, it happens whenever the user click on a button to "add" the chosen product to the order.
If I'm getting this right, ordine2 is a list of list (besides it gets re-initialized each time and so it contains one line at time, but I can avoid it)...can you confirm this?
 
Upvote 0

ziovec

Member
Licensed User
Why not use File.WriteList?
Because this happen everytime the user clicks a button to select a product and add it to a cart.
Usinge File.WriteList gets me a text file with only the very last item added, while I need to have ALL the products.
With my code I obtain a text file composed this way, where each line is a list:

B4X:
[AGRUMI MISTI VASO 18, 1, 6, null]
[GERANIO PARIGINO V.27 COLONNA, 1, 6, null]
[ROSA VASO 18 PIRAMIDE, 1, 6, null]
[CICLAMINO MINI V. 11 PL., 1, 6, null]
[GERANIO PARIGINO V.27 COLONNA, 1, 6, null]

I NEED the text file to be like that, because I have to interact with another software that would read those lines and do stuff with them :)

Also, instead of writing a list of list, if I would write just a list the output will be like this:

B4X:
AGRUMI MISTI VASO 18
1
6
null
GERANIO PARIGINO V.27 COLONNA
1
6
null
ROSA VASO 18 PIRAMIDE
1
6
null
CICLAMINO MINI V. 11 PL.
1
6
null
GERANIO PARIGINO V.27 COLONNA
1
6
null
Not very useful for my needs ;)

By the way, I managed to work out how to fill the table with a list ^^
Now I have other problems and shenaningans, but I'll open separated threads for them ;)
 
Upvote 0
Top