3

I need to dynamically add values from a text input to a listbox field. Basically, I am making a custom multi-line text component. The following works with a standard <h:selectOneListbox>, but when I use the PrimeFaces equivalent, <p:selectOneListbox>, nothing seems to happen. I am using all PrimeFaces inputs in my application, and I want to keep the look consistent.

<!--This works:-->

<h:form id="mainForm">
<p:inputText id="txtInput" /><p:commandButton value="Add" id="addTxt" 
onclick="addText()"/>
<h:selectOneListbox id="selectTxt"/>
<script type="text/javascript">
function addText(){
    var txtInput = $('#mainForm\\:txtInput').val();
    var opts = $('#mainForm\\:selectTxt').prop('options');
    opts[opts.length] = new Option(txtInput,txtInput,true,true);
        }
</script>
</h:form>

<!--This doesn't work:-->
<h:form id="mainForm">
<p:inputText id="txtInput" /><p:commandButton value="Add" id="addTxt"     
onclick="addText()"/>
<p:selectOneListbox id="selectTxt"/>
<script type="text/javascript">
    function addText(){
        var txtInput = $('#mainForm\\:txtInput').val();
        var opts = $('#mainForm\\:selectTxt_input').prop('options');
        opts[opts.length] = new Option(txtInput,txtInput,true,true);
        }
    </script>
    </h:form>

Thanks in advance for the help.

bricks
  • 153
  • 1
  • 3
  • 14

1 Answers1

2

The <p:selectOneListbox> uses an <ul><li> instead of <select><option> to present the options (with the simple reason that there's then so much more freedom to give it a different look'n'feel). That's exactly the reason why you can't just create and add new Option elements. Rightclick page in browser and do View Source and you'll understand it much better (if you're already familiar with basic HTML, of course).

Please note that adding new options this way will indeed seem to work for <h:selectOneListbox>, but that this is purely visually, in the client side. JSF won't accept the in the client side created items on submit, but throw a Validation Error: Value not valid with the simple reason that the submitted value is not part of the items as available in the server-side definied <f:selectItems>.

You need to use JSF instead of JavaScript to manipulate the <f:selectItems> value. This way it will work in <p:selectOneListbox> as well.

Here's a concrete kickoff example how to do it using pure JSF:

<h:form>
    <p:inputText value="#{bean.text}" />
    <p:commandButton value="Add" action="#{bean.add}" update="listbox" />
    <p:selectOneListbox id="listbox">
        <f:selectItems value="#{bean.items}" />
    </p:selectOneListbox>
</h:form>

With:

@ManagedBean
@ViewScoped
public class Bean {

    private String text; // +getter+setter
    private List<String> items; // +getter

    @PostConstruct
    public void init() {
        items = new ArrayList<String>();
    }

    public void add() {
        items.add(text);
    }

    // ...
}

That's all.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks, @BalusC! That is the answer. I still need to get a little tricky, because the intent is to have the user be able to add and remove values from the list, and have the entire list be the submitted value. Not sure if there is a better way to do this than using the listbox. Do you know how to set the selected values for this PrimeFaces component via jQuery? – bricks Sep 19 '12 at 15:50
  • As explained in the "See also" link, forget "plain vanilla" jQuery in cases where JSF component tree state plays a role. JSF/PrimeFaces itself is already using jQuery under covers. Use JSF/PrimeFaces directly instead of working around it. As to setting the selected values, just bind `` to a `List` backing bean property and fill that list. – BalusC Sep 19 '12 at 15:53
  • if I bind the value attribute to a `List`, does that only fill the list with the selected values? I want it to fill with all of the values in the list. – bricks Sep 19 '12 at 17:40
  • The `` represents all available items. The `` represents all selected items. If they both contain the same values, then all available items are selected. – BalusC Sep 19 '12 at 17:42
  • Thanks again for all of your insight! – bricks Sep 19 '12 at 18:21