primefaces p:ajax返回值 [英] primefaces p:ajax return value
问题描述
我有一个带有primefaces 4.0的JSF 2.2应用程序.我有一个带有一些复选框的页面.我想要完成的是,当我单击一个复选框时,一个ajax请求将被触发到我的托管bean.该请求将命中一个将返回字符串的方法.因此,我不需要更新元素,而是取回js中的字符串,因为如果实现了该目标,则可以返回JSON.
我有这段代码:
I have a JSF 2.2 app with primefaces 4.0. I have a page with some checklboxes. What I want to acomplish is that when I click on one checkbox an ajax request to be fired to my managed bean. That request will hit a method that will return a string. So i do no need to update an element, but to get back the string in js, because if i achieve that then I can return an JSON.
I have this piece of code :
<p:selectManyCheckbox id="queues" value="#{viewAssignUsersMB.queueIds}" layout="grid" columns="3" converter="javax.faces.Long" converterMessage="Error.">
<f:selectItems value="#{viewAssignUsersMB.queues}"
var="queue"
itemValue="#{queue.id}"
itemLabel="#{queue.application.name}"/>
<p:ajax process="@this" partialSubmit="true" event="change" listener="#{viewAssignUsersMB.xxx()}" async="true" oncomplete="handleSaveRequest(xhr, status, args)"/>
以及支持bean的此方法:
and this method in backing bean :
public void xxx(){
RequestContext.getCurrentInstance().addCallbackParam("stringToBePassed","TriluLilu");
}
和我的js回调方法:
function handleSaveRequest(xhr, status, args) {
alert('User with username ' +args.stringToBePassed+ ' is saved successfully');
}
现在我的主要问题是我不知道如何检测上次按下的复选框,因为在后备Bean中,我具有所有选中的复选框(#{viewAssignUsersMB.queueIds})...
有没有更好的方法,因为这是一个很常见的场景/实现该目标的经典"方法是什么?
My main problem now is that I do not know how to detect what checkbox was last pressed as in my backing bean I have all the checked checkboxes (#{viewAssignUsersMB.queueIds})...
Is there a better approach for that, as this is a pretty common scenario/ What is the "classic" way to achieve that ... ?
推荐答案
您可以使用remoteCommand来实现.
You can achieve this by using a remoteCommand.
- 首先:在所有复选框输入上钩住
onchange
事件. - 第二:在事件函数处理程序中,获取元素值,并将其值传递给remoteCommand.
- 第三:作为请求参数读取支持Bean中的值.
- First: Hook
onchange
event on all the checkboxes inputs. - Second: In the event function handler, get the element value, and pass the value to the remoteCommand.
- Third: Read the value in the backing bean, as a request parameter.
xhtml
xhtml
<p:selectCheckboxMenu id="selectCheckboxMenuId" widgetVar="selectCheckWV">
<f:selectItems value="#{mainBean.map}" />
</p:selectCheckboxMenu>
<p:remoteCommand name="submitNewCheckBoxValue"
process="@this selectCheckboxMenuId"
actionListener="#{mainBean.sendLastCheckedBox()}" />
javascript
javascript
<script>
$(document).ready(function() {
PF('selectCheckWV').inputs.change(function() {
if($(this).prop('checked'))
submitNewCheckBoxValue([{name: 'submitedValue', value: $(this).val()}]);
});
});
</script>
managedBean
managedBean
public void sendLastCheckedBox() {
FacesContext facesContext = FacesContext.getCurrentInstance();
Map map = facesContext.getExternalContext().getRequestParameterMap();
String submitedValue = (String) map.get("submitedValue");
FacesMessage facesMessage = new FacesMessage(submitedValue);
facesContext.addMessage(null, facesMessage);
}
Here's a working Demo, and a small example on github [1][2]
希望这会有所帮助.
这篇关于primefaces p:ajax返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!