【问题标题】:selectBooleanCheckbox not updated after filtered data table update过滤数据表更新后 selectBooleanCheckbox 未更新
【发布时间】:2012-10-16 12:51:44
【问题描述】:

我们有一个包含 3 列的数据表,第一列有一个 selectBooleanCheckbox,最后两列有 outputText。

我们还在此表上设置了一个过滤器,以根据过滤器布尔值显示或隐藏行。

用于数据表的实体由这 3 列属性组成,1 个布尔值,2 个字符串。见下文。

问题是,当我们在第一列选择一个过滤器例如“是”并单击更新按钮时,表格将被更新但过滤器未应用,因此表格过滤器不会刷新。

重现步骤:

  1. 加载页面,不要修改任何复选框
  2. 选择过滤器“是”
  3. 按下更新按钮
  4. 数据表未更新

这是我的 Entity.java

public class Entity {
    boolean                 enabled;
    private String          label;
    private String          description;

    public Entity(boolean enabled, String label, String description) {
        this.enabled    = enabled;
        this.label      = label;
        this.description = description;
    }

    public boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

这是我的简单 bean

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

@Controller
@Scope ("view")
public class TestBean {
    private List<Entity>    list;

    public TestBean() {
        list = new ArrayList<Entity>();

        /* Populate examples */
        list.add(new Entity(true, "Book", "This is a C book"));
        list.add(new Entity(false, "Screen", "21\" HP Screen"));
        list.add(new Entity(true, "Game", "You won 1 million"));
    }

    /*
     * Update list, in the real application it is a little bit more complicated
     * but I simplified it and the problem is reproducible for this stuff too.
     */
    public void update() {
        Entity entity;

        /* "Disable" the third because we didn't win */
        entity = list.get(2);
        entity.setEnabled(false);
    }

    public List<Entity> getList() {
        return list;
    }

    public void setList(List<Entity> list) {
        this.list = list;
    }
}

这是我的过滤布尔值的小豆子

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

/**
 * The Class BooleanListBean.
 */
@Controller
@Scope ("session")
@SuppressWarnings ("serial")
public class BooleanListBean implements Serializable {

    /**
     * Instantiates a new boolean list bean.
     */
    public BooleanListBean() {
        super();
    }

    /**
     * Gets the options.
     * 
     * @return the options
     */
    public SelectItem[] getOptions() {
        final List<SelectItem> options = new ArrayList<SelectItem>();

        final FacesContext facesContext = FacesContext.getCurrentInstance();
        options.add(new SelectItem("", "Select"));
        options.add(new SelectItem(Boolean.FALSE.toString(), "No"));
        options.add(new SelectItem(Boolean.TRUE.toString(), "Yes"));

        return options.toArray(new SelectItem[0]);
    }
}

最后是 xhtml 文件

<!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:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:fn="http://java.sun.com/jsp/jstl/functions"
    xmlns:p="http://primefaces.org/ui"
    xmlns:ft="http://primefaces.prime.com.tr/facestrace"
    xmlns:t="http://myfaces.apache.org/tomahawk"
    xmlns:util="http://java.sun.com/jsf/composite/components/util">
<ui:composition template="/xhtml/common/toolbarLayout.xhtml">
    <ui:define name="content">
        <p:commandButton value="Update"
            actionListener="#{testBean.update}"
            update=":contentForm:entityList" />

        <p:dataTable value="#{testBean.list}" var="entity" id="entityList" widgetVar="entityList">
            <p:column headerText="Activated"
                filterBy="#{entity.enabled}" filterOptions="#{booleanListBean.options}">

                <h:selectBooleanCheckbox value="#{entity.enabled}" />
            </p:column>

            <p:column headerText="Label">
                <h:outputText value="#{entity.label}" />
            </p:column>

            <p:column headerText="Description">
                <h:outputText value="#{entity.description}" />
            </p:column>
        </p:dataTable>
    </ui:define>
</ui:composition>
</html>

【问题讨论】:

  • 这似乎是一个代码审查项目。除非你能缩小问题的范围,否则很少有人会想要彻底解决这一切。
  • 为什么用 JSP 标记问题?您根本没有使用 JSP。 XHTML 文件来自其后续的 Facelets。您在那里使用的 XML 标记来自 JSF 框架。

标签: java jsp primefaces


【解决方案1】:

我认为dataTable应该有过滤的Value标签,

filteredValue="#{testBean.filteredList}"

在testBean中添加一个过滤器列表并引用它。

【讨论】:

    猜你喜欢
    • 2019-11-06
    • 2022-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-12
    • 1970-01-01
    • 2018-01-14
    • 2012-09-15
    相关资源
    最近更新 更多