【问题标题】:Can't get inputtext value on ajax after they change更改后无法在 ajax 上获取 inputtext 值
【发布时间】:2016-09-27 12:30:48
【问题描述】:

我正在尝试从在数据表列中键入的 inputtext 中获取一个值,并使用该 inputtext 值和其他 inputtext 值的乘积来更新来自其他列的 outputtext。我的问题是我无法在调用 ajax 时获取键入的值,因为我从接收值 (gasto.valor) 的字段中获取先前的值,而不是当前键入的值。这是我的代码:

XHTML

<p:dataTable binding="#{cadastroContasBean.dataTable}" var="gasto"
             value="#{cadastroContasBean.listaGasto}" scrollable="true"
             scrollHeight="150" editable="true" editMode="cell" id="Tabela"
             widgetVar="wTabela">
    <p:column headerText="Nome">
        <p:cellEditor>
            <f:facet name="output">
                <h:outputText value="#{gasto.nome}" />
            </f:facet>
            <f:facet name="input">
                <p:inputText value="#{gasto.nome}" style="width:96%" />
            </f:facet>
        </p:cellEditor>
    </p:column>
    <p:column headerText="Quantidade">
        <p:cellEditor>
            <f:facet name="output">
                <h:outputText value="#{gasto.quantidade}" />
            </f:facet>
            <f:facet name="input">
                <p:inputText id="inputQtd" value="#{gasto.quantidade}"
                             style="width:96%" onkeydown="MascaraNumero()"
                             onkeyup="MascaraNumero()">
                    <p:ajax  event="blur" render="outputVT" listener="#{cadastroContasBean.atualizaVT}"  execute="inputQtd" process="@this" immediate="true"/>
                </p:inputText>
            </f:facet>
        </p:cellEditor>
    </p:column>
    <p:column headerText="Valor">
        <p:cellEditor>
            <f:facet name="output">
                <h:outputText value="#{gasto.valor}">
                    <f:convertNumber currencySymbol="R$" type="currency" />

                </h:outputText>
            </f:facet>
            <f:facet name="input">
                <p:inputText id="inputValor" value="#{gasto.valor}"
                             style="width:96%" onkeyup="mascara(this, valor)"
                             onkeydown="mascara(this, valor)">
                    <f:convertNumber pattern="#,##0.00"></f:convertNumber>
                    <f:validator validatorId="MoedaValidator" />
                    <p:ajax  event="blur" render="outputVT" listener="#{cadastroContasBean.atualizaVT}"  execute="inputValor" process="@this" immediate="true" >
                    </p:ajax>
                </p:inputText>
            </f:facet>
        </p:cellEditor>
    </p:column>
    <p:column headerText="Valor Total" id="vt">
        <h:outputText id="outputVT" value="#{gasto.valorTotal}">
            <f:convertNumber currencySymbol="R$" type="currency" />
        </h:outputText>
    </p:column>
</p:dataTable>

MB

public void atualizaVT(AjaxBehaviorEvent event) {

    Gastos gastoDataTableAtual = ((Gastos)dataTable.getRowData());

    if(!event.getComponent().getId().equals("inputQtd")){
        BigDecimal valorAtual = (BigDecimal)((UIOutput)event.getSource()).getValue();
        gastoDataTableAtual.setValorTotal(valorAtual.multiply(new BigDecimal(((Gastos)dataTable.getRowData()).getQuantidade())));
        Iterator<Gastos> gastosAsIterator = listaGasto.iterator();
        while (gastosAsIterator.hasNext()){
            Gastos it = gastosAsIterator.next();
            int id = 0;
            if(it.getId() == gastoDataTableAtual.getId()){
                //listaGasto.get(id).setValor(gastoDataTableAtual.getValor());
                listaGasto.get(id).setValorTotal(gastoDataTableAtual.getValorTotal());
                ((Gastos)dataTable.getRowData()).setValorTotal(gastoDataTableAtual.getValorTotal());
                break;
            }
            id++;
        }
    }else{
        Integer valorAtual = (Integer)((UIOutput)event.getSource()).getValue();
        gastoDataTableAtual.setValorTotal(((Gastos)dataTable.getRowData()).getValor().multiply(new BigDecimal(valorAtual)));
        Iterator<Gastos> gastosAsIterator = listaGasto.iterator();
        int id = 0;
        while (gastosAsIterator.hasNext()){
            Gastos it = gastosAsIterator.next();
            if(it.getId() == gastoDataTableAtual.getId()){
                //listaGasto.get(id).setQuantidade(gastoDataTableAtual.getQuantidade());
                listaGasto.get(id).setValorTotal(gastoDataTableAtual.getValorTotal());
                ((Gastos)dataTable.getRowData()).setValorTotal(gastoDataTableAtual.getValorTotal());
                break;
            }
            id++;
        }

我知道在更改“gasto.valor”之前会调用 ajax 事件“blur”,但是当我将 ajax 事件更改为“更改”时,不会调用方法“atualizaVT”。我怎样才能得到这个值?

【问题讨论】:

    标签: ajax jsf primefaces


    【解决方案1】:

    “更改”事件不是 AjaxBehaviorEvent。 尝试在没有参数的情况下实现 atualizaVT 这样:

    public void atualizaVT(){

    这应该可行。

    【讨论】:

    • 感谢您的回答,但没有奏效。当我编辑 inputtext 时,只调用验证器。反正我需要得到inputtext内容,不带参数不知道怎么弄的。
    • 我们在datatable selection="#{actionBean.selectedData}"中使用了selection参数,这样当你选择一行,发送ajax请求时,selectedData就会被填充为当前值。跨度>
    • 它起作用了,但我用另一种方式做到了:我在 managebean 类中创建了一个带有 getter 和 setter 的 inputtext,并与 XHTML 视图中的 inputtext 绑定。然后在 ajax 上由 event="change" 调用的 "atualizaVT() 方法中使用 "inputtext.getValue()" 获取值。感谢您的帮助!-
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    • 2012-09-20
    • 1970-01-01
    • 1970-01-01
    • 2021-08-27
    相关资源
    最近更新 更多