Automatic Persisting selectOneMenu within DataGrid

UI Components for JSF
Post Reply
murilofla
Posts: 7
Joined: 08 Dec 2011, 02:50

08 Dec 2011, 03:06

Hi
I'm new to this forum, so I'm sorry if I miss anything.
I'm trying to use a selectOneMenu inside a dataGrid, but I can't figure out how to persist a change made in the selectOneMeny. I'm trying to do it without any buttons, just some action that automatic saves the post after changing.
The scenario is the following:
I have a manageBean here called "bean", which has an array of "Posts". In my page I have a datatable which iterates all the posts of this array, in a var called "post". For every Post I want to show a selectOneMenu with a "Categoria". After user selects one "Categoria" I want the post to be persisted.
My code:

Code: Select all

<p:scrollPanel style="width:70%;" id="scrollPnl">
       <p:dataGrid var="post" value="#{bean.posts}" columns="1"
	        id="postsGrid">
		<p:column>
                      ....
                    <h:panelGrid>
			<p:selectOneMenu value="#{post.categoria}" converter="#{categoriaConverter}" style="font-size:80%" >
            	              <f:selectItem itemLabel="#{bean.categoriaDefault.nome}" itemValue="#{bean.categoriaDefault}" />
            	              <f:selectItems value="#{bean.categorias}" var="categoria" itemLabel="#{categoria.nome}" itemValue="#{categoria}" />
                           	     <p:ajax listener="#{bean.savePost}">
            		                      <f:setPropertyActionListener target="#{bean.selectedPost}" value="#{post}"/>
            	                      </p:ajax>
        	        </p:selectOneMenu>
           	
		    </h:panelGrid>
                    ...
                    //closes things later
I can see in my logs that the Post has the Categoria property setted correctly. The only point is that I can't find any way of the "selectedPost" being setted acordingly, so I can persist the Post. I tried other combinations too, including calling a remoteComand, but none worke.
Maybe any one have a better idea to do this?
Thanks in advance
Murilo

murilofla
Posts: 7
Joined: 08 Dec 2011, 02:50

08 Dec 2011, 03:53

Hi
I forgot to describe my scenario:
Jboss 6.1.0, Mojarra 2.0.3, Primefaces 3.0.M4

Also, I have made some more tests, and I got a weird behavior, which might help.
In the following code, clicking on the commandButton will save the Post as I'd like. But what I'm trying to achieve is to save the Post without the need for a button click, but just by changing the selectOneMenu option.

The weird thing in this code is in the behavior of the remoteCommand. While the commandButton passes the correct instance of Post to the savePost method, the remoteCommand will always pass the LAST POST of the array to the same method.
Wouldn't it be expected to pass the same post as the commandButton in the code below?

Code: Select all

	<p:selectOneMenu value="#{post.categoria}" converter="#{categoriaConverter}" style="font-size:80%" onchange="remoteSavePost()" > <!--  onchange="remoteSavePost()">  -->
            	<f:selectItem itemLabel="#{bean.categoriaDefault.nome}" itemValue="#{bean.categoriaDefault}" />
            	<f:selectItems value="#{bean.categorias}" var="categoria" itemLabel="#{categoria.nome}" itemValue="#{categoria}" />
            	<!-- 
            	<p:ajax listener="#{bean.savePost}">
            		<f:setPropertyActionListener target="#{bean.selectedPost}" value="#{post}"/>
            	</p:ajax>
            	 -->

        	</p:selectOneMenu>
			<p:commandButton action="#{bean.savePost(post)}" update="panel" image="ui-icon ui-icon-check" style="font-size:60%" id="cmdButtonSavePost"/>
			<p:remoteCommand action="#{bean.savePost(post)}" update="panel" name="remoteSavePost"/>
Regards
Murilo

porthos
Posts: 122
Joined: 09 May 2011, 03:17

08 Dec 2011, 05:21

I don't understand why you see a need to do something special additional to persist the Post.

As you said, the Post already gets Categoria set correctly. And you say you always want to persist once Categoria gets set. So why not just call the bean save() method from the setCategoria() method on Post?

Or rather, it probably makes sense then to move the getter and setter service for the Categoria property to "bean" rather than "post". Internally bean can still delegate to Post where necessary.
-------
PrimeFaces 3.4, Glassfish 3.1.2, Mojarra 2.1.6

murilofla
Posts: 7
Joined: 08 Dec 2011, 02:50

08 Dec 2011, 13:56

Hi Porthos
Thanks for your reply.
The reason not to persist once the Categoria gets set is beacuse of my architecture.
I think I made a mistake when I designed it: I am using my Entities as DTOs. So Categoria is a Enntity, and it's setter is called in other layers of the application. Lesson learned: change my architecture to never use entities as DTOs anymore.
But even in other scenarios, one may not want to always persist a Pojo because a property was set. For example in another View.

Your second suggestion looks great, but I can't find a way to set both Categoria AND Post in my bean on a selectOneMenu event.
Thanks again
Murilo

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 57 guests