Following post describes the CRUD operation on ADF TreeTable component. In order to create a TreeTable component on the page, drag and drop the collection from data control on to the page as treeTable.
Following is the code for showing department's name as tree and its employees as table under selected tree node.
<af:treeTable value="#{bindings.DepartmentsView1.treeModel}" var="node" columnStretching ="last" selectionListener="#{bindings.DepartmentsView1.treeModel.makeCurrent}" rowSelection="single" id="tt1" partialTriggers="::cb2 ::cb4">
<f:facet name="nodeStamp">
<af:column id="c1">
<af:outputText value="#{node.DepartmentName}" id="ot2"/>
</af:column>
</f:facet>
<f:facet name="pathStamp">
<af:outputText value="#{node}" id="ot1"/>
</f:facet>
<af:column id="c7" headerText="EmployeeId">
<af:inputText value="#{node.EmployeeId}" id="ot6"/>
</af:column>
<af:column id="c8" headerText="FirstName">
<af:inputText value="#{node.FirstName}" id="ot7"/>
</af:column>
<af:column id="c9" headerText="LastName">
<af:inputText id="it1" value="#{node.LastName}"/>
</af:column>
<af:column id="c2" headerText="Email">
<af:inputText id="it2" value="#{node.Email}"/>
</af:column>
<af:column id="c3" headerText="HireDate">
<af:inputText id="it3" value="#{node.HireDate}"/>
</af:column>
<af:column id="c4" headerText="JobId">
<af:inputText id="it4" value="#{node.JobId}"/>
</af:column>
</af:treeTable>
Now add buttons on the page. One for Adding employee under a department, one for deleting the selected employee and one "Commit" button for saving the changes in database.
Create actionListener for "Add Employee" button and write following code in the managed bean:
public void actionCreate(ActionEvent actionEvent) {
try {
//tt1 is table id in jspx page. Instead of this, table binding can also be used to access table.
RichTreeTable richTreeTable = (RichTreeTable)actionEvent.getComponent().getParent().findComponent("tt1");
RowIterator ri = getSelectedNodeRowIterator(richTreeTable);
Key selectedNodeKey = getSelectedNodeRowKey(richTreeTable);
Row[] found = ri.findByKey(selectedNodeKey, 1);
if (found != null) {
Row foundRow = found[0];
String nodeDefname = foundRow.getStructureDef().getDefFullName();
String deptViewDefName = "com.model.DepartmentsView";
String empViewDefName = "com.model.EmployeesView";
if (nodeDefname.equals(deptViewDefName)) {
//EmployeesView is name of Employee View object.
RowSet parents = (RowSet)foundRow.getAttribute("EmployeesView");
Row childrow = parents.createRow();
parents.insertRow(childrow);
} else {
RowSet parents = (RowSet)foundRow.getAttribute("EmployeesView");
Row childrow = parents.createRow();
childrow.setAttribute("DepartmentId", foundRow.getAttribute("DepartmentId"));
parents.insertRow(childrow);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public Key getSelectedNodeRowKey(RichTreeTable richTreeTable) {
if (richTreeTable != null &&
richTreeTable.getSelectedRowKeys() != null) {
for (Object opaqueFacesKey : richTreeTable.getSelectedRowKeys()) {
richTreeTable.setRowKey(opaqueFacesKey);
return ((JUCtrlHierNodeBinding)richTreeTable.getRowData()).getRowKey();
}
}
return null;
}
public RowIterator getSelectedNodeRowIterator(RichTreeTable richTreeTable) {
if (richTreeTable != null &&
richTreeTable.getSelectedRowKeys() != null) {
for (Object opaqueFacesKey : richTreeTable.getSelectedRowKeys()) {
richTreeTable.setRowKey(opaqueFacesKey);
return ((JUCtrlHierNodeBinding)richTreeTable.getRowData()).getRowIterator();
}
}
return null;
}
For "Delete Employee" button actionListoner, write following code:
public void actionDelete(ActionEvent actionEvent) {
try {
RichTreeTable richTreeTable = (RichTreeTable)actionEvent.getComponent().getParent().findComponent("tt1");
RowIterator ri = getSelectedNodeRowIterator(richTreeTable);
Key selectedNodeKey = getSelectedNodeRowKey(richTreeTable);
Row[] found = ri.findByKey(selectedNodeKey, 1);
if (found != null) {
for (Row row : found) {
row.remove();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Commit button can be directly dragged and dropped on the page from data control.
Following is the code for showing department's name as tree and its employees as table under selected tree node.
<af:treeTable value="#{bindings.DepartmentsView1.treeModel}" var="node" columnStretching ="last" selectionListener="#{bindings.DepartmentsView1.treeModel.makeCurrent}" rowSelection="single" id="tt1" partialTriggers="::cb2 ::cb4">
<f:facet name="nodeStamp">
<af:column id="c1">
<af:outputText value="#{node.DepartmentName}" id="ot2"/>
</af:column>
</f:facet>
<f:facet name="pathStamp">
<af:outputText value="#{node}" id="ot1"/>
</f:facet>
<af:column id="c7" headerText="EmployeeId">
<af:inputText value="#{node.EmployeeId}" id="ot6"/>
</af:column>
<af:column id="c8" headerText="FirstName">
<af:inputText value="#{node.FirstName}" id="ot7"/>
</af:column>
<af:column id="c9" headerText="LastName">
<af:inputText id="it1" value="#{node.LastName}"/>
</af:column>
<af:column id="c2" headerText="Email">
<af:inputText id="it2" value="#{node.Email}"/>
</af:column>
<af:column id="c3" headerText="HireDate">
<af:inputText id="it3" value="#{node.HireDate}"/>
</af:column>
<af:column id="c4" headerText="JobId">
<af:inputText id="it4" value="#{node.JobId}"/>
</af:column>
</af:treeTable>
Now add buttons on the page. One for Adding employee under a department, one for deleting the selected employee and one "Commit" button for saving the changes in database.
Create actionListener for "Add Employee" button and write following code in the managed bean:
public void actionCreate(ActionEvent actionEvent) {
try {
//tt1 is table id in jspx page. Instead of this, table binding can also be used to access table.
RichTreeTable richTreeTable = (RichTreeTable)actionEvent.getComponent().getParent().findComponent("tt1");
RowIterator ri = getSelectedNodeRowIterator(richTreeTable);
Key selectedNodeKey = getSelectedNodeRowKey(richTreeTable);
Row[] found = ri.findByKey(selectedNodeKey, 1);
if (found != null) {
Row foundRow = found[0];
String nodeDefname = foundRow.getStructureDef().getDefFullName();
String deptViewDefName = "com.model.DepartmentsView";
String empViewDefName = "com.model.EmployeesView";
if (nodeDefname.equals(deptViewDefName)) {
//EmployeesView is name of Employee View object.
RowSet parents = (RowSet)foundRow.getAttribute("EmployeesView");
Row childrow = parents.createRow();
parents.insertRow(childrow);
} else {
RowSet parents = (RowSet)foundRow.getAttribute("EmployeesView");
Row childrow = parents.createRow();
childrow.setAttribute("DepartmentId", foundRow.getAttribute("DepartmentId"));
parents.insertRow(childrow);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public Key getSelectedNodeRowKey(RichTreeTable richTreeTable) {
if (richTreeTable != null &&
richTreeTable.getSelectedRowKeys() != null) {
for (Object opaqueFacesKey : richTreeTable.getSelectedRowKeys()) {
richTreeTable.setRowKey(opaqueFacesKey);
return ((JUCtrlHierNodeBinding)richTreeTable.getRowData()).getRowKey();
}
}
return null;
}
public RowIterator getSelectedNodeRowIterator(RichTreeTable richTreeTable) {
if (richTreeTable != null &&
richTreeTable.getSelectedRowKeys() != null) {
for (Object opaqueFacesKey : richTreeTable.getSelectedRowKeys()) {
richTreeTable.setRowKey(opaqueFacesKey);
return ((JUCtrlHierNodeBinding)richTreeTable.getRowData()).getRowIterator();
}
}
return null;
}
For "Delete Employee" button actionListoner, write following code:
public void actionDelete(ActionEvent actionEvent) {
try {
RichTreeTable richTreeTable = (RichTreeTable)actionEvent.getComponent().getParent().findComponent("tt1");
RowIterator ri = getSelectedNodeRowIterator(richTreeTable);
Key selectedNodeKey = getSelectedNodeRowKey(richTreeTable);
Row[] found = ri.findByKey(selectedNodeKey, 1);
if (found != null) {
for (Row row : found) {
row.remove();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Commit button can be directly dragged and dropped on the page from data control.