OmniFaces Ajax实用程序的updateColumn()不更新p:dataTable

时间:2012-10-29 19:27:10

标签: ajax jsf primefaces omnifaces

最近,关于OmniFaces Ajax.updateColumn()的问题在以下内容中得到了解答:

How to use OmniFaces Ajax.updateColumn() or Ajax.updateRow() via p:ajax

在这种情况下,dataTable是JSF标准h:dataTable,效果很好。

现在,在这种情况下,当enduser通过p:dataTable之外的p:calendar组件选择日期时,我正在尝试更新PrimeFaces p:dataTable。 p:dataTable xhtml如下:

<p:dataTable id="flightDataTable" var="flight" 
             value="#{pf_flightController.flightsForOrder}">

    <p:column headerText="Airport" style="text-align: center !important;">
        <p:inputText value="#{flight.airportTx}" maxlength="25" size="10" label="Airport"/>
    </p:column>

    <p:column headerText="Airline" style="text-align: center !important;">
        <p:inputText value="#{flight.airlineTx}" maxlength="25" size="10" label="Airline"/>
    </p:column>

    <p:column headerText="Flight Number" style="text-align: center !important;">
        <p:inputText value="#{flight.flightNumber}" maxlength="8" size="10" label="Flight Number"/>
    </p:column>

    <p:column headerText="Type" style="text-align: center !important;">
        <h:selectOneMenu value="#{flight.flightType}" label="Flight Type">
            <f:selectItem itemValue="#{bundle.DropdownOptionValueArrive}" itemLabel="#{bundle.DropdownOptionLabelArrive}"/>
            <f:selectItem itemValue="#{bundle.DropdownOptionValueDepart}" itemLabel="#{bundle.DropdownOptionLabelDepart}"/>
        </h:selectOneMenu>
    </p:column>

    <p:column headerText="Date" style="text-align: center !important;">
        <h:panelGroup rendered="#{pf_usersController.loggedInViaAndroid == 'N'}">
            <p:calendar value="#{flight.flightDate}" navigator="true"
                        label="Flight Date"
                        pattern="MM/dd/yyyy" size="10">
                <f:convertDateTime pattern="MM/dd/yyyy" />
            </p:calendar>
        </h:panelGroup>
        <h:panelGroup rendered="#{pf_usersController.loggedInViaAndroid == 'Y'}">
            <p:inputText value="#{flight.flightDate}"
                         label="Flight Date" type="date">
                <f:convertDateTime pattern="yyyy-MM-dd" />
            </p:inputText>
        </h:panelGroup>
    </p:column>

    <p:column headerText="Time" style="text-align: center !important;">
        <h:panelGroup rendered="#{pf_usersController.loggedInViaAndroid == 'N'}">
            <pe:timePicker value="#{flight.flightTime}"
                           label="Flight Time" mode="popup"
                           showPeriod="true" style="width: 80px;">
                <f:convertDateTime pattern="hh:mm a" />
            </pe:timePicker>
        </h:panelGroup>
        <h:panelGroup rendered="#{pf_usersController.loggedInViaAndroid == 'Y'}">
            <p:inputText value="#{flight.flightTime}"
                         label="Flight Time" type="time">
                <f:convertDateTime pattern="HH:mm" />
            </p:inputText>
        </h:panelGroup>
    </p:column>

</p:dataTable>

最初和目前正在制作中,p:calendar p:ajax update =“...”更新'整个'航班p:dataTable,因此p:dataTable中的日期列已成功更新。我知道,我知道,如果没有破坏,那么不要修复它,但今天我正在修改这个代码和xhtml,所以我想使用OmniFaces Ajax实用程序的updateColumn()来更新所有的“一个”列这个PrimeFaces的行p:dataTable。

以下是最终用户通过p:calendar选择日期时执行的代码片段;这是调用OmniFaces Ajax实用程序的updateColumn()的代码。

private void updateFlightDateOnAdd() {
    flightController.updateFlightDateOnAdd(current.getTripDateTime());

    UIData flightsDataTable = null;
    try {
        flightsDataTable = (UIData) Components.findComponent(":orderAddForm:flightDataTable");
    } catch (Exception e) {
        System.out.println("pf_OrdersController.updateFlightDateOnAdd(): caught exception on Components.findComponent(...)");
    }
    if (flightsDataTable != null) {
        System.out.println("pf_OrdersController.updateFlightDateOnAdd(): Components.findComponent(...) found " + flightsDataTable.getClientId());
        Ajax.updateColumn(flightsDataTable, 4);
    }
    else {
        System.out.println("pf_OrdersController.updateFlightDateOnAdd(): Components.findComponent(...) returned null");
    }
}

以下是POJO定义:

public class FlightForOrder {
    private Integer flightId;
    private String airportTx, airlineTx, flightNumber;
    private Character flightType;
    private Date flightDate, flightTime;

    public FlightForOrder() {

    }

    public Integer getFlightId() {
        return flightId;
    }

    public void setFlightId(Integer flightId) {
        this.flightId = flightId;
    }

    public String getAirportTx() {
        return airportTx;
    }

    public void setAirportTx(String airportTx) {
        this.airportTx = airportTx;
    }

    public String getAirlineTx() {
        return airlineTx;
    }

    public void setAirlineTx(String airlineTx) {
        this.airlineTx = airlineTx;
    }

    public Date getFlightDate() {
        return flightDate;
    }

    public void setFlightDate(Date flightDate) {
        this.flightDate = flightDate;
    }

    public String getFlightNumber() {
        return flightNumber;
    }

    public void setFlightNumber(String flightNumber) {
        this.flightNumber = flightNumber;
    }

    public Date getFlightTime() {
        return flightTime;
    }

    public void setFlightTime(Date flightTime) {
        this.flightTime = flightTime;
    }

    public Character getFlightType() {
        return flightType;
    }

    public void setFlightType(Character flightType) {
        this.flightType = flightType;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 97 * hash + Objects.hashCode(this.flightId);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final FlightForOrder other = (FlightForOrder) obj;
        if (!Objects.equals(this.flightId, other.flightId)) {
            return false;
        }
        return true;
    }

}

测试时,服务器日志中显示以下信息:

INFO: pf_OrdersController.updateFlightDateOnAdd(): Components.findComponent(...) found orderAddForm:flightDataTable

所以,这意味着我获得了PrimeFaces p:dataTable的一个很好的引用,所以我将UIData类型的组件传递给了Ajax.updateColumn()。

最终结果是,OmniFaces Ajax实用程序的updateColumn()没有更新PrimeFaces dataTable。

我刚刚注释掉了Ajax.updateColumn(...)并将update =“...”重新添加到p:calendar p:ajax,并成功更新了PrimeFaces dataTable。

请告知我可能需要做些什么来确保通过Ajax.updateColumn(...)更新PrimeFaces dataTable。感谢。

1 个答案:

答案 0 :(得分:1)

更换

<p:column headerText="Date" style="text-align: center !important;">
    <h:panelGroup rendered="#{pf_usersController.loggedInViaAndroid == 'N'}">
        <p:calendar value="#{flight.flightDate}" navigator="true"
                    label="Flight Date"
                    pattern="MM/dd/yyyy" size="10">
            <f:convertDateTime pattern="MM/dd/yyyy" />
        </p:calendar>
    </h:panelGroup>
    <h:panelGroup rendered="#{pf_usersController.loggedInViaAndroid == 'Y'}">
        <p:inputText value="#{flight.flightDate}"
                     label="Flight Date" type="date">
            <f:convertDateTime pattern="yyyy-MM-dd" />
        </p:inputText>
    </h:panelGroup>
</p:column>

通过

<p:column headerText="Date" style="text-align: center !important;">
    <p:calendar value="#{flight.flightDate}" navigator="true"
                label="Flight Date"
                pattern="MM/dd/yyyy" size="10"
                rendered="#{pf_usersController.loggedInViaAndroid == 'N'}">
        <f:convertDateTime pattern="MM/dd/yyyy" />
    </p:calendar>
    <p:inputText value="#{flight.flightDate}"
                 label="Flight Date" type="date"
                 rendered="#{pf_usersController.loggedInViaAndroid == 'Y'}">
        <f:convertDateTime pattern="yyyy-MM-dd" />
    </p:inputText>
</p:column>

应该修复它。

说明:Ajax#updateColumn()仅将直接子项的客户端ID添加到PartialViewContext#getRenderIds()。但是,<h:panelGroup>没有任何属性应该在结果HTML输出中结束,例如id,不会在HTML输出中呈现任何内容。因此,JS / Ajax无法找到<h:panelGroup>的HTML表示以便更新它。

修复使表格单元格的具体内容成为直接子代,这样JS / Ajax就能找到并替换它们。另一种方法是给这些小组编写一个具体的id,但这在这个结构中是不必要的。