JSF Primefaces:更新数据表是否在另一个选项卡中打开? [英] JSF Primefaces: Update a dataTable open in another tab?

查看:67
本文介绍了JSF Primefaces:更新数据表是否在另一个选项卡中打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Web应用程序,它允许用户创建交易,并且在另一页上,用户可以查看所有过去的交易.我想要:

I have a simple webapp which allows the user to create transactions, and on another page the user can see all past transactions. I would like:

  • 当我打开过去的交易页面时,要使dataTable是最新的
  • 在创建页面上创建事务时更新dataTable

我不确定这是否可以涵盖一两个功能.另外,我不确定如何将功能分离. 事务的创建应该触发数据表的刷新,还是数据表本身应该在数据库中找到新条目?

I am not sure if this can be covered by one or two functionalities. Also, I am not sure how to decouple the functionalities. Should the creation of a transaction trigger the refresh of the dataTable, or should the dataTable itself find new entries in the DB ?

过去的交易页面:

我的TransactionListModel

package model;

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

import javax.enterprise.context.SessionScoped;
import javax.inject.Named;

import entity.Transaction;

@Named
@SessionScoped
public class TransactionListModel implements Serializable {
    private static final long serialVersionUID = 1L;

    private List<Transaction> txList;

    @PostConstruct
    public void init() {
        txList = new ArrayList<Transaction>();
    }

    public List<Transaction> getTxList() {
        return txList;
    }

    public void clearList(){
        txList = new ArrayList<Transaction>();
    }
}

我的交易视图

<!-- Fill the table before rendering -->
<f:event type="preRenderView" listener="# {transactionListController.findAllTx()}" />

<h:form id="alltxform">
    <p:dataTable id="tableAllTransactions" var="transaction"
                value="#{transactionListModel.txList}">
        <f:facet name="header">Transactions</f:facet>
            <p:column headerText="Id">
            <h:outputText value="#{transaction.id}" />
        </p:column>
    </p:dataTable>
</h:form> 

我的TransactionList控制器

@Named
@RequestScoped
public class TransactionListController implements Serializable {

    private static final long serialVersionUID = 1L;

    @Inject
    private Logger log;

    @Inject
    private TransactionService txService;

    @Inject
    private TransactionListModel txListModel;

    public void findAllTx() {
        txListModel.clearList();
        txListModel.getTxList().addAll(txService.findAllTx());
    }

    public void reset() {
        if (txListModel.getTxList() != null) {
            txListModel.clearList();
        }
    }
}

创建页面

有一个带有按钮的简单textInputField(绑定到模型):

There is a simple textInputField (bound to a model) with a button:

<h:commandButton id="sendtx" value="Send" styleClass="ui-priority-primary">
    <f:actionListener binding="#{transactionXmlController.sendTx()}" />
</h:commandButton>

被调用的方法:

public void sendTx() {

        FacesMessage message;
        if (!transactionService.sendTx(transactionXmlEditableModel.getXml()).equals("OK"))
            message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "KO");
        else {
            message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Success", "OK");

        }    
        fc.addMessage(null, message);
    }

到目前为止,这可行.

我的问题

  • 如何使用AJAX重新加载dataTable?

  • How do I reload the dataTable with AJAX ?

我希望dataTable更新,即使在创建新事务之前已在另一个选项卡中打开了dataTable也不例外.该怎么做?

I would like the dataTable to update even if it has been opened in another tab before the creation of a new transaction. How to do this ?

如何在呈现页面之前用viewAction替换我的"f:event type ="preRenderView"以填充dataTable?

How to replace my "f:event type="preRenderView" with a viewAction in order to fill the dataTable before rendering the page ?

推荐答案

我不确定如何在没有<p:poll>之类的其他选项卡中更新表,但是要使用Ajax更新表,您可以执行以下操作:

I'm not sure how you would update the table in another tab, without something like <p:poll> but to update your table with Ajax you can do something like:

<h:commandButton id="sendtx" value="Send" styleClass="ui-priority-primary">
    <f:ajax listener = '#{transactionXmlController.sendTx()}' render="alltxform:tableAllTransactions" />
</h:commandButton>

这篇关于JSF Primefaces:更新数据表是否在另一个选项卡中打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆