代码之家  ›  专栏  ›  技术社区  ›  stakahop

p:datatable未正确更新行

  •  0
  • stakahop  · 技术社区  · 6 年前

    <h:panelGroup layout="block" styleClass="ui-g-12 refinance" id="cotPanel">
    
        <p:dataTable id="cot" value="#{cot.cods}" var="offer" reflow="true">
                <p:column width="18%" headerText="OFFER">
                        <p:outputLabel value="#{offer.name}" />
                </p:column>
                <p:column headerText="AAA" width="18%" styleClass="num">
                        <p:outputLabel value="#{of:formatNumberDefaultForLocale(offer.aaa, 'nl_NL')}" />
                </p:column>
                <p:column headerText="BBB" width="18%">
                        <p:outputLabel value="#{offer.bbb}" />
                </p:column>
                <p:column headerText="CCC" width="18%" styleClass="num">
                        <p:outputLabel value="#{of:formatNumberDefaultForLocale(offer.ccc, 'nl_NL')}" />
                </p:column>
                <p:column headerText="DDD" width="18%" styleClass="num">
                        <p:outputLabel value="#{offer.ddd}" />
                            </p:column>
                <p:column headerText="Select" width="10%">
                        <h:panelGroup styleClass="Width100 Flex">
                                <p:inputSwitch value="#{offer.selected}">
                                        <p:ajax event="change" listener="#{cot.onSelectCod(offer)}" process="@this"
                                            update="mainForm:cotPanel" />
                                </p:inputSwitch>
                        </h:panelGroup>
                </p:column>
                <p:column headerText="SELECTED" width="10%">
                        <p:outputLabel value="#{offer.selected}"/>
                </p:column>
        </p:dataTable>
    </h:panelGroup>
    

    public void onSelectCod(Cod entry) {
        if (entry.isSelected()) {
            for (Cod codTemp : cods) {
                if (!entry.equals(codTemp)) {
                    codTemp.setSelected(false);
                }
            }
        }else {
            // i dont let it become unselected if click on selected row, it becomes unselected only if it is clicked on another entry
            entry.setSelected(true);
        }
    }
    

    datatable中的最后一列显示所有值都是正确的,输入开关在右侧,但它是红色的。我还尝试编写一个样式类,当selected为true时为“绿色”,否则为“红色”,我在DOM中看到所有其他行都得到了正确的类,但selected行没有。

    enter image description here

    1 回复  |  直到 6 年前
        1
  •  1
  •   Aritz    6 年前

    相反,您可以使用单击事件而不是更改:

    <p:inputSwitch value="#{offer.selected}" styleClass="#{offer.selected ? 'green' : 'red'}">
            <p:ajax event="click" process="@this" listener="#{cot.onSelectCod(offer)" 
                update="mainForm:cotPanel" />
    </p:inputSwitch>
    

    此外,您还需要稍微重写侦听器方法,因为现在应该只使用您感兴趣的对象调用它:

    public void onSelectCod(Cod entry) {
        // The entry here is now selected (you set it in the inputSwitch value)
        // So just mark the rest of the entries as unselected
        for (Cod codTemp : cods) {
            if (!entry.equals(codTemp)) {
                codTemp.setSelected(false);
            }
        }
    }