How to pass data from a Datatable line to a Dialog in Primefaces

Asked

Viewed 1,886 times

0

I have a Datatable in which I have several data and a select button. When I click the select button I call a Dialog with a few more fields. I would like to click on the button select it to take the data of the Object and fill in the fields of this Dialog.

This is the method that generates the Dialog:

@ManagedBean(name="dtBasicView")
@ViewScoped
public class ListaProcedimentosBean {
    public void abrirDialogo() {
            Map<String, Object> opcoes = new HashMap<>();
            opcoes.put("modal", true);
            opcoes.put("resizable", false);
            opcoes.put("contentHeight", 670);
            opcoes.put("contentWidth", 870);
            RequestContext.getCurrentInstance().openDialog("procedimentosPesquisaCompleta", opcoes, null);
        }
}

And here is the select button:

<p:column headerText="Opções">
                <p:commandButton value="Selecionar" icon="ui-icon-search" action="#{dtBasicView.abrirDialogo}" process="@this">
                </p:commandButton>

Anyone can help?

  • Good, for that the best is to follow the example of the showcase of the first faces http://www.primefaces.org/showcase/ui/data/datatable/selection.xhtml .

1 answer

2


I understand you’re using the component Dialog Framework of the Primefaces. The third parameter of the method openDialog(pagina, opcoes, params) serves to be passed the data (parameters) to the dialog, the bad of this method parameter is that only one is accepted HashMap of List<String>.

Therefore, one approach that can be taken is to pass the item code to the method openDialog() and in the ManagedBean from the dialog page retrieve that code that is as parameter, and after that load the remaining data and display them on the dialog page.

For example:

Pass the selected object to the ManagedBean

<p:column headerText="Opções">
    <p:commandButton value="Selecionar" icon="ui-icon-search" 
                     action="#{dtBasicView.abrirDialogo}" process="@this">
        <f:setPropertyActionListener target="#{dtBasicView.objetoSelecionado}" value="#{objeto}" />
    </p:commandButton>
</p:column>

Grab the selected object and open the dialog

public void abrirDialogo() {
  Map<String, Object> optionsDialog = new HashMap<>();
  optionsDialog.put("modal", true);
  optionsDialog.put("resizable", false);
  optionsDialog.put("contentHeight", 670);
  optionsDialog.put("contentWidth", 870);

  Map<String, List<String>> params = new HashMap<>();
  params.put("meuParametro", Arrays.asList(""+objetoSelecionado.getCodigo()));           

  RequestContext.getCurrentInstance()
      .openDialog("procedimentosPesquisaCompleta", optionsDialog, params);
}

Take the parameter passed to the dialog

It can be in the builder of ManagedBean that controls the open tab in the dialog.

String paramResposta = ((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext()
    .getRequest()).getParameter("meuParametro");

    if(paramResposta != null && !paramResposta.isEmpty()){
        int codigo = Integer.parseInt(paramResposta);
        Objeto = dao.buscarPorCodigo(codigo);
    }
  • Yeah, it can be done, too. As you just put it would be even simpler, because all the remaining data could be obtained from the database from that code received as parameter. That is, without the need to carry out the parse for JSON and vice versa.

  • I will edit the answer to this situation.

Browser other questions tagged

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