简体   繁体   English

Primefaces DataTable-rowSelectListener将CommandButton隐藏在行内

[英]Primefaces DataTable - rowSelectListener hiding CommandButton inside rows

I have a PrimeFaces DataTable, where each row has a save , delete and another commandButton . 我有一个PrimeFaces DataTable,其中每一行都有一个savedelete和另一个commandButton The dataTable has selectionMode="single" and a rowSelectListener . dataTable具有selectionMode="single"rowSelectListener When I am clicking on the command buttons, the rowSelectListener is fired, but the commandButton's action is not getting fired. 当我单击命令按钮时,将rowSelectListener ,但不会触发commandButton的动作。

What to do? 该怎么办? I need both to fire! 我都需要开除!

I cannot use immediate="true" since there are some form fields, outside the dataTable, which must be submitted. 我无法使用immediate="true"因为在dataTable之外有一些表单字段必须提交。 It works with immediate="true" added in the commandbutton. 它与在命令按钮中添加的Instant immediate="true"一起使用。

Primafaces version: 2.2.1 JSF version: 2.0.3 Primafaces版本:2.2.1 JSF版本:2.0.3

Thank you in advance for any leads! 预先感谢您提供任何线索!

Code: 码:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
            xmlns:ui="http://java.sun.com/jsf/facelets"
            xmlns:h="http://java.sun.com/jsf/html"
            xmlns:f="http://java.sun.com/jsf/core"
            xmlns:v="http://java.sun.com/jsf/composite/composite"
            xmlns:p="http://primefaces.prime.com.tr/ui">
    <h:form id="contractReasonAddFormId">
            <span class="error">
                <h:messages /> 
            </span>
    <h:panelGrid columns="2" cellspacing="5" >
        <h:outputLabel class="label" value="#{resourceBundle.coAddReason_coStatus_label_Key}" />
        <h:outputText class="output" value="#{selectedContractService != null ? selectedContractService.COS_STATUS_DESC : contract.CO_STATUS_DESC}" />
        <h:outputLabel class="label" for="reasonId" value="#{resourceBundle.contractChangeStatus_NewReason_Key}:" />
        <h:selectOneMenu id="reasonId" value="#{bbNewStatusReason.selectedString}" required="true" requiredMessage="#{resourceBundle.contractChangeStatus_NewReason_Key} #{resourceBundle.valueIsRequired}">
            <f:selectItem itemValue="" itemLabel="- #{resourceBundle.contractChangeStatus_SelectReason_Key} -" />
            <f:selectItems value="#{bbNewStatusReason.list}" var="r" itemValue="#{r.REASON}" itemLabel="#{r.RS_DES}" />
        </h:selectOneMenu>
    </h:panelGrid>
    <!-- some other form fields --->
    <h:panelGrid>
        <p:dataTable 
        id="currentStatusReasonTable" 
        value="${bbCurrentStatusReason.list}" 
        var="currStReason"
        selectionMode="single"
        selection="#{bbCurrentStatusReason.selectedItem}"
        rowSelectListener="#{contractAddReasonAction.selectReason}" onRowSelectUpdate="reasonValidityTimes"         
        >
            <p:column sortBy="#{currStReason.RS_DES}" headerText="#{resourceBundle.coAddReason_currStReason_label_Key}">
                <h:outputText value="#{currStReason.RS_DES}: "/>
            </p:column>
            <!-- Other columns -->
            <p:column headerText="#{resourceBundle.coAddReason_action_facet_Key}">
                <p:commandButton id="updateReasonId" action="dlgUpdate" image="ui-icon ui-icon-pencil" title='#{resourceBundle["button.save"]}' disabled="#{selectedContractService != null ? currStReason.LEVEL == 'C' : false}" >
                    <f:setPropertyActionListener target="#{bbCurrentStatusReason.selectedItem}" value="#{currStReason}" />
                </p:commandButton>
                <p:commandButton id="deleteReasonId" action="dlgDelete" image="ui-icon ui-icon-circle-minus" title='#{resourceBundle["button.delete"]}' disabled="#{selectedContractService != null ? currStReason.LEVEL == 'C' : false}" >
                    <f:setPropertyActionListener target="#{bbCurrentStatusReason.selectedItem}" value="#{currStReason}" />
                </p:commandButton>
                <p:commandButton id="generateDeleteDocumentsId" action="generateDeleteDocuments" image="ui-icon" title='#{resourceBundle["button.generateDocumentsForDel"]}' immediate="true" rendered="#{showPrintDocument != null}" >
                    <f:setPropertyActionListener target="#{bbCurrentStatusReason.selectedItem}" value="#{currStReason}" />
                </p:commandButton>
            </p:column>

        </p:dataTable>

    </h:panelGrid>
    </h:form> 
</ui:composition>

I was having this same problem. 我遇到了同样的问题。 What I ended up using on the commandButton was event.stopPropagation() within the onclick attribute. 我最终在commandButton上使用的是onclick属性中的event.stopPropagation()。 Here is my delete button. 这是我的删除按钮。

<p:commandButton icon="ui-icon-trash"
                             onclick="connDelConfirm.show(); event.stopPropagation();" 
                             actionListener="#{connectionController.setSelectedConnection(connection)}"
                             disabled="#{connection.deleted}"/>

The other action on the onclick attribute opens a confirm dialog for the delete action. onclick属性上的其他操作将为Delete操作打开一个确认对话框。 Since the propagation of the event was stopped. 由于事件的传播已停止。 I needed a way to still set the selected item to be deleted. 我需要一种方法仍将所选项目设置为删除。 This was accomplished through the action listener. 这是通过动作侦听器完成的。 I simply called the setSelectedItem method in the bean and passed it the 'var' attribute from the dataTable. 我只是简单地在bean中调用setSelectedItem方法,并将其从dataTable中传递给'var'属性。

Use component binding for input component with immediate="true" . 组件绑定用于具有immediate="true"输入组件。

You have to play between input component and action component using immediate="true" . 您必须使用immediate="true"输入组件动作组件之间播放。

You need to reference JSF Lifecycle effect of immediate="true" ; 您需要参考immediate="true" JSF生命周期效果;

BeanOne.java BeanOne.java

@ManagedBean(name="BeanOne")
@RequestScoped
public class BeanOne {
    private HtmlInputText inputText;

    public HtmlInputText getInputText() {
        return inputText;
    }

    public void setInputText(HtmlInputText inputText) {
        this.inputText = inputText;
    }

    public void show() {
        System.out.println("HtmlInputText ==>" + inputText.getValue());
    }
}

pageOne.xthml pageOne.xthml

<h:inputText binding="#{BeanOne.inputText}" immediate="true"/><br/>
<h:commandButton value="Show_1" action="#{BeanOne.show}" immediate="true"/>
<h:commandButton value="Show_2" action="#{BeanOne.show}"/>

Thank you for the answers. 谢谢你的回答。 However, the root cause of the issue I was facing turned out to be something else completely. 但是,我所面临的问题的根本原因却完全是另外一些原因。

What was happening was that the action was getting triggered all right, but there was a jsf element with 'required=true' - and this was throwing a validation error which was not showing up anywhere - not even in console. 发生的事情是动作已被正确触发,但是存在一个带有'required = true'的jsf元素-并抛出了一个验证错误,该错误未在任何地方显示-甚至不在控制台中。 After updating (in the primaface command button, ajax - using 'update=' attribute) the error message displaying pane of the page layout, the error message was seen. 更新(在primaface命令按钮中,ajax-使用'update ='属性)错误消息显示页面布局的窗格后,看到了错误消息。

Thanking you, Rajarshi Mukherjee 谢谢Rajarshi Mukherjee

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM