【问题标题】:oncomplete for p:ajax event is always called, even if the listener returns nothing [duplicate]始终调用 p:ajax 事件的 oncomplete,即使侦听器没有返回任何内容[重复]
【发布时间】:2020-06-10 01:06:00
【问题描述】:

我有一个包含不同行的数据表,当我选择特定行时,我不想在特定行上执行任何 onClick 事件。我想防止在 oncomplete 中调用 show() 方法。我尝试了不同的方法,但我无法阻止它。

这是我下面给出的代码。

<h:form id="availableBooksForm">
        <p:messages />
        <p:panel>
            <p:remoteCommand name="show" action="#{boAction.selectPromotion}" />

            <p:dataTable id="BooksTable" value="#{userProfile.availableBooks}"
                var="res" selectionMode="single" rowKey="#{res.promotionId}"
                rows="#{referenceData.recordsPerPage}" lazy="true"
                resizableColumns="true" paginator="true"
                paginatorTemplate="#{referenceData.paginatorTemplate}"
                paginatorPosition="bottom" paginatorAlwaysVisible="false"
                style="margin-top: 5px; text-overflow: ellipsis;">

                <p:ajax event="rowSelect" listener="#{boAction.onSelectPromotion}"
                    oncomplete="show()" />

                <p:column headerText="#{msg.promotionInfo}">
                    <p:panelGrid columns="2" columnClasses="pct50,"
                        style="font-size: 12px">

                        <h:outputText value="#{msg.promoCode}" />
                        <h:outputText value="#{res.promoCode}">
                            <f:convertNumber pattern="#{userProfile.displayStringFormat}" />
                        </h:outputText>

                        <h:outputText value="#{msg.promoName}" />
                        <h:outputText value="#{res.promoName}">
                            <f:convertNumber pattern="#{userProfile.displayStringFormat}" />
                        </h:outputText>
                    </p:panelGrid>
                </p:column>
            </p:dataTable>
        </p:panel>

这里我调用 onSelectPromotion 方法不返回任何内容,如下所示:

public void onSelectPromotion(SelectEvent event) {

    String selectedType = ((AllBooks) event.getObject()).getPromoType();
    if(selectedType.equals(BookType.getBookType)) {
        return;
    }
}

在此之后 show() 方法正在调用 boAction.selectPromotion 操作。我不想调用这个方法。如何通过从数据表中选择特定行来防止这种情况发生。

如果我从表中选择行,我不想执行此方法。

public String selectPromotion() {
    //some code is executing here
}

【问题讨论】:

  • 也许可以尝试通过像&lt;p:column selectionMode="single" /&gt;这样的列中的单选按钮进行选择
  • @fuggerjaki61 该表已经存在,当我们在每一行上进行选择时,它会导航到另一个页面,但是对于某些行并不想根据其中的内容导航到其他页面。所以我试图不在某些选定的行上执行 onselect 事件。

标签: jsf events primefaces listener onselect


【解决方案1】:

在您的 Ajax 侦听器中使用 return 不会阻止调用 oncomplete,它只会停止执行该方法。基本上你的听众是没用的。见:

但是你可以用不同的方式解决你的问题。在您的p:dataTable 中,使用selection 属性来保留对当前选定行的引用。例如selection="#{userProfile.selectedBook}"(将selectedBook 添加到您的bean 并确保有getter 和setter)。然后在selectPromotion 方法中,您可以简单地从您的bean 中获取selectedBook 并决定是否需要结束或继续处理剩余的代码。

您可以在橱窗中see the selection attribute in action

【讨论】:

  • 如果我想在selectPromotion 中结束进程,任何要以不同方式实现的东西。需要一些想法。
  • 最近没有尝试更改 ir,但我在使用 selection 属性并在 ajax 侦听器中访问其内容时总是遇到问题。
【解决方案2】:

我已尝试在 xhtml 文件中使用以下代码 sn-p,但我不允许选择行。

<p:dataTable id="BooksTable" value="#{userProfile.availableBooks}"
            var="res" selectionMode="single" rowKey="#{res.promotionId}"
            rows="#{referenceData.recordsPerPage}" lazy="true"
            resizableColumns="true" paginator="true"
            paginatorTemplate="#{referenceData.paginatorTemplate}"
            paginatorPosition="bottom" paginatorAlwaysVisible="false"
            style="margin-top: 5px; text-overflow: ellipsis;" 
            disabledSelection="#{(res.promoName == 'Your String')}">

通过在条件中添加 disabledSelection,我不允许使用代码选择 dataTable 中的特定行:disabledSelection="#{(res.promoName == 'Your String')}"

如果你有整数类型的值来比较,你可以使用下面的条件来检查 disabledSelection="#{(res.promoName == 123456)}"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-06
    • 2011-02-19
    • 2017-10-23
    • 2022-11-21
    相关资源
    最近更新 更多