【问题标题】:show/hide datatable upon button click单击按钮时显示/隐藏数据表
【发布时间】:2014-11-11 08:43:14
【问题描述】:

我有一个要求,比如我需要在单击按钮时显示/隐藏数据表。我尝试实现它,但它不起作用。下面是代码。

如果我们可以使用 ajax,请告诉我。只有当我将 ajax 设置为 false 时才有可能。

<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:p="http://primefaces.org/ui">

<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>hello world</title>
</h:head>


 <h:body>
<h:form>

    <p:outputPanel id="panel" rendered="#{bye1.showtable}">
        <p:dataTable value="#{bye1.carmodel}" var="cartypes">

            <p:column headerText="Model">
                <h:outputText value="#{cartypes.carname}">
                </h:outputText>
            </p:column>

            <p:column headerText="Location">
                <h:outputText value="#{cartypes.location}"></h:outputText>
            </p:column>

            <p:column headerText="Price">
                <h:outputText value="#{cartypes.rate}"></h:outputText>
            </p:column>

        </p:dataTable>
    </p:outputPanel>
    <p:commandButton value="show" action="#{bye1.enabletable}" update="panel">
        </p:commandButton>
</h:form>
</h:body>
</html>


package lifecycle;

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

import javax.annotation.PostConstruct;


public class bye {

private String output;

private Boolean showtable;

private List<cars> carmodel;

public List<cars> getCarmodel() {
    System.out.println("cars populated...........");
    return carmodel;
}

@PostConstruct
public void bye1() {
    System.out.println("constructor called");
    carmodel = new ArrayList<cars>();
    output = "hai";
    carmodel.add(new cars("ford","chennai","4 laks"));
    carmodel.add(new cars("AUDI","chennai","44 laks"));
}

public String getOutput() {
    return output;
}

public Boolean getShowtable() {
    return showtable;
}

public String enabletable() {
    showtable = true;
    return "";
}

}

有什么帮助吗?

提前致谢

【问题讨论】:

  • rendered="#{bye1.showtable}" 应该在数据表上,而不是父容器上
  • ya kolossus !!现在它的工作!我做了那个改变

标签: jsf jsf-2 primefaces


【解决方案1】:

在命令按钮上使用 actionListener 而不是操作。

“空字符串或相同视图 ID 的返回值也将返回到同一页面,但会重新创建视图范围,从而销毁任何当前活动的视图范围 bean,如果适用,重新创建它们:” Differences between action and actionListener

还要考虑使用布尔值与布尔值。

您的班级似乎也缺少@ManagedBean 和范围注释?

【讨论】:

  • 感谢您的评论埃米尔。我按你说的试过了,还是同样的问题,没有显示出来。将 Boolean 更改为 boolean ,使用 actionlistener
  • 方法被调用了吗?还有什么是bye1?你的豆子不是叫再见了吗?
  • ya Emil,它被调用了,我尝试在数据表 getter 和 showtable 布尔变量中放置一个 sysout。当我单击按钮时,它们会被打印出来。
【解决方案2】:

我认为update="panel" 工作不正常,panel 组件没有更新。当你禁用 ajax 时,整个页面都会更新,也许这就是为什么更新只适用于 ajax="假”。

你能试试这个并告诉我它现在是否有效吗? :

<p:commandButton value="show" actionListener="#{bye1.enabletable}" ajax="true" update=":#{p:component('panel')}">
</p:commandButton>

【讨论】:

  • 感谢您的回复 yanni,update="panel" 正在工作,并且组件的 getter 也被调用(我检查了 sysout)。我也尝试了您提供的解决方案。问题是,在这两种情况下,数据表列表的 getter 都会被调用,但数据表没有显示
  • 你的'bye1'类的范围是什么?您还可以提供来自方法enabletable 的代码和'showtable' 的声明+ getter+ setter 吗?您是否尝试过在 getShowtable() 上调试并检查它是否返回正确的值?
  • bye1 bean 是会话范围的。 bean 是在问题本身,你可以向下滚动它。按钮单击时,值正在打印 yanni 。但表格不可见
  • 再次,你能检查一下 'getShowtable()' 是什么 a) 从 'rendered=..' 调用的 b) 它返回什么?难道它总是返回假?
  • 嗨,yanni,我在单击命令按钮时得到以下输出: showtable = true showtable = true showtable = true showtable = true carname =ford location :chennai rate :4 laks carname =AUDI location :chennai rate :44 拉克
【解决方案3】:

我找到了这个解决方案。

我做了这样的改变

<p:outputPanel id="panel" >
        <p:dataTable value="#{bye1.carmodel}" var="cartypes"
            rendered="#{bye1.showtable}">
     ..............
       </p:dataatble>
 </p:outputPanel>

将渲染属性设置为数据表而不是输出面板。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-21
    • 2013-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-14
    • 1970-01-01
    相关资源
    最近更新 更多