B4J Tutorial [BANanoWebix] Lesson 8.2. The DataTable/DataGrid

Discussion in 'B4J Tutorials' started by Mashiane, Jun 25, 2019.

  1. Mashiane

    Mashiane Expert Licensed User


    Lesson 8.1
    Lesson 8.3
    Lesson 8.4

    Get your copy of BANanoWebix

    This is the second installment about the databable. Here we look at how to add our own columns in the data-table, we will not use .SetAutoConfig(True) as that will over-write our settings for the columns.

    To make this work, we have defined a WixDataColumn Type and also some constants in the WixDataTable class.

    For example, a column can be sorted either using any of...

    Public DT_SORT_INT As String = "int"
    Public DT_SORT_DATE As String = "date"
    Public DT_SORT_STRING As String = "string"
    Public DT_SORT_STRING_STRICT As String = "string_strict"
    Public DT_SORT_TEXT As String = "text"
    Public DT_SORT_SERVER As String = "server"
    Public DT_SORT_RAW As String = "raw"
    This is applied to the .SetSort(?) method of your data-column instance.

    Then you can also indicate how the column width should be adjusted/resized using any of.

        Public DT_ADJUST_DATA As String = "data"
    Public DT_ADJUST_HEADER As String = "header"
    Public DT_ADJUST_TRUE As Boolean = True
    As this lesson is rather detailed, we are translating this data:

    Dim filmset As List
    "marker" : "#ff0000""title" : "Star Trek: TOS""network" : "CBS",    "seasons" : 3))
    "marker" : "#00ff00""title" : "Firefly""network" : "Fox""seasons" :"TOO FEW!!"))
    "marker" : "#0000ff""title" : "Cheers""network" : "NBC""seasons" : 11))
    "marker" : "#ffff00""title" : "Suits""network" : "USA""seasons" : "7 (And counting)"))
    "marker" : "#ff00ff""title" : "Babylon 5""network" : "PTN",    "seasons" : "5"))
    To this beauty:


    As you can see, our data-table has editors e.g. color picker, an edit and a delete button and these respond to events and the data does not take the complete screen area. This is due to the usage of .SetAutoWidth(True) being applied to our data-table.

    We start of by creating our data-table called 'dt' then add each column we want and set the editors and templates for each column.

    Dim dt1 As WixDataTable
    ' set selection to be by row
        dt1.SetAutoWidth(True)  'adjust width of columns automatically
        Dim c1 As WixDataColumn
    Dim t As String = MarkerColor
    "marker").SetHeader(" ").SetWidth(38).SetTemplate(t).SetEditor("color").Pop
        Dim c2 As WixDataColumn
    "title").Setheader("Show title").SetWidth(140).SetSort("string").Pop
        Dim c3 As WixDataColumn
        Dim c4 As WixDataColumn
    '    '
    For the columns with .SetEditor...(?) being specified, the editor will appear each time a click to the column is made. Of particular interest is the first column, our data there are colors but we have a rounded color and also a color picker.
    Last edited: Jul 5, 2019
    joulongleu likes this.
  2. Mashiane

    Mashiane Expert Licensed User

    Column 1: The Rounded Color and its ColorPicker

    The template applied to the first color column is called MarkerColor.

    For each record added there, we apply that template with the source defined as #marker#

    'template for color
    Sub MarkerColor As String
    Dim sout As UOENowHTML
        sout.IsImportant = 
    Return sout.html
    End Sub
    We borrowed a class UOENowHTML from UOENow for us to be able to build any type of HTML element we need easily here. The generated template is basically looking like..

    <span style='background-color:#marker#; border-radius:20px; padding-right:10px;' >&nbsp&nbsp</span>
    So each time a row is added, the marker field is read and it replaces #marker# on the template and then walla, the 'output' of our data is rendered based on the template we have specified.

    By just calling .SetEditorColor / .SetEditor("color") to the column, we are indicating that the type of editor to use is a color picker. This ensures that each time we change the record in the table, the underlying data is also changed.
  3. Mashiane

    Mashiane Expert Licensed User

    The Action Buttons (Edit/Delete)

    We added action buttons here, being Edit and Delete. These calls our built in method..

    'add edit/trash
    Sub AddEditTrash
    Dim e As WixDataColumn
    Dim d As WixDataColumn
    End Sub
    When edit is and delete is clicked, the itemClick event of the datatable is called.

    Sub itemClick(row As Map)
    'get the row
        Dim rowid As String = row.Get("row")
        rowid = pg.CStr(rowid)
    'get the column selected
        Dim cID As String = row.Get("column")
    'get the item
        Dim record As Map = pg.GetItem("dt1", rowid)
    Select Case cID
    Case "edit"
    'display a message
    Case "delete"
    'prompt to confirm
            Dim resp As Boolean = BANano.Window.Confirm("Are you sure you want to delete this record?")
    If resp Then
    "dt1", rowid)
    End If
    End Select
    End Sub
    In Lesson 8.1. we used the OnAfterSelect event and here we are using OnItemClick event. This event is attached after the UI is generated using..

    Dim row As Map
    "dt1", BANano.CallBack(Me, "itemClick"Array(row)))
    Again, we get the row, convert the rowid to a string using CStr, then we get the column that was clicked.

    If we click 'Edit' we message the contents outs, however with 'Delete' we use BANano.Window.Confirm to prompt a user for the delete confirmation. If the response is positive, we remove the record from the data-table. One can easily implement features to delete records from a db directly either using BANanoSQLUtils / BANanoMySQL depending on the database they use.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice