Thursday, February 16, 2012

Programmatically Caputure Table values and update Viewobject

In case when data is coming from any webservice and you need to update any viewobject based on the updates the user is doing on UI table. The logic is to capture key of each row of UI table and find the key in the viewobject, get corresponding records and update the values as in table using below code. I have writtten it on button action.

public String cb1_action() {
// Add event code here...
RichTable table = this.getDtlTable();
System.out.println("Row Count:"+table.getRowCount());
Number keyValuePK;
// for (int i = 0; i < table.getRowCount(); i++)
// {
Row row = ((JUCtrlHierNodeBinding)table.getRowData(10)).getRow();
System.out.println(row.getAttribute(1)+" "+row.getAttribute(2));
keyValuePK = (Number)row.getAttribute("PrimaryFieldId"); //tenth row
System.out.println("Key Value: "+keyValuePK);
row.getAttribute("PrimaryFieldId");
//}

DCBindingContainer dbc = (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
DCIteratorBinding iter = (DCIteratorBinding)dbc.findIteratorBinding("IteratorName");
Key k1 = new Key(new Object[]{keyValuePK}); //employee id
Row[] r1 = iter.getViewObject().findByKey(k1, 1);
System.out.println(r1[0].getAttribute("PrimaryFieldId")+" "+r1[0].getAttribute("AnyotherDesc"));
r1[0].setAttribute("UpdateAnyAttr", 99999);

// row = iter.findRowsByKeyValues((Key[])row.getAttribute("SubmHeadId")) ;

return null;
}