Im working with Primefaces wizard component and I need to show or hide a tab depending a variable.
Im doing this:
<p:wizard id="solWizard"
flowListener="#{wizardBean.onFlowProcess}"
widgetVar="wizardWidget"
>
<p:tab id="tab1" title="Tab 1">
...
</p:tab>
<p:tab id="tab2" title="Tab 2">
...
</p:tab>
<p:tab id="tab3" rendered=#{wizardBean.renderTab} title="Tab 3">
...
</p:tab>
</p:wizard>
Tab number 3 has a render condition. RenderTab is a boolean of wizardBean.
There are two ways to change the value of renderTab, one option is with a checkbox in the first tab, and that works great, every change to renderTab show and hide the tab (the checkbox has an ajax so I could update the tab).
The second way is in the bean, inside the onFlowProcess method, in the logic of the method when you go from tab 1 to tab 2 the boolean renderTab is changed, but the tab doesnt suffer any change
This is the onflowProcess method:
public String onFlowProcess(FlowEvent event) {
this.wizardStep = event.getNewStep();
try{
switch (event.getOldStep()) {
case "tab1":{
rendeTab=Boolean.TRUE;
PrimeFaces.current().ajax().update("#form");
}
...
As you can see, im updating the form with PrimeFaces.current().ajax().update("#form"), but the wizard doesnt show the tab.
What im doing wrong? is there a way to achieve what im trying to do? How could I show and hide the tab from the bean?
Thanks
Related
I'm using PrimeFaces v.5 with this version a new component is released that ColumnToggler, when view is rendered, refreshed all checkbox are checked as a default operation.
What I need to do is;
to uncheck some columns when I initialize the view,
make p:columnToggler remember checked and unchecked options when a refresh operation occurs on p:dataTable
In Primefaces 5.2 you can set the p:column visible attribute to false
<p:column ... visible="false">
You can ofcourse use EL in the visible attribute by colum index (reordering becomes more difficult)
<p:column ... visible="#{visibilityModel.visibleList[1]}">
It hides the column at the beginning depending on the return value and you can show/hide the column through the columnToggler checkbox
By using the ajax toggle event
<p:ajax event="toggle" listener="#{viewBean.onToggle}" />
You can update the state of the visibilityModel server side
public void onToggle(ToggleEvent e) {
list.set((Integer) e.getData(), e.getVisibility() == Visibility.VISIBLE);
}
See this PrimeFaces blog entry for full example to actually keep/store the state of the visibility server side so it can be reused later
The best solution depends on the PrimeFaces version you are using.
PrimeFaces >= 5.2
See the other answer in this question.
workaround for < 5.2
You need to solve the first problem manually by overriding Primefaces' own ColumnToggler.prototype.render() function
first add styleClass="not-show-at-start" to your column that you want to insvisibe at start to access in javascript render() function;
<!--This column will not be shown at start-->
<p:column headerText="Cloumn to hide initially" width="70" styleClass="not-show-at-start">
<h:outputText value="#{entityVar.nmProcessOwner}" />
</p:column>
<!--This column will be shown at start-->
<p:column headerText="Column to show initially" width="70">
<h:outputText value="#{entityVar.nmProcessOwner}" />
</p:column>
secondy create a javascript file and paste code below in it, this function will re assign render function of ColumnToggler
PrimeFaces.widget.ColumnToggler.prototype.render = function() {
//variable for creating id referance for each checkbox in ColumnToggler
var id=0;
this.columns = this.thead.find("> tr > th:visible:not(.ui-static-column)");
this.panel = $("<div></div>").attr("id", this.cfg.id).addClass("ui-columntoggler ui-widget ui-widget-content ui-shadow ui-corner-all").append('<ul class="ui-columntoggler-items"></ul').appendTo(document.body);
this.itemContainer = this.panel.children("ul");
for (var a = 0; a < this.columns.length; a++) {
id++;
var b = this.columns.eq(a);
$('<li class="ui-columntoggler-item"><div class="ui-chkbox ui-widget"><div id="cb'+id /*creating id for each checkbox for accessing later*/+'" class="ui-chkbox-box ui-widget ui-corner-all ui-state-default ui-state-active"><span class="ui-chkbox-icon ui-icon ui-icon-check"></span></div></div><label>' + b.children(".ui-column-title").text() + "</label></li>").data("column", b.attr("id")).appendTo(this.itemContainer);
//access clumns using class reference(not-show-at-start) created in jsf page
if(b.hasClass( "not-show-at-start")){
//access checkbox using id attribute created above and uncheck it
//this will hide columns that have "not-show-at-start" class
this.uncheck($('#cb'+id));
}
}
this.hide();
}
An alternative solution could be to set directly the checkbox you want to uncheck after you load the page. It's a less elegant solution but it works. I did it this way:
<h:body onload="javascript:
$('.ui-chkbox-box')[18].click();">
By this way, after loading the page, javascript hide the column referenced by chechbox number 18 but the checkbox is still present on the columnToggler for the user to check it and show the column again if he wants to.
Greetings
Complementing Damián answer:
$(function() {
$('.ui-columntoggler-items .ui-chkbox .ui-chkbox-box').click();
});
This will do the job, clicking the columntoggler chkbox after page load..
#Edit
You should set as toggleable="false" the columns you don't want to hide (always displayed)
Reason: if you use the javascript method to "override" primefaces toggler, sometimes the datatable column shown can be displayed wrong in the layout (out of the table size, for an example, like happened with me), that's why i decided to use the method described above..
I have the next structure in my xhtml
<p:panel>
<p:acordeonPanel>
<p:tab>
<h:form>
<table>
<tr>td><commandButton action="#{myBean.searchAnimals}"update=":form:partidos" ></td></tr>
</table>
</h:form>
</p:tab>
</p:acordeonPanel>
</p:panel>
<p:panel>
<h:form id="form">
<p:dataTable id="partidos" value="#{myBean.foundAnimals}">
</h:form>
....
The method searchAnimals() it´s OK, it returns the Animal items, but the dataTable doesn't refresh, but in the java code (managedBean) the ArrayList contains the items.
What can i do? Thanks
EDIT:
I have de h: and the p: suffix correctly (i think so), the code is part of the complete code but is the fragment that doesn't work correctly. In the first panel i have a filter controls (by category, by date, etc.) and the function of the command button is call a function that execute a query on the database, this function is well because it's filling the ArrayList with the resultset (myBean.foundAnimals) but these items doens't refresh on the view (dataTable) :(
You should use an <h:form> for both and set the render value from your commandButton to rendered=":form" to achieve rerendering the form after clicking the commandButton.
Use case
I have a dialog that contains a datatable and an inputTextArea. The datatable is filled when the user clicks a button. When some (single) row of the datatable is selected, I want the contents of this row to be inserted at the end of the inputTextArea.
Problem
When using the selection attribute of the datatable, I dont have the most recent contents in the inputTextArea. Scenario: I enter "ABC" in my inputTextArea, then click the button, then make my inputTextArea "FFF" and select a row ("dtText") from the datatable. The new contents of the inputTextArea is "ABC dtText" instead of "FFF dtText".
What I tried
I tried to add a new listener but I cannot figure out how to bring it in properly so that it is being called before the selection happens. Found nothing on google or in the PF User Guide.
I was hoping the blur event updates my ruleText, but it doesn't happen.
inputTextArea
<p:inputTextarea value="#{mrBean.selectedElement.ruleTxt}" id="rt1New" rows="20" cols="100" autoResize="false">
<f:ajax event="blur" update="duoDlgForm2" />
</p:inputTextarea>
datatable
<p:dataTable id="qPdt" var="p" value="#{regelBean.queriedParams}" rowKey="#{p}"
selection="#{mrBean.selectedParam}" selectionMode="single">
<p:ajax event="rowSelect" update="#form"/>
<p:column ...
java
public void setSelectedParam(ParamOrDBParamModel selectedParam) {
if(selectedParam != null) {
selectedElement.setRuleTxt(selectedElement.getRuleTxt() + "\n" + selectedParam.getName().trim());
How can I work with the most recent text (selectedElement.getRuleTxt) when I am inside the setSelectedParam method?
PF 4.0
EDIT: if I try jquery, what do I have to insert into my function
$('#qPdt tr').on('click', function() {
// I reach this point, now what? How to get the contents of a row when I know the type?
$("#rt1New").append( ?? )
});
I have some tabs and rendered attiributes. My problem is how can i set this tab's rendered attiributes to false when i close the tab. I set with setRendered method but the problem is renderTab1 variable still holds True. What i want to do is; setting renderTab1 variable to "False". By the way i have many tabs like 20-25. If you have any better solution you can share.
my xhtml;
<p:ajax event="tabClose" listener="#{myController.onTabClose}"/>
<p:tab id="firstTab" closable="true"
rendered="#{myController.renderTab1}"/>
my tabclose method;
public void onTabClose(TabCloseEvent event) {
event.getTab().setRendered(false);
}
I was find a solution and i need to did this to solve my problem.
My solution not only did with rendered, i needed to use one variable to render tab, and one to send you to position that will be my tab inside of my tabView
some like this.
in the tabView set parameters like this...
<p:tabView id="idTabView" widgetVar="tabView" dynamic="true" cache="false">
in tab
<p:tab rendered="#{mb.valiable == 1}"><!--the number 1 is an example-->
</p:tab>
</p:tabView>
On commandButton where i suppose is on the first tab i put this:
<p:commandButton
actionListener="mb.activeTab()"
update="#form"
oncomplete="PF('tabView').select(1);" <!--this line send to tab that you are rendering with your flag in mb "variable" if you are render TAB in position three you need to put PF('tabView').select(2);-->
on my MAnagedBean method:
mb{
private int valiable;
public voi activeTab(){
setValiable(1);
}
//setter y getters
}
i hope help you my solution.
regards
The rendered property of a JSF component is resolved multiple times throughout the JSF lifecycle, so setting it manually on an event is unlikely to work by the time JSF actually begins rendering. The rendered attribute however can take the following values:
A boolean property: Eg. rendered="#{myController.booleanProperty}"
A method that returns a boolean: Eg. rendered="#{myController.doStuffAndReturnBoolean()}"
An EL expression that resolves to a boolean value: Eg. rendered="#{myController.intValue gt 3}"
What i want to do is; when user clicks to row, it will select the row.When user double clicks to row, it will start cell editing. At Primefaces showcase(http://www.primefaces.org/showcase/ui/d ... nstant.jsf) it says "Instant row selection, dblclick selection and unselection is implemented using ajax behaviors." but i couldnt find where they implemented dblclick selection. Is there a way to start cell editing event with double click event?
<p:ajax event="rowDblselect">
From PrimeFaces Users Guide
Use
<p:ajax event="rowDblselect" />
in your <p:dataTable /> like this:
<p:dataTable
id="yourTableId"
value="#{yourBean.items}"
selectionMode="single"
selection="#{yourBean.selectedItem}"
var="item"
rowKey="#{item.id}">
<p:ajax
event="rowDblselect"
listener="#{yourBean.onRowDoubleClick}"
update="#form:theComponentYouWantToUpdate"
global="false" />
<!-- your columns here -->
</p:dataTable>
In your bean/controller use:
import org.primefaces.event.SelectEvent;
public void onRowDoubleClick(final SelectEvent event) {
YourObject obj = (YourObject) event.getObject();
// rest of your logic
}
Try setting dblClickSelect="true" on your table.
From the documentation:
By default, row based selection is enabled by click event, enable dblClickSelect so that clicking double on a row does the selection.