Quantcast
Channel: Ashish Awasthi's Blog
Viewing all articles
Browse latest Browse all 165

Add and delete records (Parent/Child) in POJO Based ADF TreeTable

$
0
0

Hello All

Previously I have posted about creating POJO based ADF TreeTable , Get Selected Record from POJO based TreeTable and Traverse POJO based TreeTable

Recently a developer asked me about adding and deleting records in POJO based TreeTable so here goes a new blog post

To add records in af:treeTable first we have to check that if user has selected any parent node then new record will be added under it and if there is no selection then new record will be added as parent node, This is the core concept of this post and all these posts uses same sample application and same 2-Level Tree



Added an inputText to enter new record, a button to add this record to TreeTable and a link in treeTable column to delete record
af:treeTable XML Source-

<af:treeTablerowBandingInterval="0"id="tt1"
value="#{viewScope.ProgrammaticTreeTabBean.charatcerVal}"var="node"
rowSelection="multiple"initiallyExpanded="true"width="300px"
binding="#{viewScope.ProgrammaticTreeTabBean.treeTableBind}">
<f:facetname="nodeStamp">
<af:columnheaderText="Node Stamp"id="c1"width="250">
<af:outputTextvalue="#{node.name}"id="ot1"inlineStyle="font-weight:bold;"/>
</af:column>
</f:facet>
<af:columnid="c2"width="30"align="center">
<af:linkid="l1"icon="#{resource['images:delete1.png']}"
actionListener="#{viewScope.ProgrammaticTreeTabBean.deleteSelectedRecordAction}"/>
</af:column>
</af:treeTable>

and page looks like this-

then created component binding of input text in managed bean to get it's value ,

//Component Binding on af:inputText
private ComponentReference inputValBind;

publicvoidsetInputValBind(RichInputText inputValBind){
this.inputValBind= ComponentReference.newUIComponentReference(inputValBind);
}

public RichInputText getInputValBind(){
if(inputValBind !=null){
return(RichInputText) inputValBind.getComponent();
}
returnnull;
}

Code to Add Records in POJO based TreeTable-


/**Method to add records (Parent/Child) in treeTable
* @param actionEvent
*/
publicvoidaddRecordsTreeTableAction(ActionEvent actionEvent){
//Check that if there is a value in input text
if(getInputValBind().getValue()!=null){
//Get Collection Model from treeTable binding
CollectionModel treeModel =null;
treeModel =(CollectionModel)this.getTreeTableBind().getValue();
//Get selected row keys from treeTable
RowKeySet selectedChildKeys = getTreeTableBind().getSelectedRowKeys();
//If a record is selected
if(!selectedChildKeys.isEmpty()){
List<Seasons> seasonList =(List<Seasons>) treeModel.getWrappedData();
//Create iterator from RowKeySet
Iterator selectedCharIter = selectedChildKeys.iterator();
//Iterate over RowKeySet to get all selected childs of treeTable
while(selectedCharIter.hasNext()){
List val =(List) selectedCharIter.next();
//Get Seasons (Parent) List of selected row
Seasons s = seasonList.get(Integer.parseInt(val.get(0).toString()));
//Get charaters list of selected seasons
List<Seasons> characterList = s.getCharacters();
//If selected record is parent (Character) then add new record as it's child
if(val.size()==1){
Seasons character =new Seasons(getInputValBind().getValue().toString());
s.addCharaters(character);
}
}
}
//If no record is selected then new record will added as parent
else{
Seasons season =new Seasons(getInputValBind().getValue().toString());
seasonList.add(season);
}
//Refresh af:treeTable after adding records
AdfFacesContext.getCurrentInstance().addPartialTarget(treeTableBind.getComponent());
}
}


Code to Delete selected records from TreeTable-


/**Method to delete selected node from af:treeTable
* @param actionEvent
*/
publicvoiddeleteSelectedRecordAction(ActionEvent actionEvent){
//Get Collection Model from treeTable binding
CollectionModel treeModel =null;
treeModel =(CollectionModel)this.getTreeTableBind().getValue();
//Get selected row keys from treeTable
RowKeySet selectedChildKeys = getTreeTableBind().getSelectedRowKeys();

if(!selectedChildKeys.isEmpty()){
List<Seasons> seasonList =(List<Seasons>) treeModel.getWrappedData();
//Create iterator from RowKeySet
Iterator selectedCharIter = selectedChildKeys.iterator();
//Iterate over RowKeySet to get all selected childs of treeTable
while(selectedCharIter.hasNext()){
List val =(List) selectedCharIter.next();
//Get Seasons (Parent) List of selected row
Seasons s = seasonList.get(Integer.parseInt(val.get(0).toString()));
//Get charaters list of selected seasons
List<Seasons> characterList = s.getCharacters();
//If selected record is child (Character)

if(val.size()>1){
Seasons character = characterList.get(Integer.parseInt(val.get(1).toString()));
//Remove character name from Season List
s.removeCharaters(character);
}else{
//Remove entire Season from the list
seasonList.remove(s);
}
}
}
}

All Done :) Now run and check application


Sample ADF Application-Download
Cheers :) Happy Learning

Viewing all articles
Browse latest Browse all 165

Trending Articles