Pick selected line from datatable

Asked

Viewed 2,083 times

0

I have a dataTable with the data in front of each given one button, when I click one button dialog is opened so that I can edit this data, but I cannot take the object that was selected to pass as parameter pro method that makes the editing.

How do I get the linha(objeto) selected from 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>

Bean method:

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();
    }
}

2 answers

1


A best practice of programming would be to leave the processing on the Bean, who is responsible for controlling the attributes on the front end. Always remember this. It is important to note that the JSF/JSP pages must have as few rules as possible to facilitate the maintenance of the code.

Let’s solve:

On your screen, use the property action in your commandButton, and change the attribute onclick for 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:

  1. Use actionListener only when your bean expects one Event JSF. To call business rules and pass attributes please use action, as in our case, using Methodexpression and valid EL 2.2.
  2. The attribute I’m passing in the action, task, should be the same used for iteration, ie the same used in the attribute var of your datatable.
  3. The attribute has been changed from onclick for oncomplete, because this causes the component to perform all the processing of its bean, referenced in the action, and only after successfully processed, will it call the js code in the oncomplete to open the dialog.

0

First: I created an objectSelected in your Bean.

According to:

<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 method pausarTarefa()to use the objectSelected

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. Follows the link.

  • When I put pausarTarefa(Tarefa tarefa) of error. When I leave without works, but editing gets a little crazy, if I give an F5 it goes back to the previous state, ie with the data before editing

  • You do not need to add a parameter, it is only within your method to use the objectSelected.

  • I’m using, but it edits if I give F5 it goes back to the previous state, if I give another F5 it goes back to the edited and so goes.

  • 1

    your bean this @Viewscoped?

  • Yes, I have to change it?

  • Then the problem should be in the method that fills the datatable list. It should not be updating the list or datatable correctly.

  • I made a Sysout To print the list in the DAO and each F5 it brings a different list, updated and old. How is this possible if in the database is only the updated data?

  • Well then your problem is in your method or with you are calling it in datatable, I saw you asked another question I will answer by there, if this question solved your problem mark it as correct.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.