B4J Tutorial [Web] SithasoDaisy SDUITable Inline Editing - Calculations

Hi Fam

Download

NB: This project is built with SithasoDaisy 2.5 BETA, however the same principles discussed here still apply.

This demo project is about how inline editing works. When some values in the table change, we calculate some other values. This is also effected when we make changes in our modal.

1720616365768.png


When inline editing is enabled, i.e. each table row has input components like textboxes, selects, radio etc, one is able to trap changes in each row. This is done by adding a change event on the table.

In this table, when any value of the table is changed, the qta, listno are multipled and the importo field updated with the result of the computation. The database is also updated with the changes.

In this example, the table is reloaded again, but that part can be excluded from the operations.

B4X:
'trap changes on table inline edits
Private Sub tblcart_ChangeRow (Row As Int, Value As Object, Column As String, item As Map)
    Dim sid As String = item.get("id")
    Dim sqta As String = item.get("qta")
    Dim slistino As String = item.get("listino")
    Dim simporto As String = item.get("importo")
    Dim snome As String = item.get("nome")
    '
    sqta = SDUIShared.CInt(sqta)
    slistino = SDUIShared.CInt(slistino)
    simporto = banano.parseint(sqta) * banano.parseInt(slistino)
     
    'update the map column with value
    item.Put("importo", simporto)
    item.Put(Column, Value)
    'update the row contents at this position
    tblcart.UpdateRow(Row, item)
    '
    'get the CurrentPage
    tblcart.SaveLastAccessedPage
    'process the old Cart
    dbcart.SetRecord(item)
    'update the new changes
    dbcart.SetField(Column, Value)
    'execute an update
    Dim newid As String = banano.Await(dbcart.UPDATE)
    If newid <> "" Then
        app.ShowToastSuccess("The Cart has been saved!")
    Else
        app.ShowToastError("The Cart could not be saved, please try again!")
        Return
    End If
    're-load the Carts
    banano.Await(mounted_cart)
    'go to the last page
    banano.Await(tblcart.ShowLastAccessedPage)
    Dim summary As Map = tblcart.SetFooterTotalSumCountColumns(Array("qta","listino","importo"))
    'get the total number of processed rows
    srowcount = summary.Get("rowcount")
    'format the value to be a thousand
    srowcount = SDUIShared.Thousands(srowcount)
    'set the first column to show the total
    tblcart.SetFooterColumn(tblcart.FirstColumnName, $"Total (${srowcount})"$)
End Sub



 
Last edited:

Mashiane

Expert
Licensed User
Longtime User
Changes in Modal

1720616621008.png


The other changes we can trap are the modal values.
When the qta or listino values are changed, the importo values are updated. Note that we have used BANano.ParseInt (Integers), you can also use BANAno.ParseFloat (Double) and use SDUIShared.CDbl also instead of SDUIShared.CInt where the values are Doubles.

B4X:
'
Private Sub txtqta_Change (event As BANanoEvent)
    event.PreventDefault
    CalculateImporto
End Sub

'
Private Sub txtlistino_Change (event As BANanoEvent)
    event.PreventDefault
    CalculateImporto
End Sub

Sub CalculateImporto
    Dim sqta As Int = SDUIShared.CInt(txtqta.value)
    Dim slistino As Int = SDUIShared.CInt(txtlistino.Value)
    Dim simporto As String = banano.parseint(sqta) * banano.parseInt(slistino)
    txtimporto.value = simporto
End Sub
 

Mashiane

Expert
Licensed User
Longtime User
Defining the table columns


For inline editing, we have used input controls.

B4X:
tblcart.AddColumnTextBox("nome", "Articolo", False)
    tblcart.AddColumnTextBox("qta", "Quantità", False)
    tblcart.SetColumnAlign("qta", "right")
    tblcart.AddColumnTextBox("listino", "Prezzo", False)
    tblcart.SetColumnAlign("listino", "right")
    tblcart.AddColumn("importo", "Importo")
    'Set the computed value for importo
    tblcart.SetColumnComputeValue("importo", "ComputeValueimporto")
    tblcart.SetColumnAlign("importo", "right")
    '
    tblcart.SetColumnWidth("qta", "10rem")
    tblcart.SetColumnWidth("listino", "10rem")
    tblcart.SetColumnWidth("importo", "10rem")

This ensures that we can change the data in the rows of the table, these changes are trapped with the _changeRow event as explained above.

Note that for the "importo" column, we have also defined a computed value. This is just for demonstration purposes and is not really necessary, because this column is already existing in the database with the computed values.

I am adding this "computation" for brevity. If for example the column didnt exist in the database and you want to know the calculated values, you would add a computed value column, which will be calculated for each row in the table when the table rows are updated.

B4X:
'compute value for importo
Sub ComputeValueImporto(item As Map) As Object            'ignore
    Dim sqta As String = item.GetDefault("qta", "0")
    Dim slistino As String = item.GetDefault("listino", "0")
    Dim simporto As String = item.GetDefault("importo", "0")
    sqta = SDUIShared.CInt(sqta)
    slistino = SDUIShared.CInt(slistino)
    simporto = banano.parseint(sqta) * banano.parseInt(slistino)
    item.Put("importo", simporto)
    Return simporto
End Sub
'
This read the values in the rows being processed and multiplies the qta and listno values and then updates the "importo" column for that row.
 
Top