Get Selected DataTable Row

0

I have a dataTable with the data in front of each given a button, when I click the button a dialog is opened so that I can do an edit in that data, however I can not get the object that was selected to pass as pro method parameter that does the editing.

How can I get the% of selected% of the table by clicking the edit button?

I'm doing it this way:

<p:dialog header="Pausar Atividade" widgetVar="dlg3"
                modal="true" height="300" width="700">
                <p:panelGrid columns="2" cellpadding="4" style="width:100%;margin-top:20px" columnClasses="rotulo, campo">      
                    <p:outputLabel value="Motivo" />
                    <h:panelGroup>
                        <p:inputTextarea rows="5" cols="30" counter="display1"
                            maxlength="255" counterTemplate="{0} Caracteres Restantes."
                            autoResize="false" style="width:70%" value="#{tarefaBean.tarefa.comentario}" />
                        <br />
                        <h:outputText id="display1" />
                    </h:panelGroup>
                </p:panelGrid>
                <p:commandButton value="Pausar Tarefa" style="margin-left:25%;margin-top:20px;width:50%" actionListener="#{tarefaBean.pausarTarefa}" update=":msgGlobal: frmPrin"/>
</p:dialog>

The button in the dataTable:

<p:column headerText="Ações" style="text-align:center">
    <p:commandButton icon="ui-icon-check" title="Finalizar"  onclick="PF('dlg4').show();" />
    <p:commandButton icon="ui-icon-pause" title="Pausar" onclick="PF('dlg3').show();" />
</p:column>

Method in bean:

public void pausarTarefa(){
    try {
        tarefa.setStatus("Pausado");
        tarefa.setDataFim(new Date());

        TarefaDAO tarefaDAO = new TarefaDAO();
        tarefaDAO.editar(tarefa);   
        FacesUtil.adicionarMsgInfo("Solicitação Enviada com Sucesso");

    } catch (RuntimeException e) {
        FacesUtil.adicionarMsgErro("Erro ao Enviar Solicitação!");
        e.printStackTrace();
    }
}
    
asked by anonymous 14.07.2015 / 16:15

2 answers

1

A better programming practice would be to leave the processing in the Bean part, which is responsible for controlling the attributes on the front end. Always remember this. It is important to note that JSF / JSP pages should have as few rules as possible to make code maintenance easier.

Let's get the solution:

On your screen, use the action property on your commandButton, and change the onclick attribute to oncomplete :

    <p:column headerText="Ações" style="text-align:center">
        <p:commandButton icon="ui-icon-check" title="Finalizar" action="#{tarefaBean.setarAtributo(tarefa)}" oncomplete="PF('dlg4').show();" />
        <p:commandButton icon="ui-icon-pause" title="Pausar" action="#{tarefaBean.setarAtributo(tarefa)}" oncomplete="PF('dlg3').show();" />
    </p:column>

In your bean add:

public synchronized void setarAtributo(Tarefa tarefa) {
    this.setTarefa(tarefa);
}


Some important remarks:

  • Use actionListener only when your bean expects an event from JSF. For business rules call and attribute passing try to use action , as in our case, using MethodExpression and EL 2.2.
  • The attribute I'm passing in the action, task , should be the same as the one used for iteration, ie the same as the var attribute of your dataTable. li>
  • The attribute was changed from onclick to oncomplete because this causes the component to execute all its bean processing, referenced in the action, and only after it has successfully processed , it will call the js code in oncomplete to open the dialog.
  • 17.07.2015 / 21:54
    0

    First: I created a selected object in your Bean.

    Second:

    <p:column >
             <p:commandButton oncomplete="PF('dlg3').show();" icon="ui-icon-search" title="View">
                <f:setPropertyActionListener value="#{objeto_que_esta_no_var_datatable}" target="#{SEUBEAN.objetoSelecionado}" />
            </p:commandButton>
    </p:column>
    

    Third: Change your pausarTarefa() method to use the selected object

    This is the first example of Selection in the primefaces showcase, I recommend taking a look to see which is best for you, but this option will solve your problem. Follow the link .

        
    14.07.2015 / 16:45