Selected tree node not set when context menu action is invoked [duplicate] - jsf

This question already has an answer here:
What event should be used on a p:tree to select a tree node and have a context menu?
(1 answer)
Closed 6 years ago.
I'm creating a PrimeFaces (5.3) tree with a context menu. Selected nodes should be stored in #{myBean.selectedNode}. When I select a node using the left mouse button the correct node is set. But, when I try to run an action on a node from a context menu, without selecting it first, the correct node isn't set (the setter in my bean is not called).
I'm following the example in the PrimeFaces showcase, and I suppose I've got everything lined up. What am I doing wrong?
As you can see, in the PrimeFaces showcase you are able to immediately right click a node, click "View", and the growl will display the correct node.
Bean
I don't think the bean code is relevant (it is ViewScoped and there is a private TreeNode selectedNode with getter and setter).
Here are the interesting bits though:
public void onNodeSelect(NodeSelectEvent event) {
MyTreeNode myTreeNode = (MyTreeNode) event.getTreeNode();
myController.setSelected(myTreeNode.getEntity());
}
public void addChild(String name) {
MyTreeNode myTreeNode = (MyTreeNode) selectedNode;
MyTreeNode childNode = myTreeNode.addChild(name);
myController.setSelected(childNode.getEntity());
myController.insert();
}
XHTML
<h:form id="mainForm">
<p:tree value="#{myBean.root}" var="node"
id="myTree" dynamic="true"
selectionMode="single" selection="#{myBean.selectedNode}">
<p:treeNode expandedIcon="ui-icon-folder-open" collapsedIcon="ui-icon-folder-collapsed"
type="myType">
<h:outputText value="#{node}"/>
</p:treeNode>
<p:ajax event="select" listener="#{myBean.onNodeSelect}" />
</p:tree>
<p:contextMenu for="myTree">
<p:menuitem action="#{myBean.addChild('new')}"
value="Add"
process="#this"
update=":mainForm:myTree"/>
</p:contextMenu>
</h:form>

hy,
is a bug in primefaces.
You can fixed this issue by youself. For it you neccessary add verification in the primefaces js file which mouse button has been pressed. You can override file primefaces.js in your project, find first call function selectNode(d) and add follow checking:
if (e.which == 1) {
this.selectNode(d)
this.cursorNode = d;
}
This is part of code you can find then function nodeClick: function (e, a) is called:
nodeClick: function (e, a) {
PrimeFaces.clearSelection();
if ($(e.target).is(":not(.ui-tree-toggler)")) {
var d = a.parent();
if (this.cfg.onNodeClick) {
this.cfg.onNodeClick.call(this, d)
}
if (a.hasClass("ui-tree-selectable") && this.cfg.selectionMode) {
var c = this.isNodeSelected(d), f = e.metaKey || e.ctrlKey, b = e.shiftKey;
if (this.isCheckboxSelection()) {
this.toggleCheckboxNode(d)
} else {
if (c && f) {
this.unselectNode(d)
} else {
if (this.isSingleSelection() || (this.isMultipleSelection() && !f)) {
this.unselectAllNodes()
}
if (this.isMultipleSelection && b) {
} else {
if (e.which == 1) {
this.selectNode(d);
this.cursorNode = d;
}
}
}
}
}
}
}

It turns out you need the undocumented contextMenu Ajax event listener.
See:
What event should be used on a p:tree to select a tree node and have a context menu?

Related

Primefaces Tree node is draggable to children

I have a PrimeFaces (6.2.5) tree, where I have enabled draggable and droppable. I have a few issues with this, and have implemented the drag and drop manually with jquery in stead, while I figure this out;
<div class="plantree" id="realplantree">
<p:tree id="plantree" value="#{curriculumngTreeFacade.root}" var="node" dynamic="false" selectionMode="single" selection="#{curriculumngTreeFacade.selectedNode}" draggable="true" droppable="true">
<p:ajax event="select" listener="#{curriculumngTreeFacade.onNodeSelect}" update="administration-form:curriculummain" oncomplete="initDNDPlan();"/>
<p:ajax event="expand" listener="#{curriculumngTreeFacade.onNodeExpand}" />
<p:ajax event="collapse" listener="#{curriculumngTreeFacade.onNodeCollapse}" />
<p:ajax event="dragdrop" listener="#{curriculumngTreeFacade.onDragDrop}"/>
<p:treeNode expandedIcon="ui-icon ui-icon-folder-open" collapsedIcon="ui-icon ui-icon-folder-collapsed">
<h:outputText value="#{node}"/>
</p:treeNode>
</p:tree>
</div>
My issues are:
I have not added an "update='plantree'" variable to the p:ajax event
for dragdrop. Even still, if I try and drag a parent category to a
sub category, it breaks my tree, because the UI tries to update. I
actually run a check for parent / child connection in the backend
code, and I dont update the tree if that's the case, but in this
case, my method in the backing bean is not even called before the
tree explodes. It does the very same on the PrimeFaces Showcase
Backing code:
public void onDragDrop(TreeDragDropEvent event) throws Exception {
TreeNode dragNode = event.getDragNode();
TreeNode dropNode = event.getDropNode();
int dropIndex = event.getDropIndex();
if (dragNode.getData() instanceof CurriculumCategoryMetaModel && dropNode.getData() instanceof CurriculumCategoryMetaModel) {
CurriculumCategoryMetaModel drop = (CurriculumCategoryMetaModel) dropNode.getData();
CurriculumCategoryMetaModel drag = (CurriculumCategoryMetaModel) dragNode.getData();
if (drop.getType() == drag.getType() && drag.getClassid() > 0) {
drag.setParent(drop);
CurriculumBuilderProvider provider = CurriculumBuilderProvider.getProvider(drop.getType());
provider.saveCategory(drag, UserSessionFacade.getUserLocale());
this.initialize();
expandCategory(root, drop.getClassid(), drop.getType());
} else {
this.initialize();
}
}
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Dragged " + dragNode.getData(), "Dropped on " + dropNode.getData() + " at " + dropIndex);
FacesContext.getCurrentInstance().addMessage(null, message);
List<String> updates = new ArrayList<>();
updates.add("administration-form:curriculummenu");
updates.add("administration-form:growl");
PrimeFaces.current().ajax().update(updates);
}
private boolean dragIsParentOfDrop(CurriculumCategoryMetaModel drop, CurriculumCategoryMetaModel drag) {
CurriculumCategoryMetaModel parent = drop.getParent();
while (parent != null) {
if (parent.getClassid() == drag.getClassid()) {
return true;
} else {
parent = parent.getParent();
}
}
return false;
}
Is there a way to add accepted nodes that you can drop into. I have 2
root nodes, for different categories, and it is not allowed to drag
and drop between them. In fact, I dont even want the root nodes to be
draggable. On the picture, the "Learning paths" and "Ad hoc" are not supposed to be draggable, since they're only containers for the categories underneath.

Primefaces update fails after removing component in a model

My goal is to create a panel where I can drag and drop elements of two different types: button and textfield. All of the elements are stored in a TreeMap and are iterated over with JSTL <c:forEach> tag. I can add extra components and remove the most recently selected ones thorough dedicated p:commandXxx fields outside of the viewPanel. Once draggable components are in the viewPanel, I can drag and drop them. Through javascript, I'm appending top and left coordinates of a particular component before the drop action to update component's location is invoked. The delete commandButton should remove the selected element from the list and trigger the update of the viewPanel.
Problem: Deleting any component except the most recently added fails to update the viewPanel resulting in the following exception:
Severe: Error Rendering View[/developer/testDr.xhtml]
org.primefaces.expression.ComponentNotFoundException: Cannot find >component for expression "button-" referenced from "defStep-10:j_idt13"..
It appears that in render time draggable's for attribute evaluates #{child.idstepNode} to null. Does anyone know why this happens, or how to circumvent this?
Interestingly, when Delete button is clicked again or the whole page is refreshed, viewPanel renders without a hitch.
<h:form id="viewPanel" class="default-step droppable" style="width:500px;height:500px;background:green;">
<p:droppable for="viewPanel" tolerance="touch" activeStyleClass="ui-state-highlight" >
<p:ajax listener="#{stepUtility.onDrop}" update="viewPanel" />
</p:droppable>
<c:forEach items="${stepUtility.stepNodesMap}" var="child">
<c:if test="${child.value.elementType == 'BUTTON'}">
<p:outputLabel id="button-${child.value.idstepNode}" style="top:${child.value.top};left:${child.value.left};
background-color: beige;position:absolute;" value="button ${child.value.elementValue}"/>
<p:draggable for="button-${child.value.idstepNode}"/>
</c:if>
<c:if test="${child.value.elementType == 'TEXT'}">
<p:outputLabel id="text-${child.value.idstepNode}" style="top:${child.value.top};left:${child.value.left};
background-color: beige;position:absolute;" value="text ${child.value.elementValue}"/>
<p:draggable for="text-${child.value.idstepNode}" />
</c:if>
</c:forEach>
</h:form>
//...
<p:commandButton value="Delete" actionListener="#{stepUtility.removeStepNode()}"
update=":viewPanel"/>
stepUtility bean
#Named(value = "stepUtility")
#SessionScoped
public class StepUtility implements Serializable {
private Integer selecteNode;
public void setSelectedElement(Integer idstepNode) //'StepNode')
{
selecteNode=idstepNode;
}
public void addNewComp(String typeNode)
{
if(stepNodesMap == null)
stepNodesMap = new TreeMap<>();
StepNodeSimple snode = new StepNodeSimple();
Integer id = 0;
if(stepNodesMap.size()>0)
{
Integer lastNodeId = ((StepNodeSimple) stepNodesMap.lastEntry().getValue()).idstepNode;
id = lastNodeId+1;
}
snode.idstepNode = id;
snode.elementType = typeNode;
snode.elementValue = typeNode +"value";
snode.left = "2px;";
snode.top = "2px;";
stepNodesMap.put(id, snode);
}
NavigableMap stepNodesMap;
public NavigableMap getStepNodesMap() {
return stepNodesMap;
}
public void setStepNodesMap(NavigableMap stepNodesMap) {
this.stepNodesMap = stepNodesMap;
}
public void removeStepNode()
{
if(selecteNode!=null)
{
stepNodesMap.remove(selecteNode);
}
}
public void onDrop(DragDropEvent dragDropEvent)
{
String dragId = dragDropEvent.getDragId();
Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
String left = params.get(dragId + "_left");
String top = params.get(dragId + "_top");
Integer idstepNode = Integer.parseInt(dragId.substring(dragId.lastIndexOf("-")+1));
setSelectedElement(idstepNode);
StepNodeSimple element = (StepNodeSimple) stepNodesMap.get(selecteNode);
element.left = left+"px;";
element.top = top+"px;";
}
}

t:dataScroller not working correctly on refresh

I'm using t:dataScroller to scroll some data from a t:dataTable and it is working fine except for one thing: every time an action that causes the screen to be refreshed is triggered the t:dataScroller's index is set to 1.
To be more clear: when i'm in the second page (index == 2) and a screen refreshing action triggers, after the refresh, the data of the dataTable is still from index 2 but the dataScroller shows that the page being displayed is the first one.
I'm using the dataScroller this way:
<t:dataScroller for="myDataTable" id="myDataScroller" paginator="true"
paginatorMaxPages="#{myBean.paginatorMxPgs}"
pageCountVar="pgCount" pageIndexVar="#{myBean.curPg}"
actionListener="#{myBean.pgListener}">
<f:facet name="prv">
<h:panelGroup rendered="#{myBean.curPg > 1}" />
</f:facet>
<f:facet name="nxt">
<h:panelGroup rendered="#{myBean.curPg != pgCount}"/>
</f:facet>
</t:dataScroller>
i'm using tomahawk20-1.1.11.jar and myfaces-api-2.0.4.jar
For setting scroller to firstpage set actionlistener on submit button.
<t:commandButton actionListener="#{IFussBean.resetDataScroller}"
action="#{IFussBean.searchLocation}"
image="images/submit-button.png">
</t:commandButton>
Binding:
if it is datatable
<t:dataTable id="data"
headerClass=""
footerClass=""
rowClasses="text_holder"
columnClasses="search_img,search_txt"
var="item"
value="#{IFussBean.searchVideoList}"
preserveDataModel="false"
rows= "6"
binding="#{IFussBean.iFussData}"
>
Declare HtmlDataTable in your Bean,define its setter getter as below:
private HtmlDataTable iFussData;
Getter and setter
public HtmlDataTable getiFussData() {
return iFussData;
}
public void setiFussData(HtmlDataTable iFussData) {
this.iFussData = iFussData;
}
Now define ActionListener Method in Bean:
public void resetDataScroller(ActionEvent e) {
if(iFussData!= null) {
iFussData.setFirst(0);
}
}
Your page will set to first page when you'll do new search.
/**********************************************************************************************************************************/
if you are using <rich:dataGrid> and your scroller is <t:dataScroller> then
<rich:dataGrid
id="data"
var="item"
columns="3"
elements="6"
width="600px"
value="#{IFussBean.searchVideoList}"
binding="#{IFussBean.iFussDataGrid}"
>
Declare HtmlDataGrid in Bean:
private HtmlDataGrid iFussDataGrid;
Its getter and setter
public HtmlDataGrid getiFussDataGrid() {
return iFussDataGrid;
}
public void setiFussDataGrid(HtmlDataGrid iFussDataGrid) {
this.iFussDataGrid = iFussDataGrid;
}
Now define its action listener in Bean which will invoke on pressing command button for search :
public void resetDataScroller(ActionEvent e) {
if(iFussDataGrid != null) {
iFussDataGrid.setFirst(0);
}
}
Invoke ActionListener on command button
<h:commandButton styleClass="submit-button" actionListener="#{IFussBean.resetDataScroller}" action="#{IFussBean.searchLocation}" image="images/submit-button.png"/>
It also give desire result.

Issue in treetable expand event jsf primefaces

I am using primfaces3.2 and i am using the component treetable. while expanding the tree i am trying to collapse all other tree. but expand event is not working for me.
xhtml:
<p:ajax event="expand" listener="#{MyBean.onNodeExpand}" update=":form1" />
MyBean:
public void onNodeExpand(NodeExpandEvent event) {
String id = ((dummyclass) event.getTreeNode().getData()).getID();
for (TreeNode app : List.getChildren()) {
dummyclass d = (dummyclass) app.getData();
if (id.equalsIgnoreCase(d.getID())) {
app.setExpanded(true);
} else
{
app.setExpanded(false);
}
}
}
dummyclass is a pojo class which contains all the getters and setters for the treetable.

primefaces tabView activeIndex issue

I have Primefaces TabView with two Tab like:
<p:tabView dynamic="true" cache="false"
onTabShow="scrollBottom(#{stanzaBean.activeIndex})"
tabChangeListener="#{messaggioBean.onTabChange}"
activeIndex="#{stanzaBean.activeIndex}" >
it works fine, except that when I change the Tab the activeIndex isn't updated on the Server and it returns always the default value.
I'm using primefaces 2.2.1.
Thank you.
Going by the PrimeFaces ShowCase example, if you give each tab an id:
<p:tabView tabChangeListener="#{indexBean.onTabChange}" >
<p:tab title="tab 0" id="tab0"></p:tab>
<p:tab title="tab 1" id="tab1" ></p:tab>
<p:tab title="tab 2" id="tab2"></p:tab>
</p:tabView>
you can get that tab id in the tabChangeListener.
public void onTabChange(TabChangeEvent event) {
System.out.println("tab id = " + event.getTab().getId());
}
Then you'll know which tab was selected.
Edit:
There is an open PrimeFaces issue 1640 TabView: Wrong activeIndex in TabChangeListener, always 0 on the problem you are having.
Edit 2:
With PrimeFaces 5.0 and up the tabChangeListener is no longer available on the tabView element but should be used via an explicit ajax tag with a tabChange event.
<p:tabView id="analysisSections" value="#{analysisBean.analysis.sections}" var="section" activeIndex="#{analysisBean.activeIndex}">
<p:ajax event="tabChange" listener="#{analysisBean.onTabChange}"/>
Also you can directly get index of tab:
public void onTabChange(TabChangeEvent event) {
activeIndex = ((TabView) event.getSource()).getIndex();
}
with all these changes, activeIndex works properly.
this worked for me:
public void onTabChange(TabChangeEvent event) {
Tab activeTab = event.getTab();
tabPanelIndex = ((TabView)event.getSource()).getChildren().indexOf(activeTab);
}
Although the question was related to PrimeFaces 2.2.1, I like to mention that in modern PrimeFaces versions (tested with version 6.2) there is no need to trigger a separate event when attribute dynamic is set to true and cache is set to false. By using this attribute combination the active index is automatically updated on the server when another tab is selected.
Facelet:
<p:tabView activeIndex="#{stanzaBean.activeIndex}"
cache="false"
dynamic="true">
Bean:
#Named
#ViewScoped
public class StanzaBean implements Serializable {
private int activeIndex;
public int getActiveIndex() {
return activeIndex;
}
/**
* Automatically called whenever a tab changes and dynamic="true"
* and cache="false".
*/
public void setActiveIndex(int activeIndex) {
this.activeIndex = activeIndex;
// do other stuff when tab changes
}
}

Resources