1

I have a datatable listing a bunch of tests. I would like to show a Primefaces dialog after a user selects a row. The dialog references the data in the selected row, like "#{bean.selectedTest.name}". However, when the page is first rendered, there is no row selected and the references to the selected data yield "Target Unreachable, 'selectedTest' returned null" (the dialog references selectedTest.name).

I don't believe this is addressed in Identifying and solving javax.el.PropertyNotFoundException: Target Unreachable which is otherwise awesome.

In the Primefaces Showcase for DataTable selection this is dealt with by using a FacesMessage, which is constructed AFTER the row is selected, rather than a dialog which exists in the page before. In my case I need user interaction in a dialog.

I have tried putting the dialog in a separate form as suggested in primefaces datatable selection issue, same problem.

I have tried to render the component inside the dialog that contains the references only after selectedTest is populated (rendered="#{not empty bean.selectedTest}"), and to both process and update the dialog after row selection, and this gets rid of the error, but the re-render never happens and the dialog pops up empty. I can see in my debugger that getSelectedTest() is called before setSelectedTest() and returns null. Is there some better way to force the re-render that I am missing? Or is this the wrong way to handle the problem?

I have worked around this by setting an arbitrary test to selected but this seems an ugly hack, since the row shows as selected but user hasn't actually selected anything.

Thanks in advance for help.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
snakedog
  • 307
  • 2
  • 11
  • `I have tried...` - you're on the right track here, but you need to post your code, otherwise it's a guessing game as to why the re-render never happens. – Vsevolod Golovanov Jan 14 '18 at 20:21
  • Well I should have committed the code that used render= to omit the dialog when the referenced stuff in it is null but I didn't since it didn't work and I thought that was not the right way to handle it. If you think this is the right way to handle it I'll try again. I gave up because I'm sure I've done this before in other projects and did not have to use render=. – snakedog Jan 18 '18 at 14:54

1 Answers1

2

You were on the right track: a conditional render is the way to go here.

As to why the re-render didn't work: most likely you were trying to update the condionally rendered component itself. That's probably the most common mistake. See explanation.

An alternative is to use p:dialog dynamic="true" attribute - this is essentially a conditional render with a condition "a dialog was opened on the client".

Vsevolod Golovanov
  • 3,355
  • 3
  • 25
  • 59
  • Thank you so much! "dynamic" is exactly what I was looking for - the dialog is not rendered until the user selects a row, at which point the selected data is no longer null. This is the approach I will use. I totally missed this in the documentation. – snakedog Jan 19 '18 at 17:30