The Table Component requires a couple of define commands to work with UBot and also requires the UBot “Table Commands” plugin to be activated.

To load data into the table we set the “Table Data” #table that was set in UStrap to the value of the &table.

set(#table,&table,"Global")

The first row (0) in the &table is used to define the column fields, so these must match exactly.

Now when the user interacts with the table such as deleting/editing rows. A variable #usTableData is set with a json object.

Next the USTableAction define command is called, we then parse out the “Action” and manipulate the table accordingly.

comment("
1) \"#usTableData\" will be set as JSON object.
2) \"USTableAction\" will be called.")
comment("USTableAction - CALLED ON TABLE INTERACTION")
define USTableAction {
    comment("Parse TABLE NAME from #usTableData")
    set(#Table Name,$eval("JSON.parse(\'{#usTableData}\').table"),"Global")
    comment("Parse ACTION from #usTableData")
    set(#Action,$eval("JSON.parse(\'{#usTableData}\').action"),"Global")
    comment("Parse INDEXES from #usTableData")
    if($comparison(#Action,"=","delete_rows")) {
        then {
            USDeleteRows(#Table Name, $eval("JSON.parse(\'{#usTableData}\').indexes"))
        }
        else if($comparison(#Action,"=","edit_cell")) {
            USEditCell($eval("JSON.parse(\'{#usTableData}\').table"), $eval("JSON.parse(\'{#usTableData}\').rowIndex"), $eval("JSON.parse(\'{#usTableData}\').colIndex"), $eval("JSON.parse(\'{#usTableData}\').newValue"))
        }
        else if($comparison(#Action,"=","Your_Defined_Action")) {
            comment("Do Something :-)")
        }
        else {
        }
    }
}

 

USDeleteRows/USEditCell Defines (called from USTableAction)

This is where you need to add your logic to edit the tables. The #Table variable will contain the Table Name set in UStrap.

comment("**ADD YOUR TABLE LOGIC HERE 
TO DELETE & EDIT ROWS ECT**")
define USDeleteRows(#Table Name, #Indexes) {
    set(#delete_counter,0,"Local")
    with each($list from text(#Indexes,","),#Index) {
        set(#Index,$subtract($add(#Index,1),#delete_counter),"Local")
        increment(#delete_counter)
        if($comparison(#Table Name,"=","table")) {
            then {
                plugin command("TableCommands.dll", "delete from table", &table, "Row", #Index)
            }
            else if($comparison(#Table Name,"=","yourOtherTableName")) {
                plugin command("TableCommands.dll", "delete from table", &yourOtherTableName, "Row", #Index)
            }
            else {
            }
        }
    }
}
define USEditCell(#Table Name, #Row, #Column, #Value) {
    if($comparison(#Table Name,"=","table")) {
        then {
            set table cell(&table,$add(#Row,1),#Column,#Value)
        }
        else if($comparison(#Table,"=","yourOtherTableName")) {
            set table cell(&yourOtherTableName,$add(#Row,1),#Column,#Value)
        }
        else {
        }
    }
}

Any question please feel free to leave a comment. 🙂


Leave a Reply

Your email address will not be published.