0

I have a selectonemenu that appears in 2 different dialogs on the same screen, I need that when one of them changes its value and then user opens the other dialog, the selectonemenu still has the selected value previously, and viceversa.

I tried doing same binding for both of them but I notice that one of them would dissapear, whats the proper way to achieve this?

this is the code of them by the way:

<p:selectOneMenu binding="#{homeView.somTipoPlato}"
                id="somTipoPlato" style="width:100%">
                <p:ajax event="change" listener="#{homeView.listerprueba}" />
                <f:selectItem itemLabel="Tipo de Comida"></f:selectItem>
                <f:selectItems value="#{homeView.losTipoPlatoSelectItem}" />
            </p:selectOneMenu>

    <p:selectOneMenu id="somTipoPlatoBuscarPlato"
                    binding="#{homeView.somTipoPlato}" style="width:100%">
                    <p:ajax event="change" listener="#{homeView.listerprueba}" />
                    <f:selectItem itemLabel="Tipo de Comida"></f:selectItem>
                    <f:selectItems value="#{homeView.losTipoPlatoSelectItem}" />
                </p:selectOneMenu>

Any proper way or workaround?

BugsForBreakfast
  • 712
  • 10
  • 30
  • Why do you need binding here? Really, really weird design. – Kukeltje Sep 01 '19 at 17:42
  • @Kukeltje Because I need to check whats their value on the server side mate, no other way to check that or there is? – BugsForBreakfast Sep 01 '19 at 17:46
  • Uhhh.... there is value attribute. It's in the docs, showcase etc. It the attribute ised in 99.9% of the tutorials for whatever jsf compoment. You even used it in previous cases iirc. – Kukeltje Sep 01 '19 at 18:35
  • @Kukeltje so I can achieve what I want in question by using value on both of them instead of binding? Like doing value="#{homeView.somTipoPlato}" instead of binding="#{homeView.somTipoPlato}" ?? – BugsForBreakfast Sep 01 '19 at 19:16
  • Yes, but still then, binding two inputs to one value is really not common since the last one aplied will always win (iirc). So it would normally be pointing to two different fields.But the value should be a normal String/Date/Integer/.. field while the binding would point to a component – Kukeltje Sep 02 '19 at 05:28
  • 1
    See also https://stackoverflow.com/questions/14911158/how-does-the-binding-attribute-work-in-jsf-when-and-how-should-it-be-used – Kukeltje Sep 02 '19 at 08:34
  • @Kukeltje Hey brother, I read about it, to be honest I didn't know about that.. so using binding is more a design smell, in my case its just that I didn't know it was a bad practice, I can use value with no problem, will try to change all bindings to values, that also makes performance better right?? thanks mate but still don't downvote my answer cause maybe in some rare case might be helpful for somebody :) – BugsForBreakfast Sep 02 '19 at 13:43
  • You answer is honestly totally useless and very bad practice for just getting values. The link I posted in my comment already makes clear binding should 1: be request scoped and 2: per COMPONENT. So If you improve your answer with al the info from the comments (including the bad practice/design smell and reference the link I posted, I'll even UPVOTE it. – Kukeltje Sep 02 '19 at 13:50
  • @Kukeltje I added some edit mate :) – BugsForBreakfast Sep 02 '19 at 14:13
  • @Kukeltje hey dear friend, can you do me a little favor and thumbs up this question? I appreciate so I can ask something im having trouble with at the moment :) thanks – BugsForBreakfast Sep 12 '19 at 13:34

1 Answers1

1

Hey guys I just needed to use the value property of the selectonemenus, here is why:

After receiving some feedback in the comments and reading How does the 'binding' attribute work in JSF? When and how should it be used?

I noticed that using binding most of the times is a bad practice or a design smell, so in my case I didn't know it was a bad practice so I was using it just because I though it was a normal way to do the things, but it wasn't, so the proper solution for this would be using value instead of binding and that way I can achieve what I wanted, 2 selectonemenus with same value, like so:

<p:selectOneMenu value="#{homeView.selectedValue}"
                id="somTipoPlato" style="width:100%">

                <f:selectItem itemLabel="Tipo de Comida"></f:selectItem>
                <f:selectItems value="#{homeView.losTipoPlatoSelectItem}" />
            </p:selectOneMenu>


       <p:selectOneMenu id="somTipoPlatoBuscarPlato"
                value="#{homeView.selectedValue}" style="width:100%">

                <f:selectItem itemLabel="Tipo de Comida"></f:selectItem>
                <f:selectItems value="#{homeView.losTipoPlatoSelectItem}" />
            </p:selectOneMenu>

notice that this way they are able to share the same value and there is also no need of ajax listeners, hope this helps somebody that comes across same situation.

In the link provided you will find all the reasons why using value is the correct approach

BugsForBreakfast
  • 712
  • 10
  • 30
  • 2
    I'm inclined to downvote since this is an uncommon solution for an unclear problem. But I won't yet. But next time (like stated several times) create a good question. Ask about your functional problem with technical code instead of asking a pure technica; question. You seem to have an http://xyproblem.info (again) here. – Kukeltje Sep 01 '19 at 18:39
  • 1
    https://stackoverflow.com/questions/14911158/how-does-the-binding-attribute-work-in-jsf-when-and-how-should-it-be-used – Kukeltje Sep 02 '19 at 08:34
  • The first old part is better removed... The part after the edit is the relevant part. But please also add that you 'just' need it to get a value of a component. – Kukeltje Sep 02 '19 at 14:23
  • @Kukeltje Done brother, thanks for your help, I was wondering if you could check this question of mine too and see if maybe you could help me? https://stackoverflow.com/questions/57739084/where-can-i-find-practical-example-of-knn-in-java-using-weka – BugsForBreakfast Sep 02 '19 at 14:29
  • Begging for support is not nice to others (and me) and (ab)using a question for another question isn't either. Are you sure you want me to have a look? I'm 100% sure it is off-topic and all I can do is vote to close (no, no downvote) #4 in https://stackoverflow.com/help/on-topic (I did have quick look) – Kukeltje Sep 02 '19 at 16:34
  • @Kukeltje im not begging mate, just asked you to have a quick look, I added the link I am getting the info of so you can understand me better :) and hopefully it isn't offtopic anymore, im having trouble with that algorithm – BugsForBreakfast Sep 02 '19 at 16:45
  • Asking to have a look at other questions is 'begging' especially if it is a completely different subject, the amount of time I now spent discussing this is time wasted on topocs for me. And adding a link to a remote sort of tutorial does not make it on-topic persay but it might not be off topic with all the other info you added. I just don't use all this, so sorry – Kukeltje Sep 02 '19 at 16:51