Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

af:selectonechoice - Default selected value 1

Status
Not open for further replies.

DairylandDan

Programmer
Apr 3, 2008
17
0
0
US
Hmmm, I'm stuck and would appreciate any help available (bet you've never read that on here before - LOL).

I'm building a dynamically populated af:selectonechoice and it works almost exactly as I'd like it. I'm grabbing distinct years from our database for those records which are active via a query in a sessionfacadebean - this query simply orders them in decending order (for example: 2011, 2010, 2009, 2008). But, my problem is that I can only get the page to load with the selectonechoice defaulted to the first year.

Here's the code as it sits now . . .
jspx page:
Code:
<!-- Dynamic selectOneChoice of only available years from vHolidayDisplayFlag -->                           
<af:selectOneChoice value="0" autoSubmit="true" required="false"
                    id="selectOneChoiceYears" label="Year"
                    binding="#{backing_reports_calholiday.selectOneChoiceYears}"
                    valueChangeListener="#{backing_reports_calholiday.valueChangeListenerYear}">
 <f:selectItems id="selectItemsYears" binding="#{backing_reports_calholiday.selectItemsYears}" value="#{backing_reports_calholiday.availableYears}"/>
</af:selectOneChoice>
<!-- End of Dynamic selectOneChoice of only available years from vHolidayDisplayFlag -->

and here's the code from the backing bean:
Code:
 // get Available Years
 // This is designed to populate the selectOneChoice list with ONLY those years which are available in V_Holiday_Display_Flag
 public List<SelectItem> getAvailableYears() {
  Calendar cal = new GregorianCalendar();
  Integer currentyear = cal.get(Calendar.YEAR);
  int counter = 0;
  int holder = 0;

  OperationBinding operationBinding = getBindings().getOperationBinding("queryVHolidayScheduleByYearsAvailable");
  List<Long> years =  (List<Long>)operationBinding.execute();          
  Vector<SelectItem> listYears = new Vector<SelectItem>();

  for(Long year : years) {
   listYears.add(new SelectItem(Integer.toString(counter), year.toString()));
   if(year.equals(currentyear)){
    selectOneChoiceYears.setSubmittedValue(Integer.toString(counter)); // trying this?
    selectOneChoiceYears.setValue(Integer.toString(counter)); // trying this?
   }
   counter++;
  }

  return listYears;
 }

I've tried any and all manner of voodoo and code permutations, but, obviously, I'm missing the obvious somewhere. The selectoncechoice ALWAYS defaults to whatever the first year is in the query - and, of course, we want the years in order. But, we'd like the page to load with the current year selected by default.

* I've tried setting a value of "holder" in the backing bean when I'm working with the current year, and then reference that in the jspx page with the "value" element of the af:selectOneChoice - like this --> value="#{backing_reports_calholiday.holder}" . . . but, that didn't work.
* I can, of course, hard code the "value" element of the af:selectOneChoice when I "know" what the placement of the current year is in my result set . . . but, obviously, that's not the solution.

Hmmm (Image of my scratching my chin thoughtfully)

Any and all help would be greatly appreciated.



dLd
 
I'm not sure what is doing that if sentence, but comparing a Long with an Integer will always return false AFAIK.

Cheers,
Dian
 
Hey 'Diancecht', thanx for the pointer!

OK, I got it figured out.

The problem was with my if statement in the backing bean.
I had (see lines with red asterisk):

Code:
 // get Available Years
 // This is designed to populate the selectOneChoice list with ONLY those years which are available in V_Holiday_Display_Flag
 public List<SelectItem> getAvailableYears() {
  Calendar cal = new GregorianCalendar();
[COLOR=red]*[/color] Integer currentyear = cal.get(Calendar.YEAR);
  int counter = 0;
  int holder = 0;

  OperationBinding operationBinding = getBindings().getOperationBinding("queryVHolidayScheduleByYearsAvailable");
  List<Long> years =  (List<Long>)operationBinding.execute();          
  Vector<SelectItem> listYears = new Vector<SelectItem>();

[COLOR=red]*[/color] for(Long year : years) {
   listYears.add(new SelectItem(Integer.toString(counter), year.toString()));
[COLOR=red]*[/color]  if(year.equals(currentyear)){
    selectOneChoiceYears.setSubmittedValue(Integer.toString(counter)); // trying this?
    selectOneChoiceYears.setValue(Integer.toString(counter)); // trying this?
   }
   counter++;
  }

  return listYears;
 }

The problem (I see) is that 'year' is a Long and 'currentyear' is an Integer. So, I was getting a false. I changed it to (again, see lines with red asterisk):

Code:
 // get Available Years
 // This is designed to populate the selectOneChoice list with ONLY those years which are available in V_Holiday_Display_Flag
 public List<SelectItem> getAvailableYears() {
  Calendar cal = new GregorianCalendar();
[COLOR=red]*[/color] Integer currentyear = cal.get(Calendar.YEAR);
  int counter = 0;
  int holder = 0;

  OperationBinding operationBinding = getBindings().getOperationBinding("queryVHolidayScheduleByYearsAvailable");
  List<Long> years =  (List<Long>)operationBinding.execute();          
  Vector<SelectItem> listYears = new Vector<SelectItem>();

[COLOR=red]*[/color] for(Long year : years) {
   listYears.add(new SelectItem(Integer.toString(counter), year.toString()));
[COLOR=red]*[/color]  if(year.intValue() == currentyear){
    selectOneChoiceYears.setValue(Integer.toString(counter));
   }
   counter++;
  }

  return listYears;
 }

And, then, removed the hard coded set of the value from the .jspx and now it works correctly.

Dan-O

 
Glad you got it working.

To avoid future problems with this, I'd modify the queryVHolidayScheduleByYearsAvailable operation to return an array of Integers.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top