Primefaces datatable filter kill set paginator - primefaces

I want to keep filter and current page after updating PrimeFaces dataTable.
To do it I am trying to filter a datatable after a update with a commandlink. Then I will put the same page before filtering.
I use Primeface 6.0
<p:commandlink action="#{bean.updateBD}" update=":form" oncomplete="PF('table').filter(); PF('table').getPaginator().setPage(#{myBean.indexPag})>
I use this for setting page.
<p:ajax event="page" listener="#{bean.update}"/>
public void update(PageEvent event) {
int var = event.getPage();
setIndexPag(var);
}
But after de action the paginator position return for pag 0 and ignore de value of indexPag.
If I made another commandbutton and put onclick PF('table').getPaginator().setPage(#{myBean.indexPag}), if I click after filtering and paging datatable it works. But I need to do it with previous commandlink.

Related

p:rowExpansion call rowToggle event only on open

I have some code, something like this:
<p:rowExpansion>
<p:datatable>
<p:ajax event="rowToggle" listener="methodCall()">
<p:column>
My problem is that the rowToggle event is calling the listener on both open and close of the rowExpansion.
Is it possible to call the event only on open?
You need to create a listener that takes the ToggleEvent as a parameter. Then you can read the Visibility of that event to determine whether the row was opened or closed.
So, your listener method should look like:
public void onRowToggle(ToggleEvent event) {
if (event.getVisibility() == Visibility.VISIBLE) {
//...
}
}
And link it without parenthesis in your XHTML using EL:
<p:ajax event="rowToggle" listener="#{myBean.onRowToggle}"/>

primefaces tree node update issue with event Expand

In Primefaces, i want to create tree-node look like country>street>address. but i don't want get all info by first time. i'm using NodeExpandEvent, my code:
<p:tree id="treeId" value="#{bean.root}" var="item" selectionModel="single">
<p:ajax event="expand" listener=#{bean.onNodeExpand} update="treeId"/>
<p:treeNode> <h:outputText value=#{item.name}/>
</p:tre>
and in backing bean
//init values
public TreeNode createRoot(){
TreeNode root = new DefaultTreeNode("root",null);
Model model = new Model("---Select Option---",-1);// 2 properties name + value, and set label look like select one menu
TreeNode child1 = new DefaultTreeNode(model,root);
new DefaultTreeNode(new Model(),child1); //add new child to show expand
}
//handle event
public void onNodeExpand(NodeExpandEvent event) throws Exception{
TreeNode currentNode = event.getTreeNode();
Model first = (Model) cureentNode.getData(); //get model from TreeNode, name and value is ("", null)
if(first.getName().equal("") && first.getValue() == null){
currentNode.getChildren().clear(); //remove object earlier for expand
for(int i=1;i<10;i++) {
new DefaultTreeNode(new Model("Country"+i,i),currentNode);
}
currentNode.setExpanded(true); //importain, set expand for current node
}
}
and in browser, i was update. i want show look like
<icon-expander> ----Select Option-----
Country1
Country2
...
Country10
but, i only see
Country1
Country2
...
Contry10
what heppened??, if i using selection-model is checkbox, a set event is NodeSelectEvent, that's ok. but i wan't using event expand. please help me!

PrimeFaces autoComplete expand maxResults with moreText

I have a autocomplete with a maxResult and a moreText property.
XHTML
<p:autoComplete maxResults="50" moreText="show more results" ...>
<p:ajax event="moreText" listener="#{bean.onMoreText} />
</p:autoComplete
Bean
public void onMoreText(AjaxBehaviourEvent event) {
AutoComplete ac = (AutoComplete)event.getSource();
ac.setMaxResults(ac.getMaxResults() + 50);
}
What I want is that the result list expands for additional 50 rows. It does work this way but my the problem is that the result list hides after click on the moreText row. When I search again I'll get a result list with 100, 150, ... results.
How can I achive that the result list expands immediately after click on show more results
I found a solution which fits my needs by adding this code
<p:autoComplete widgetVar="widgetVarName" completeMethod="#{bean.complete}">
<p:ajax event="moreText" listener="#{bean.onMoreText}" />
</p:autoComplete>
public List<ResultType> complete(String qry) {
setQryString(qry);
// code to get results;
}
public void onMoreText(javax.faces.event.AjaxBehaviorEvent event) {
org.primefaces.component.autocomplete.AutoComplete ac = (AutoComplete) event.getSource();
ac.setMaxResults(ac.getMaxResults() + 30);
executeJS("PF('widgetVarName').search('" + getQryString() + "')");
}
public void executeJS(String source) {
org.primefaces.context.RequestContext.getCurrentInstance().execute(source);
}
What is happening now is that after you click on the moreText row the suggestionList hides, maxResults gets increased and a new search starts with the qryString used for the completeMethod.
If someone has a better solution feel free to share it with us because my current solution is a terrible workaround which invokes a new database select for data that were already transferred.

How to do MenuItem.addActionListener in PrimeFaces 4.0?

I updated PF 3.5 to 4.0 and this peace of code doesn't compile anymore.
MenuItem itemNone = new MenuItem();
itemNone.setValue(Constants.MULTI_SELECT_NONE);
itemNone.setUpdate(UPDATE_AREA_ID);
itemNone.setAjax(true);
itemNone.addActionListener(actionListenerASG);
I replaced MenuItem with DefaultMenuItem and now I only have one problem left:
"The method addActionListener(MethodExpressionActionListener) is undefined for the type DefaultMenuItem",
that's last line of code:
itemNone.addActionListener(actionListenerASG);
How do I implement this in PrimeFaces 4.0?
If you want to have an action called, you can do the following:
itemNone.setCommand("#{myBean.myAction}");
Where the action should look like:
public void myAction(){
//whatever this should do
}
If you need to know, which MenuItem has been clicked, what I am currently struggling with, here you have a hint for an approach: Primefaces 4, dynamic menu setCommand method
I have the same problem, the method itemNone.setCommand("#{myBean.myAction}"); doesn't work!
I use a workaround
<f:event type="preRenderView" listener="#{myBean.myAction}" />
On a page to display

primefaces 3.2 - Can't auto focus the next field

I encountered a strange problem, not sure if it is a bug. Basically, the focus will skip the next if the current update the next via ajax.
To duplicate the issue, you can use the below bean:
public class TestBean implements Serializable {
private BigDecimal a;
private BigDecimal b;
private BigDecimal c;
//
public TestBean() {
}
public BigDecimal getA() {
return a;
}
public void setA(BigDecimal a) {
this.a = a;
}
public BigDecimal getB() {
return b;
}
public void setB(BigDecimal b) {
this.b = b;
}
public BigDecimal getC() {
return c;
}
public void setC(BigDecimal c) {
this.c = c;
this.calculate();
}
public void calculate() {
if (a != null && b != null)
c = a.multiply(b);
}
}
and the below xhtml
<p:inputText id="a" value="#{testBean.a}" style="text-align:right">
<p:ajax event="blur" listener="#{testBean.calculate}" update="c d"/>
</p:inputText>
<p:inputText id="b" value="#{testBean.b}" style="text-align:right">
<p:ajax event="blur" listener="#{testBean.calculate}" update="c d"/>
</p:inputText>
<p:inputText id="c" value="#{testBean.c}" style="text-align:right">
</p:inputText>
<p:inputText id="d" value="#{testBean.c}"/>
First of all, I'll click the field id="a", id="a" will gain focus, and I'll key in some value. No problem here.
Then I'll press the tab key to move the cursor to the next field, id = "b", and key in some value. No problem here.
Then I'll press the tab key again, expecting the cursor to move the next field, id = "c". But it doesn't happen. Only the value of the field id="c" is updated, focus is lost some where. While focus is missing, if I press the tab key again, focus will move to id = "a" (or the first field).
Even if I don't use the tab key and use mouse click instead, if I am at the field id = "b" now and do a click on id="c", focus will go missing too.
If I remove the "c" from the field id="b" update, making it update="d", then focus will rest on field id="c", but the problem is, it doesn't get updated with the calculated value of "c".
In conclusion, I believe I can say that if the current field runs an ajax event to update the next field via update="nextfield", then, the next field cannot be focused via tab-key or the next mouse clicked. A second mouse click will bring the focus to the next field but that is tedious. However, I can update all the fields after the immediate next field and still have focus rest correctly on the immediate next field.
Is this the way it supposed to be? How to actually bring the focus to the next field in this case?
(PrimeFaces 3.2)
When the update happens, the HTML is renewed for c and d, so cursor cannot be focused on these elements.
Not sure you want to use some post event JavaScript to set the focus or just accept this behaviour.