4 Replies Latest reply: Sep 2, 2008 2:34 PM by 622585 RSS

    How to get selected value from SelectOneChoice

    420027
      Hi,
      I'm facing a problem to get selected value from SelectOneChoice. I have valueChangeListener event on a (SelectOneChoice)item. After user makes a choice I want to store selected value in a bean property to pass it to a method.
      For example List item shows dname from dept table after user makes a choice I want to get deptno and populate into bean which I use to pass into my method.
      If I use valueChangeEvent.getNewValue() I always get negative value instead I want deptno selected. Sample code pasted below.

      public void setDeptno(ValueChangeEvent valueChangeEvent) {
      BindingContainer b = getBindings();
      OperationBinding oB = b.getOperationBinding("setDeptno");
      //Checking instance of because same method is called from another text inputText item.
      if (valueChangeEvent.getSource() instanceof CoreSelectOneChoice){
      CoreSelectOneChoice cN = (CoreSelectOneChoice)valueChangeEvent.getSource();
      if (columnName.getId().toString().equals("deptDname")){
      JSFUtils.setManagedBeanValue("dept.deptDeptno",valueChangeEvent.getNewValue());
      }
        • 1. Re: How to get selected value from SelectOneChoice
          371798
          Hi,
          Frank Nimphus had posted an example about how to do this right. The idea is that valueChangeEvent.getNewValue() gives you the index of the selected choice not the choice itself, so you have to use the getRowAtRangeIndex function in order to retrieve the data from your iterator.

          The link I kept for the original post is not working anymore, so here is an example of how I retrieve a selected region from my own code.
                  . . .
                  int rangeIndex = (Integer)valueChangeEvent.getNewValue();
          
                  // new value only gives you the index
                  DCIteratorBinding regionsIter =
                      (DCIteratorBinding)bindings.get("RegionsLOVIterator");
                  Row row = regionsIter.getRowAtRangeIndex(rangeIndex);
          
                  // get the selected region
                  String region = (String)row.getAttribute("RegionName");
          
                 . . .
          HTH
          Thanassis
          • 2. Re: How to get selected value from SelectOneChoice
            635600
            if your selectOneChoice has value equal to #{bindings.deptno} bound to the iterator Dept1Iterator,
            then the backing code will look more like
                            public void setDeptno(ValueChangeEvent valueChangeEvent) {
                                BindingContainer b = getBindings();
                                OperationBinding oB = b.getOperationBinding("setDeptno");
                                //Checking instance of because same method is called from another text inputText item.
                                if (valueChangeEvent.getSource() instanceof CoreSelectOneChoice){
                                    CoreSelectOneChoice cN = (CoreSelectOneChoice)valueChangeEvent.getSource();
                                if (columnName.getId().toString().equals("deptDname")){
                                    FacesContext ctx = FacesContext.getCurrentInstance();
                                    Application app = ctx.getApplication();
                                    ValueBinding bind = app.createValueBinding("#{bindings.Dept1Iterator.currentRow}");
                                    Row row = (Row)bind.getValue(ctx);
                                    JSFUtils.setManagedBeanValue("dept.deptDeptno", row.getAttribute("deptno"));
                                }
                            } 
            I haven't tested it, so it could perfectly not work at all
            • 3. Re: How to get selected value from SelectOneChoice
              420027
              Thnaks guys it worked !
              • 4. Re: How to get selected value from SelectOneChoice
                622585
                I've implemented the following code in an attempt to pull the 'actual' value from the selectOneChoice list box.

                . . .
                int rangeIndex = (Integer)valueChangeEvent.getNewValue();

                // new value only gives you the index
                DCIteratorBinding regionsIter =
                (DCIteratorBinding)bindings.get("RegionsLOVIterator");
                Row row = regionsIter.getRowAtRangeIndex(rangeIndex);

                // get the selected region
                String region = (String)row.getAttribute("RegionName");

                . . .


                I'm receiving a compile error on ' bindings.get ' Can someone give the rest of the code where bindings gets set? Thanks