Formatting/Changing Data Before Loading

Basic formatting methods

APPLICABLE TO: Grid, TreeGrid, Tree, Combo, Scheduler

When you need to update values which were returned from a database table or set some specific formatting before sending data to the client side, you should use the beforeRender event handler.

Common usage

class RenderBehavior extends ConnectorBehavior{
    public void beforeRender(DataItem data) {
        if (data->get_index() % 2 == 1 )
            data.set_row_color("red");
    } 
}
Connection res = ( new DataBaseConnection()).getConnection();
GridConnector grid = new GridConnector(res);
grid.event.attach(new RenderBehavior());
grid.render_table("grid50000","item_id","item_nm,item_cd");
  • The code snippet above sets colors for rows subject to their indices
  • during data generation, for each record outputted for the client side the beforeRender event will be executed, i.e. the handler function will be called for each record
  • data is an instance of GridDataItem object related to the current record

Data formatting

APPLICABLE TO: Grid, TreeGrid, Tree, Combo, Scheduler

class RenderBehavior extends ConnectorBehavior{
    public void beforeRender(DataItem data) {
        String field = data.get_value("some");
        data.set_value("some","<a href='details.php?id="+field+"'>"+Details+"</a>");
    } 
}
 
Connection res = ( new DataBaseConnection()).getConnection();
GridConnector grid = new GridConnector(res);
grid.event.attach(new RenderBehavior());
  • the get_value and set_value methods allow getting or setting the value of any field related to the record (it doesn't affect actual values in DB)
  • If an alias was used during data configuration, you need to use it instead of real db field name as the first parameter of the get/set command.

Using extra fields

APPLICABLE TO: Grid, TreeGrid, Tree, Combo, Scheduler

More complex formatting rules can be defined by using extra fields during the configuration.

class RenderBehavior extends ConnectorBehavior{
    public void beforeRender(DataItem data) {
        //set row color
        data.set_row_color(data.get_value("color"));
        //save in userdata 
        data.set_userdata("some_data",data.get_value("count"));
    } 
}
 
Connection res = ( new DataBaseConnection()).getConnection();
GridConnector grid = new GridConnector(res);
grid.event.attach(new RenderBehavior());
grid.render_table("some_table","id","name,price","color,count");
  • field color isn't outputted to the client side but used to define the property of a row.
  • during the update/insert operation only the 'name' and 'price' columns may be changed,the 'color' one will stay untouched.
  • the 'count' field will be sent to the client side as userdata of the row and it will be possible to access it on the client side through the related data.

Tree/TreeGrid specificity

APPLICABLE TO: Tree, TreeGrid

TreeGrid provides TreeGridDataItem and Tree provides TreeDataItem as the input parameter of the beforeRender event handler. Both of them support base operations and a few specific ones.

class RenderBehavior extends ConnectorBehavior{
    public void beforeRender(DataItem data) {
        TreeDataItem item = (TreeDataItem)data;
        if (item.get_value("complete").equals("75"))
            item.set_check_state(true);
 
        if (item.get_value("duration").equals("10"))
            item.set_image("true.gif");
        else
            item.set_image("false.gif");
    } 
}
 
tree.event.attach(new RenderBehavior());
  • the set_image method allows setting an image of a tree element (for treegrid it accepts the only parameter, while for tree it can be 3 different images for 3 states of a tree's item)
  • the set_check method exists only in TreeDataItem object and allows setting the state of the related checkbox (tree needs having checkboxes enabled in js. configuration code as well)
  • the beforeRender event can be used in dynamic Tree and TreeGrid to define which elements of hierarchy are branches and which are leafs (see details here).
Back to top