FilterForm with DropDownChoice

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

FilterForm with DropDownChoice

christophManig
Hello,

I want to create a table with a filter form.

I used the wicket FilterForm and implemented an own DataProvider. It works fine for the textfields. I can write some text and press "enter" and the list will be filtered. But if I make a choice in the selectbox and press "enter"
nothing happened. How can I use a DropDownChoice in the FilterForm?

DataProvider:
public class RouteStateDataProvider extends SortableDataProvider<RouteStateModel, String> implements IFilterStateLocator<RouteStateModel> {

        /** serialVersionUID */
        private static final long serialVersionUID = 6220885802292065166L;

        /** Logger which writes on the console. */
        private static final Logger log = LoggerFactory.getLogger(ItemDataProvider.class);

        /** The list of all data. */
        private List<RouteStateModel> dataList;
        /** The class which contains the filter field */
        private RouteStateModel filterModel = new RouteStateModel();
        /** The comparator for sorting. */
        private DAPComparator<RouteStateModel> comparator;

        /**
         * Constructor which create the data provider and sets the initial sort column and the sort
         * order ascending.
         *
         * @param dataList list of all rows which should be displayed
         * @param startSortField the field name of the initial sort column
         */
        public RouteStateDataProvider(List<RouteStateModel> dataList, String startSortField) {
                this.dataList = dataList;

                setSort(startSortField, SortOrder.ASCENDING);
                comparator = new DAPComparator<RouteStateModel>(this);
        }

        @Override
        public Iterator<RouteStateModel> iterator(long first, long count) {
                if (dataList != null) {
                        Collections.sort(dataList, comparator);
                } else {
                        log.debug("List of data is emtpy!");
                }
                return this.filterRoutes().subList((int) first, (int) (first + count)).iterator();
        }

        /**
         * Creates a list which only contains the items with the given id.
         *
         * @return the filtered item list
         */
        private List<RouteStateModel> filterRoutes() {
                List<RouteStateModel> resultList = new ArrayList<>();
                String camelRouteId = filterModel.getCamelRouteId();
                String modulName = filterModel.getModuleName();
                TargetState targetState = filterModel.getTargetState();

                if(filterModel.isEmpty()) {
                        return dataList;
                }  else {
                        for(RouteStateModel route : dataList) {
                                boolean inFilter = false;

                                if(camelRouteId != null) {
                                        if(route.getCamelRouteId().contains(camelRouteId)) {
                                                inFilter = true;
                                        } else {
                                                inFilter = false;
                                        }
                                }
                                if(modulName != null) {
                                        if(route.getModuleName().contains(modulName)) {
                                                inFilter = true;
                                        } else {
                                                inFilter = false;
                                        }
                                }
                                if(targetState != null) {
                                        if(route.getTargetState().equals(targetState)) {
                                                inFilter = true;
                                        } else {
                                                inFilter = false;
                                        }
                                }

                                if(inFilter) {
                                        resultList.add(route);
                                }
                        }
                        return resultList;
                }
        }

        @Override
        public long size() {
                return this.filterRoutes().size();
        }

        @Override
        public IModel<RouteStateModel> model(RouteStateModel routeStateModel) {
                return new Model<RouteStateModel>(routeStateModel);
        }

        @Override
        public RouteStateModel getFilterState() {
                return this.filterModel;
        }

        @Override
        public void setFilterState(RouteStateModel routeStateModel) {
                this.filterModel = routeStateModel;
        }
}

FilterForm:
public class RouteStateForm extends FilterForm<RouteStateModel> {

        private IFilterStateLocator<RouteStateModel> locator;

        public RouteStateForm(String id, IFilterStateLocator<RouteStateModel> locator) {
                super(id, locator);

                this.locator = locator;

                addTable();
                addTextfields();
                addSelectBoxes();
        }

        /**
         * Adds the table to the form.
         */
        private void addTable() {
                RouteStateTable routeStateTable = new RouteStateTable("searchTable", this, (SortableDataProvider<RouteStateModel, String>) locator);
                add(routeStateTable.init());
        }

        /**
         * Adds the necessary textfields to the form.
         */
        private void addTextfields() {
                TextField<String> camelRouteId = new TextField<>("camelRouteId", PropertyModel.of(locator, "filterModel.camelRouteId"));
                TextField<String> moduleName = new TextField<>("moduleName", PropertyModel.of(locator, "filterModel.moduleName"));

                add(camelRouteId);
                add(moduleName);
        }

        /**
         * Adds the additional selectboxes.
         */
        private void addSelectBoxes() {
                DropDownChoice<TargetState> targetState = new DropDownChoice<TargetState>("targetState", PropertyModel.of(locator, "filterModel.targetState"), Arrays.asList(TargetState.values()));
                targetState.setChoiceRenderer(new ChoiceRenderer<TargetState>("guiValue"));
                targetState.setNullValid(true);
                targetState.setOutputMarkupId(Boolean.TRUE);
                add(targetState);
        }
}


Mit freundlichen Grüßen
Christoph Manig

Reply | Threaded
Open this post in threaded view
|

Re: FilterForm with DropDownChoice

Sven Meier
Hi,

have you tried subclassing your choices and adding some JavaScript?

   DropDownChoice(...) {
      onComponentTag(ComponentTag tag) {
        super.onComponentTag(tag);
        tag.put("onchange", "this.form.submit()");
      }
   }

I haven't tried this though.

Have fun
Sven
Reply | Threaded
Open this post in threaded view
|

AW: FilterForm with DropDownChoice

christophManig
Hello,

thank you that works fine for my problem.


Mit freundlichen Grüßen
Christoph Manig

-----Ursprüngliche Nachricht-----
Von: Sven Meier [mailto:[hidden email]]
Gesendet: Mittwoch, 22. März 2017 16:16
An: [hidden email]
Betreff: Re: FilterForm with DropDownChoice

Hi,

have you tried subclassing your choices and adding some JavaScript?

   DropDownChoice(...) {
      onComponentTag(ComponentTag tag) {
        super.onComponentTag(tag);
        tag.put("onchange", "this.form.submit()");
      }
   }

I haven't tried this though.

Have fun
Sven

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/FilterForm-with-DropDownChoice-tp4677385p4677392.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]