How to pass data from a row from DataTable to a Dialog in PrimeFaces

0

I have a DataTable in which I have multiple data and a select button. When I click on the select button I call a Dialog with a few more fields. I would like to click on the select button to get the object data and fill in the fields of this Dialog.

This is the method that generates 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>

Can anyone help?

    
asked by anonymous 03.06.2015 / 16:25

1 answer

2

I realize that you are using the Dialog Framework component of PrimeFaces. The third parameter of the openDialog(pagina, opcoes, params) method is to pass the data (parameters) to the dialog, the bad of this method parameter is that only HashMap of List<String> is accepted.

Therefore, one approach is to pass the item code to the openDialog() method and the ManagedBean of the dialog page to retrieve this code that is as a parameter, and after that loads the remaining data and displays them on the dialog page.

For example:

Pass the selected object to 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>

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

Get the parameter passed to the dialog

It may be in the ManagedBean constructor that controls the open page 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);
    }
    
03.06.2015 / 20:08