【问题标题】:Ajax update breaks Primefaces datatable functionalityAjax 更新破坏了 Primefaces 数据表功能
【发布时间】:2013-09-05 19:28:06
【问题描述】:

在我的应用程序中,我有一个 PF 布局,其西部有一个 Tree 节点,中心有一个内容部分,我想使用 ajax 技术动态加载不同的页面。
为了得到它,这个内容部分包含一个带有EL 表达式的ui:include 标签。当用户单击树节点按钮时,页面会在中心正确呈现(效果很好!)。但是数据表的某些功能(例如排序)已损坏或丢失。
此外,如果从网络浏览器完全刷新页面,一切正常。

为了给你一个简洁的例子,我已经简化了我的项目。
index.xhtml:

    <p:layout fullPage="true">
        <p:layoutUnit position="north" size="100" header="Top">
        </p:layoutUnit>

        <p:layoutUnit position="west" size="100" resizable="true"> 
            <h:form>
            <p:tree id="menuTree" 
                    value="#{menuController.root}"
                    var="node" 
                    selection="#{menuController.selectedNode}"
                    selectionMode="single">  
                <p:ajax event="select" update=":content" listener="#{menuController.setPage(node)}" />
               <p:treeNode>
                   <h:outputText value="#{node}" />
               </p:treeNode>
            </p:tree>
            </h:form>
        </p:layoutUnit>
        <p:layoutUnit position="east" size="50"/>

        <p:layoutUnit position="south" size="60"/>

        <p:layoutUnit position="center" id="centerlayout">   
            <h:panelGroup id="content">
            <c:if test="${not empty menuController.page}">
                <ui:include src="#{menuController.page}.xhtml" />
            </c:if>
            </h:panelGroup>
        </p:layoutUnit>
    </p:layout>
</h:body>

我想说明我已经尝试将ui:include 更改为EL 并使用容器h:panelGroup 和静态ui:include 的条件渲染,但问题仍然存在。

Tree 菜单的后台bean:

@Named
@SessionScoped
public class MenuController implements Serializable {

private TreeNode root;  
private TreeNode selectedNode;
private String pageName;

public MenuController() {  
    root = new DefaultTreeNode("Root", null);  
    TreeNode node0 = new DefaultTreeNode("Node 0", root);  

    TreeNode node00 = new DefaultTreeNode("/list", node0);  
    TreeNode node01 = new DefaultTreeNode("/list2", node0);     
}  

public TreeNode getRoot() {  
    return root;  
} 

public TreeNode getSelectedNode() {
    return selectedNode;  
}  

public void setSelectedNode(TreeNode selectedNode) {  
    this.selectedNode = selectedNode;        
}     

public void setPage(String page){
    this.pageName = page;       
}
public String getPage(){       
    return this.pageName;
}     

包含数据表list.xhtml的页面(注意list2.xhtml是相等的,更改一些文本以“观看”内容更新):

<ui:composition>  

<h:form id="ItemListForm">

    <p:panel header="Title">

        <p:dataTable id="datalist" value="#{itemController.items}" var="item"
                     selectionMode="single" selection="#{itemController.selected}"
                     rowKey="#{item.itemid}"
                     paginator="true"
                     rows="10"
                     rowsPerPageTemplate="10,20,30" >

            <p:column sortBy="#{item.itemid}" filterBy="#{item.itemid}">
                <f:facet name="header">
                    <h:outputText value="Id"/>
                </f:facet>
                <h:outputText value="#{item.itemid}"/>
            </p:column>
            <p:column sortBy="#{item.productid}" filterBy="#{item.productid}">
                <f:facet name="header">
                    <h:outputText value="ProductId"/>
                </f:facet>
                <h:outputText value="#{item.productid}"/>
            </p:column>
            <p:column sortBy="#{item.name}" filterBy="#{item.name}">
                <f:facet name="header">
                    <h:outputText value="Name"/>
                </f:facet>
                <h:outputText value="#{item.name}"/>
            </p:column>
            <p:column sortBy="#{item.description}" filterBy="#{item.description}">
                <f:facet name="header">
                    <h:outputText value="Description"/>
                </f:facet>
                <h:outputText value="#{item.description}"/>
            </p:column>
        </p:dataTable>
    </p:panel>
</h:form>

ItemController 豆:

@Named
@SessionScoped
public class ItemController implements Serializable {
private Item selected;
private List<Item> items;


public ItemController() {
    this.items = new ArrayList<>();
    this.items.add(new Item("1", "1", "Product 1", "testing sorting"));
    this.items.add(new Item("3", "3", "Product 3", "testing sorting"));
    this.items.add(new Item("2", "2", "Product 2", "testing sorting"));
    this.items.add(new Item("4", "4", "Product 4", "testing sorting"));
    this.items.add(new Item("5", "5", "Product 5", "testing sorting"));
    this.items.add(new Item("6", "6", "Product 6", "testing sorting"));
}

public Item getSelected() {
    return selected;
}

public void setSelected(Item selected) {
    this.selected = selected;
}    

public List<Item> getItems() {
    return items;
}

Item,很简单:

public class Item implements Serializable {

private String itemid;

private String productid;

private String name;

private String description;


public Item() {
}

public Item(String itemid) {
    this.itemid = itemid;
}

public Item(String itemid, String productid, String name, String description) {
    this.itemid = itemid;
    this.productid = productid;
    this.name = name;
    this.description = description;
}

public String getItemid() {
    return itemid;
}

public void setItemid(String itemid) {
    this.itemid = itemid;
}

public String getProductid() {
    return productid;
}

public void setProductid(String productid) {
    this.productid = productid;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDescription() {
    return description;
}

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

最后,我正在与:
Java EE 7
PrimeFaces 3.5 社区
JSF 2.2
玻璃鱼 4
NetBeans 7.3.1
Safari浏览器

【问题讨论】:

    标签: ajax jsf primefaces


    【解决方案1】:

    对不起,我的英语说得不好,但发生这种情况是因为 PF 只在页面中包含成功呈现页面上存在的组件所需的 javascript。如果您通过使用例如 ui:include 将组件动态地包含到页面中,则页面将不包含用于呈现该组件的 js

    【讨论】:

    • 假设这就是问题所在:那么解决方案是什么?
    【解决方案2】:

    问题是 Primefaces 3.5 不适合 glassfish 4 切换到 primefaces 4.0-SNAPSHOT,我想它可能会工作

    【讨论】:

    • 确定吗?那我试试看。
    • 除了netbeans 7.4 beta之外,我的堆栈与您完全相同,并且我在p:commandButton上遇到了ajax问题,通过升级到primefaces-4.0-SNAPSHOT修复了它
    • 噢耶!它适用于 primefaces 4.0-SNAPSHOT。万分感谢!我被阻止了,我肯定不会修改这些观点。
    • 花了我 3 个工作日来弄清楚 :) 归功于 BalusC 他指出了我正确的方式
    猜你喜欢
    • 2014-01-08
    • 2015-09-20
    • 2015-05-24
    • 1970-01-01
    • 2011-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多