Wednesday, July 30, 2014

Xpages : Dynamically Create Navigator TreeNodes

In the AfterPageLoad event of my custom navigator control add this:


try{

var newContainer:com.ibm.xsp.extlib.tree.impl.BasicContainerTreeNode = new com.ibm.xsp.extlib.tree.impl.BasicContainerTreeNode();
newContainer.setLabel("Dynamic Container Node");
var newNode:com.ibm.xsp.extlib.tree.impl.BasicLeafTreeNode = new com.ibm.xsp.extlib.tree.impl.BasicLeafTreeNode();
newNode.setLabel("Dynamic Basic Node");
newContainer.addChild(newNode);
var Nav = getComponent("nav1");
Nav.addNode(newContainer);

}catch(e){
print(e.toString);
}

'nav1' Navigator control















Improved version, use a Managed Bean (does not need to be managed can be just a class)







Java class:


package com.my.javaclass;

import java.io.Serializable;
import com.ibm.xsp.extlib.tree.ITreeNode;
import com.ibm.xsp.extlib.tree.impl.BasicLeafTreeNode;
import com.ibm.xsp.extlib.tree.impl.BasicContainerTreeNode;
import com.ibm.xsp.extlib.tree.impl.BasicNodeList;

public class javaclass extends BasicNodeList implements Serializable {


private static final long serialVersionUID = -7600042596799645230L;

        public javaclass() {
                 
                   BasicContainerTreeNode newContainer = new BasicContainerTreeNode();
                   newContainer.setLabel("Dynamic Container Node");
                   BasicLeafTreeNode newNode = new BasicLeafTreeNode();
                   newNode.setLabel("Dynamic Basic Node");
                   newNode.setSubmitValue(/*some value you want to submit*/);
                   newNode.setSelected(/*some boolean function*/);
                   newContainer.addChild(newNode);
                   addChild(newContainer);

      }
}


2 comments:

  1. Thank you so much for the code! This solves a huge problem that I have.

    Can you tell me if I generate a dynamic set of BasicLeafTreeNodes by reading some documents to generate the setSubmitValues, how can I go back and update the setSelected value to true when a node is clicked?

    ReplyDelete
  2. Well when creating that dynamic set, you need to add a submitValue and a selected value on each node.
    In the navigator onClick event you add something like this :

    (you can also use a sessionScope variable instead of this managed java bean class Main.Navigation)
    Just store the submittedValue somewhere...

    Main.Navigation.currentCategory = context.getSubmittedValue();

    Example :

    ContainerNode1
    - ContainerNode2
    - newNode1

    // the submit value should contain the entire string parent\\child\\child
    newNode.setSubmitValue("ContainerNode1\\ContainerNode2\\newNode1");
    newNode.setSelected(Main.Navigation.currentCategory.equals("ContainerNode1\\ContainerNode2\\newNode1");

    ReplyDelete