views:

171

answers:

2

Hello, I'm starting a small project, using JSF 2.0. I'm having problems right in the start, in the CRUD of the first model implemented.

What I want to do is pretty simple:

The page has some filters to search, and using ajax, it populates a h:dataTable with the results. The user should now select the line with the result he wants to see/edit. But I just can't find a way to make the line selectable.

This is my table:

<h:dataTable var="aluno" value="#{alunoController.resultado}" >
  <h:column>
     #{aluno.id}
  </h:column>
  <h:column>
     #{aluno.nome}
  </h:column>
  <h:column>
     <!-- radiobutton, commandLink/Action goes gere -->
  </h:column>
</h:dataTable>

First I tried having a radiobutton on each line, then I learned that I cant have a radiogroup inside a table.

Then I tried having a radio group inside the first cell of each line, and handling selecion with a little bit of javascript. Somehow, the databinding doesn't work, and I cant get the selected model back in my ManagedBean.

So I tried having a commandButton/Link, which sends the model via parameter.... not. It just refresh the page.

The I tried using query parameters, sending the id of that row and getting the model from de database again, but I cant find a way to tell wich method should be called in the ManagedBean.

So I came here for my very first time, looking for suggestions. What should I do? Am I missing some information?

Tell me if you need more information.

I just don't want to believe what I want to do is too advanced.

Sorry for the bad english.

A: 

I would recommend to you to use PrimeFaces components.

Allowing you to choose single rows. See Show Case

Odelya
PrimeFaces looks amazing, I'll give a look.
Marcio Cruz
Didn't solve my problem, p:dataTable won't work for the same reason: the table contents gets lost between requests.Primefaces still rocks though.
Marcio Cruz
Do you use @ViewScope on the bean?
Odelya
No, I wasn't aware of @ViewScoped until BalusC answered this question.
Marcio Cruz
When you try it with @ViewScoped - would it help?
Odelya
It works while I'm in the same page. It's suppose to be like this, but I need the table data in another page. I can't get it because the bean is constructed again after the request. I'll use ConversationScope instead.
Marcio Cruz
A: 

The JSF 2.0 way is to put managed bean in view scope. You can do that using @ViewScoped.

@ManagedBean
@ViewScoped
public class Bean {}

This way the bean will be preserved for the subsequent request and retained when the target view of the subsequent request is the same.

See also:

BalusC
It's way easier than using CDI, but I can't get it to work. It works only with ajax requests. This page that contains the dataTable is a search result, when the user choose one of the results, the action is fired correctly, and it's possible to retrieve the row data, but right after that both construtor and @PostConstruct get called. So in the next page, values are null. Is this supposed to be this way?
Marcio Cruz
Forget it, it's supposed to be this way. I need the info to be forwarded to another page, so I'll stick with ConversationScope. Thanks for the tip on ViewScope though.
Marcio Cruz