2013-04-15 80 views
1

選擇下拉值從表中我有一個對象像阿帕奇檢票:入門的UI

public class Table { 
    private String id; 
    private String name; 
    private List<Field> fieldsList; 
} 

public class Field { 
    private List<Column> columnList; 
} 

public class Column{ 
    String id; 
} 

所以,在這裏我的工作流程是一個表由多個領域的,並現場將有多個列的。所以,在webUI我需要顯示一個名稱和字段下拉列表作爲行。當用戶選擇一個字段時,我需要動態獲取所選字段並渲染屬於所選字段的列。在這裏,如何從Web UI中獲取選定的字段。我嘗試了AjaxFormComponentUpdatingBehavior(「onchange」)。但我正在獲取該下拉列表的所有字段。

我的下拉選擇如下:

IChoiceRenderer choiceRenderer = new ChoiceRenderer("Name", "id"); 
DropDownChoice dropDownChoice = new DropDownChoice("ddc"); 
dropDownChoice.setChoiceRenderer(choiceRenderer); 
dropDownChoice.setChoices(table.getFieldsList()); 
dropDownChoice.setModel(new CompoundPropertyModel(new Field())); 

dropDownChoice.add(new AjaxFormComponentUpdatingBehavior("onchange") { 

@Override 
protected void onUpdate(AjaxRequestTarget target) { 
    //Following is returning all the List of Fields. 
    Object defaultModelObject = getModelObject(); 
} 
}); 

如何處理這樣的情況。請幫助...

回答

1

請參閱下面的完整示例。主要部分是ViewModel(VM)和ViewModelLoader(VML)。 虛擬機是你想要在用戶界面中顯示的。 VML使用say數據庫中的數據填充虛擬機。 使用PropertyModel我綁定下拉項目和選定的值。 要更新doropdown,我正在更新VM並將組件添加到AjaxRequestTarget。

public class PlayingWithDropDownPage extends WebPage { 

     public PlayingWithDropDownPage() { 
      final ViewModelLoader viewModelLoader = new ViewModelLoader(); 
      final ViewModel viewModel = viewModelLoader.load(); 

      IChoiceRenderer choiceRenderer = new ChoiceRenderer("name", "id"); 
      final DropDownChoice dropDownChoice = new DropDownChoice("dropDown"); 
      dropDownChoice.setChoiceRenderer(choiceRenderer); 
      dropDownChoice.setChoices(viewModel.getItemsModel()); 
      dropDownChoice.setModel(viewModel.getSelectedModel()); 

      dropDownChoice.add(new AjaxFormComponentUpdatingBehavior("onchange") { 
       @Override 
       protected void onUpdate(AjaxRequestTarget target) { 
        viewModelLoader.load(viewModel); 

        target.add(dropDownChoice); 
       } 
      }); 
      add(dropDownChoice); 
     } 

     public static class ViewModel implements Serializable { 
      private WhatToShow whatToShow; 
      private List<Item> items = new ArrayList<>(); 
      private Item selected; 

      public IModel<List<Item>> getItemsModel() { 
       return new PropertyModel<>(this, "items"); 
      } 

      public IModel<Item> getSelectedModel() { 
       return new PropertyModel<>(this, "selected"); 
      } 
     } 

     public static class ViewModelLoader extends LoadableDetachableModel<ViewModel> { 

      @Override 
      protected ViewModel load() { 
       return load(new ViewModel()); 
      } 

      protected ViewModel load(ViewModel vm) { 
       vm.items.clear(); 

       if (vm.whatToShow == WhatToShow.City) { 
        vm.whatToShow = WhatToShow.Person; 
        vm.items.add(new Person("1", "John", "Smith")); 
        vm.items.add(new Person("2", "Robert", "Evans")); 
        vm.items.add(new Person("3", "Jeff", "Jones")); 
       } else { 
        vm.whatToShow = WhatToShow.City; 
        vm.items.add(new City("1", "London")); 
        vm.items.add(new City("2", "Moscow")); 
        vm.items.add(new City("3", "Kiev")); 
       } 

       return vm; 
      } 
     } 

     public static interface Item { 
      public String getId(); 

      public String getName(); 
     } 

     private enum WhatToShow { 
      Person, 
      City 
     } 

     public static class City implements Item { 

      public String id; 
      public String name; 

      public City(String id, String name) { 
       this.id = id; 
       this.name = name; 
      } 

      @Override 
      public String getId() { 
       return id; 
      } 

      @Override 
      public String getName() { 
       return name; 
      } 
     } 

     public static class Person implements Item { 
      public String id; 
      public String fname; 
      public String lname; 

      public Person(String id, String fname, String lname) { 
       this.id = id; 
       this.fname = fname; 
       this.lname = lname; 
      } 

      @Override 
      public String getId() { 
       return id; 
      } 

      @Override 
      public String getName() { 
       return String.format("%s %s", fname, lname); 
      } 
     } 
    } 
+0

你知道了,但如何更新Columndropdown在這個選定的字段上使用ajax。我會得到選中字段的列下拉列表的相應下拉列表名稱嗎? – speruri

+0

我已經更新了我的答案 –

+0

非常感謝@Alexey,它幫助我找到了解決方案。 TanX很多... – speruri