简体   繁体   English

Primefaces如何在数据表选择事件后触发另一个事件

[英]Primefaces how to fire another event after datatable select event

I have a datatable with selection and a commandbutton. 我有一个带有选择和一个命令按钮的数据表。 Problem is If user doesn't select any item from datatable and clicks to button, selectedItem object returns null. 问题是,如果用户未从数据表中选择任何项目并单击按钮,则selectedItem对象将返回null。 I think datatable's selection event runs after my commandbutton's action method. 我认为数据表的选择事件在我的命令按钮的操作方法之后运行。

What i want to do is; 我想做的是 If user Doesn't select any item from datatable and clicks the button, I will create a new SelectedItem object. 如果用户未从数据表中选择任何项目并单击按钮,我将创建一个新的SelectedItem对象。 But after button's action method runs, datatable changes it to null. 但是在按钮的action方法运行之后,数据表将其更改为null。

<p:panel id="panel1">
<p:dataTable selection="#{myController.selectedItem}"
             selectionMode="single"....
//some other stuff
</p:panel>

<p:panel id="panel2">
//some other stuff
<p:commandButton value="Create" action="myController.create"....
</p:panel>

My Controller; 我的控制器;

SelectedItem selectedItem = new SelectedItem();

public void create(){
 selectedItem = new SelectedItem();
}

You can try this: 您可以尝试以下方法:
.xhtml .xhtml

<p:dataTable var="result" selection="#{myController.selectedItem}"
         selectionMode="single"....

<p:commandButton actionListener="#{myController.create()}">
    <f:setPropertyActionListener value="#{result}" target="#{myController.selectedItem}" />  
</p:commandButton>      

.java .java

SelectedItem selectedItem = new SelectedItem();  

public void create(){
  selectedItem = new SelectedItem();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM