You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Jehan <je...@gmail.com> on 2011/03/10 09:45:44 UTC

radioGroup in DataView

Dear All

I am using radioGroup example found at URL:
https://cwiki.apache.org/WICKET/using-radiogroups.html
when I am clicking on submit button then old/Dafault selected values are
printing.

Using following code(saveRoleAssignments method) I want to print user
selected radio button values (e.g., true or false)

for printing I am using info method.

Thanks

public class rdoTest extends WebPage {
 public rdoTest()
 {
  ArrayList list = new ArrayList();
  list.add(new Person(1,"Xyz"));
  list.add(new Person(2,"abc"));
  list.add(new Person(3,"def"));
  list.add(new Person(1,"ghi"));
  list.add(new Person(1,"jkl"));



  ListDataProvider myIDataProvider= new ListDataProvider(list);

  final Form profileForm = new Form("profile-form");
  final DataView dataView = new DataView("profile-rows", myIDataProvider) {
   private static final long serialVersionUID = 1L;
   private static final int ROLE_SYS_ADMIN = 1;
   private static final int ROLE_DEPT_ADMIN = 2;
   private static final int ROLE_NO_ADMIN = 3;
   /**
   * {@inheritDoc}
   */
   @Override
   protected void populateItem(final Item item) {
    Person person = (Person) item.getModelObject();
    item.add(new Label("first-name", person.getName()));
   // item.add(new Label("last-name", person.getLastName()));
    // add the radio group to select system or department (organizational)
administrators
    final RadioGroup adminRadioGroup = new RadioGroup("radio-admin");
    item.add(adminRadioGroup);


    // create our three models with corresponding object values
(PersonRoleXrefId = persistent model that holds person/role id)
    final Model sysAdminModel = new Model(new
PersonRoleXrefId(person.getId(), ROLE_SYS_ADMIN));
    final Model deptAdminModel = new Model(new
PersonRoleXrefId(person.getId(), ROLE_DEPT_ADMIN));
    final Model noAdminModel = new Model(new
PersonRoleXrefId(person.getId(), ROLE_NO_ADMIN ));
    // add the models to the radios and the radios to the radio group
    adminRadioGroup.add(new Radio("radio-sys-admin", sysAdminModel));
    adminRadioGroup.add(new Radio("radio-dept-admin", deptAdminModel));
    adminRadioGroup.add(new Radio("radio-non-admin", noAdminModel));
    // set current default role selection based upon the currently iterated
item
    // ( hasRole(...) is example method that determines if the person has
the specified role or not)
    if (person.getId() == ROLE_SYS_ADMIN) {
     adminRadioGroup.setModel(sysAdminModel);
    } else if (person.getId() == ROLE_DEPT_ADMIN) {
     adminRadioGroup.setModel(deptAdminModel);
    } else {
     adminRadioGroup.setModel(noAdminModel);
    }
   }
  };
  profileForm.add(dataView);
  Button okButton = new Button("button-save")
  {
   private static final long serialVersionUID = 1L;
   /**
   * {@inheritDoc}
   */
   @Override
   public final void onSubmit() {
    info("\n------------------------------------------------------\n");
   // radioGroup.getInput(), radio1.getValue());
    // see section for "Processing more than one RadioGroup simultaneously"
for explanation
    saveRoleAssignments(dataView);
   }
  };
  profileForm.add(okButton);
  add(profileForm);

 }

 private final void saveRoleAssignments(final DataView dataView) {
  final ArrayList<PersonRoleXrefId> saveXrefs = new
ArrayList<PersonRoleXrefId>();
  for (final Iterator<?> it = dataView.getItems(); it.hasNext();) {
   final Item item = (Item) it.next();
   final Person person = (Person) item.getModelObject();
   item.visitChildren(new Component.IVisitor() {
    /**
    * {@inheritDoc}
    */
    //@Override
    public final Object component(final Component component) {
     if (component instanceof RadioGroup) {


      final PersonRoleXrefId prId = (PersonRoleXrefId)
component.getDefaultModelObject();//getModelObject();
      // compare with existing person's assigned roles and only add changed
associations
      if (person.getId() == prId.getId()) {
       //saveXrefs.add(prId);
       //info(person.getName() + "--------" + prId.getId());
      }
      ///here I want to show new selected radio buttons [true or false]
      info("\n"+person.getName() + "--------" + prId.getId());
     }
     // continue visiting children to locate other RadioGroup components
     return CONTINUE_TRAVERSAL;
    }
   });
  }
  if (!saveXrefs.isEmpty()) {
   // save person/role assignments using a IDataProvider assigned to the
DataView
   //((MyIDataProvider)
dataView.getDataProvider()).saveRoleAssignments(saveXrefs);
  }
 }

}