Question

I have an object something like

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;
}

So, Here my workflow is a Table consists of multiple Field's and a Field will have multiple Column's. so, on webUI i need to show a Table with name and Field Dropdown as rows. When user selects a field, then i need to dynamically get the selected field and the render the columns belonging to the selected field. Here, how to get the selected Fields from web UI. I tried with AjaxFormComponentUpdatingBehavior("onchange"). But i am getting all the fields of that dropdown.

My DropDown Choice is as follows :

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();
 }
});

How to handle such scenario. Please help...

Was it helpful?

Solution

See complete example below. Main parts are ViewModel(VM) and ViewModelLoader(VML). VM is what you want to display in the UI. VML fills VM with data from say database. With PropertyModel I'm binding dropdown items and selected value. To update doropdown I'm updating VM and adding component to the 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);
            }
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top