简体   繁体   English

Primefaces Datatable不会更新选定的行

[英]Primefaces Datatable doesnt update selected rows

I have a simple problem. 我有一个简单的问题。 I have a Primefaces Datatable. 我有一个Primefaces数据表。 When the user clicks on a row, I would like the selected rows property in the backing bean to be updated. 当用户单击一行时,我希望更新辅助bean中的选定行属性。 This can be achieved if the form that the Datatable is in is submitted, but I would like it to happen asynchronously. 如果提交了Datatable所在的表单,则可以实现这一点,但我希望它是异步发生的。 Ive read the various questions on here about this question, but still have not been able to find a solution. 我已经在这里阅读了关于这个问题的各种问题,但仍然无法找到解决方案。

Here is a small example to demonstrate the issue : 这是一个展示问题的小例子:

Test JSF Page: 测试JSF页面:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:p="http://primefaces.org/ui">
<h:head>
    <title>Facelet Title</title>
</h:head>
<h:body>
    <p:dataTable var="v" value="#{test.values}" selectionMode="multiple"
                 selection="#{test.selectedValue}" rowKey="#{v.value}" >
        <p:column headerText="Test">
            <h:outputText value="#{v.value}" />
        </p:column>
    </p:dataTable>
</h:body>

Backing Bean: 支持Bean:

import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import org.primefaces.component.menuitem.MenuItem;
import org.primefaces.component.stack.Stack;

@ManagedBean
@ViewScoped
public class Test 
{

    private Value[] selectedValues;

    public List<Value> getValues()
    {
        List<Value> retVal = new ArrayList<Value>();
        retVal.add(new Value("a"));
        retVal.add(new Value("b"));
        return retVal;
    }

    public Value[] getSelectedValues() {
        return selectedValues;
    }

    public void setSelectedValues(Value[] selectedValues) {
        this.selectedValues = selectedValues;
    }
}

And a simple POJO that they use: 他们使用的简单POJO:

public class Value {

    private String value;
    public Value(String value)
    {
        this.value = value;
    }

    public String getValue()
    {
        return value;
    }

    public void setValue(String value)
    {
        this.value = value;
    }
}

As per the responses, I have update the Datatable like so: 根据回复,我更新了Datatable,如下所示:

    <p:dataTable id="dt" var="v" value="#{test.values}" selectionMode="multiple"
                 selection="#{test.selectedValues}" rowKey="#{v.value}" >
        <p:column headerText="Test">
            <h:outputText value="#{v.value}" />
        </p:column>
        <p:ajax event="rowSelect"/>
        <p:ajax event="rowUnselect" />
    </p:dataTable>

This however still fails to call the setter setSelectedValues(); 然而,这仍然无法调用setter setSelectedValues(); I made them also say: 我让他们也说:

    <p:ajax event="rowSelect" update="@this" />
    <p:ajax event="rowUnselect" update="@this" />

And this only called the getter when a row was clicked. 这只在点击一行时调用了getter。 Any ideas? 有任何想法吗?

You can use the rowSelect event for it in <p:ajax> . 您可以在<p:ajax>使用rowSelect事件。

<p:dataTable ...>
    ...
    <p:ajax event="rowSelect" />
</p:dataTable>

This makes however very little sense in this context. 然而,这在这种背景下没有多大意义。 It'd have made more sense if you'd like to hook a listener or an update attribute on that as well. 如果你想在它上面挂一个listenerupdate属性,那就更有意义了。 Eg 例如

<p:dataTable ...>
    ...
    <p:ajax event="rowSelect" update="menu" />
</p:dataTable>

If you'd like to hook on unselecting of rows as well, add another one which hooks on rowUnselect : 如果你想挂钩取消选择行,请添加另一个挂在rowUnselect上的rowUnselect

<p:dataTable ...>
    ...
    <p:ajax event="rowSelect" />
    <p:ajax event="rowUnselect" />
</p:dataTable>

See also: 也可以看看:

Just add <p:ajax event="rowSelect" /> inside your datatable 只需在数据表中添加<p:ajax event="rowSelect" />

<p:dataTable selection="#{customerView.selectedEntity}" selectionMode="single">
    <p:ajax event="rowSelect" />
<!--more code-->
</p:dataTable>

This will call setSelectedEntity() in your backing bean, so you have it set when the actionListener fires 这将在您的支持bean中调用setSelectedEntity(),因此您可以在actionListener触发时设置它

This is useful if you want to call an actionListener from outside the datatable form (which won't submit the value) 如果要从数据表格外部调用actionListener(不提交值),这非常有用

I believe your statement "Works when submitting the form" is the solution. 我相信你的陈述“在提交表格时起作用”是解决方案。

I had a similar problem where my page has multiple panels, the first panel is a list of people, once someone is selected the details are loaded in the following panels. 我有一个类似的问题,我的页面有多个面板,第一个面板是人员列表,一旦选择某人,详细信息将在以下面板中加载。

So my rowSelect listener was firing but my bean always had null for my selection. 所以我的rowSelect监听器正在触发,但我的bean总是为我的选择而为null。 FYI, my rowSelect Listener updates ap:outputPanel which wraps my detail panels. 仅供参考,我的rowSelect Listener更新了ap:outputPanel,它包装了我的细节面板。 My detail panels are only rendered when the selection is not null. 仅当选择不为空时才会呈现我的详细信息面板。

I just wrapped the datatable with an h:form and everything started to work, just by selecting a row. 我只是用h:form包装了数据表,一切都开始工作,只需选择一行。

Now I used Spring, Session Scoped Component for my bean to house the data list and selection and Spring singleton Component for my controller for the rowSelect operation. 现在我使用Spring,Session Scoped Component为我的bean提供数据列表和选择,并使用Spring singleton Component为我的控制器进行rowSelect操作。 But it should still work for your example. 但它应该仍然适用于你的例子。

<p:column headerText="Test">
    <h:outputText value="#{v.value}" />
</p:column>

Remove the h:outputText tag, use as follows 删除h:outputText标记,使用如下

<p:column headerText="Test">
    #{v.value}
</p:column>

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

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