Thanks alot Ernesto, exactly what I've been wanting, the dropdown no longer
crashes when there is no data.

Cheers,
Kimotho.

On Thu, Apr 29, 2010 at 4:39 PM, Ernesto Reinaldo Barreiro <
reier...@gmail.com> wrote:

> Kimotho,
>
> Following example works.
>
> ------------------------------------
>
> package test.dataview;
>
> import java.io.Serializable;
> import java.util.ArrayList;
> import java.util.List;
>
> public class TestBean implements Serializable {
>
>        private static final long serialVersionUID = 1L;
>
>        public static final List<String> CATEGORIES = new
> ArrayList<String>();
>
>        static {
>                CATEGORIES.add("A");
>                CATEGORIES.add("B");
>                CATEGORIES.add("C");
>        };
>
>        private static List<TestBean> beans;
>
>        String name;
>        String category;
>
>        public TestBean() {
>        }
>
>        public String getName() {
>                return name;
>        }
>        public void setName(String name) {
>                this.name = name;
>        }
>        public String getCategory() {
>                return category;
>        }
>        public void setCategory(String category) {
>                this.category = category;
>        }
>
>        public static List<TestBean> getBeans() {
>                if(beans == null) {
>                        beans = new ArrayList<TestBean>();
>                        for(String cat: new String[]{"A","B"})
>                                for(int i=0; i<10; i++) {
>                                        TestBean bean = new TestBean();
>                                        bean.setCategory(cat);
>                                        bean.setName("Name " + cat +  i);
>                                        beans.add(bean);
>                                }
>                }
>                return beans;
>        }
> }
>
> --------------------
>
> package test.dataview;
>
> import java.util.ArrayList;
> import java.util.List;
>
> import org.apache.wicket.markup.repeater.data.IDataProvider;
> import org.apache.wicket.model.IModel;
> import org.apache.wicket.model.Model;
>
> public class TestBeanDataProvider implements IDataProvider<TestBean> {
>
>        private static final long serialVersionUID = 1L;
>
>        private TestBean filter;
>
>        private List<TestBean> list;
>
>        /**
>         *
>         */
>        public TestBeanDataProvider() {
>        }
>
>        public java.util.Iterator<? extends TestBean> iterator(int first,
> int count) {
>                return getList().iterator();
>        };
>
>        public int size() {
>                return getList().size();
>        }
>
>        public IModel<TestBean> model(TestBean object) {
>                return new Model<TestBean>(object);
>        }
>
>        List<TestBean> getList() {
>                if(filter == null || filter.category == null) {
>                        return TestBean.getBeans();
>                }
>                if(list == null) {
>                        list = new ArrayList<TestBean>();
>                        for(TestBean bean: TestBean.getBeans()){
>                                if(bean.category.equals(filter.category))
>                                        list.add(bean);
>                        }
>                }
>                return list;
>        }
>
>
>        public void detach() {
>                list = null;
>        };
>
>        public TestBean getFilter() {
>                return filter;
>        }
>
>        public void setFilter(TestBean filter) {
>                this.filter = filter;
>        }
> }
>
> -------------------------
>
> package test.dataview;
>
> import org.apache.wicket.ajax.AjaxRequestTarget;
> import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
> import org.apache.wicket.markup.html.WebMarkupContainer;
> import org.apache.wicket.markup.html.basic.Label;
> import org.apache.wicket.markup.html.form.DropDownChoice;
> import org.apache.wicket.markup.html.form.Form;
> import org.apache.wicket.markup.html.panel.Panel;
> import org.apache.wicket.markup.repeater.Item;
> import org.apache.wicket.markup.repeater.data.DataView;
> import org.apache.wicket.model.Model;
>
> public class TestDataViewPanel extends Panel {
>
>        private static final long serialVersionUID = 1L;
>
>    private WebMarkupContainer toRepaint;
>
>    private TestBean bean;
>
>    private TestBeanDataProvider dataProvider;
>
>    /**
>         * Constructor that is invoked when page is invoked without a
> session.
>         *
>         * @param parameters
>         *            Page parameters
>         */
>    public TestDataViewPanel(String id) {
>        super(id);
>
>        bean = new TestBean();
>
>        dataProvider = new TestBeanDataProvider();
>
>
>        toRepaint = new WebMarkupContainer("toRepaint");
>        toRepaint.setOutputMarkupId(true);
>
>        add(toRepaint);
>
>        Form<TestBean> form = new Form<TestBean>("form");
>
>        toRepaint.add(form);
>
>        DropDownChoice<String> category = new
> DropDownChoice<String>("category", new Model<String>() {
>
>                        private static final long serialVersionUID = 1L;
>
>                        @Override
>                public String getObject() {
>                        return bean.getCategory();
>                }
>
>                        @Override
>                public void setObject(String object) {
>                        bean.setCategory(object);
>                }
>        },TestBean.CATEGORIES);
>        category.add(new AjaxFormComponentUpdatingBehavior("onchange") {
>
>                private static final long serialVersionUID = 1L;
>
>                        @Override
>                protected void onUpdate(AjaxRequestTarget target) {
>                                 dataProvider.setFilter(bean);
>                                target.addComponent(toRepaint);
>                }
>        });
>
>        form.add(category);
>
>        form.add(new DataView<TestBean>("rows", dataProvider) {
>
>                        private static final long serialVersionUID = 1L;
>
>                        public void populateItem(final Item<TestBean> item)
>                        {
>                                final TestBean user = item.getModelObject();
>                                item.add(new Label("id", user.getName()));
>                        }
>                 });
>    }
>
> }
>
> --------------------------------------
>
> <html xmlns:wicket="
> http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd";>
>    <body>
> <wicket:panel>
>        <div wicket:id="toRepaint">
>                <form wicket:id="form">
>
>                        <div>
>                                Category: <select
> wicket:id="category"></select>
>                        </div>
>
>
>                        <table>
>                                <tbody>
>                                <tr wicket:id="rows">
>                                        <td><span
> wicket:id="id"></span></td>
>                                </tr>
>                        </tbody>
>                        </table>
>
>                </form>
>        </div>
> </wicket:panel>
>    </body>
> </html>
>
> ---------------------------
>
> Is something like this what you want to achieve?
>
> Ernesto
>
>
> On Thu, Apr 29, 2010 at 2:25 PM, Ernesto Reinaldo Barreiro
> <reier...@gmail.com> wrote:
> > Just some questions... How does DataView's dataProvider get's affected
> > by the code on onUpdate? Does  getDataProvider(params); recreate it?
> > Then the instance stored on DataView is different from the one you
> > retrieve with getDataProvider(params)? Why not implement a
> > SortableDataProvider, keep a local copy of it, pass it to DataView and
> > update it's sort state on the onUpdate method?
> >
> > Ernesto
> >
> > On Thu, Apr 29, 2010 at 1:51 PM, Robert Kimotho <kimot...@gmail.com>
> wrote:
> >> I have experienced the same issue, the only difference is that
> >> when there was no data in the database the dropdown crashed and could
> >> not display any
> >> values, I'm also using AjaxFormComponentUpdatingBehavior onchange
> >>
> >> Here is a section of my code:-
> >>
> >> final WebMarkupContainer commentListContainer = new
> >> WebMarkupContainer("commentListContainer");
> >>        commentListContainer.setOutputMarkupId(true);
> >>
> >> final DropDownChoice<String> cats = new
> DropDownChoice<String>("cat_select",
> >> new PropertyModel<String>(new VComment(), "category")
> >> , category.getCategoriesByType("vcomment"), new
> ChoiceRenderer<String>());
> >> cats.setOutputMarkupId(true);
> >> add(cats);
> >>
> >> final AbstractDataProvider<VComment> dataProvider =
> (VCommentDataProvider)
> >> getDataProvider(params);
> >>
> >> final DataView<VComment> commentView = new
> DataView<VComment>("commentList",
> >> dataProvider, 10) {
> >>
> >> @Override
> >> protected void populateItem(Item<VComment> item) {.....}
> >>  }
> >> commentListContainer.add(commentView);
> >> ........
> >> cats.add(new AjaxFormComponentUpdatingBehavior("onchange") {
> >> @Override
> >> protected void onUpdate(AjaxRequestTarget target) {
> >> params.put("cat", cats.getModelObject());
> >> getDataProvider(params);
> >> target.addComponent(commentListContainer);
> >> target.addComponent(cats);
> >> }
> >> });
> >>
> >> Kimotho.
> >>
> >> On Thu, Apr 29, 2010 at 12:42 PM, Ernesto Reinaldo Barreiro <
> >> reier...@gmail.com> wrote:
> >>> Can you post the code that was failing somewhere? Maybe someone can
> >>> spot what is happening;-)
> >>>
> >>> Best,
> >>>
> >>> Ernesto
> >>>
> >>> On Thu, Apr 29, 2010 at 11:21 AM, Reinout van Schouwen
> >>> <rein...@gmail.com> wrote:
> >>>>
> >>>> Replying to myself.
> >>>>
> >>>> I've worked around the problem by not using the AJAX method to update
> my
> >>>> DataView but by reloading the page with different pageparameters after
> a
> >>>> selection instead. Far from ideal, but the best I could come up with.
> >>>>
> >>>> Alternative solutions still welcome!
> >>>>
> >>>> Op maandag 26-04-2010 om 14:20 uur [tijdzone +0200], schreef Reinout
> van
> >>>> Schouwen:
> >>>>> I'm having trouble figuring out the following problem.
> >>>>>
> >>>>> I have a div (coupled to a WebMarkupContainer) containing a form and
> a
> >>>>> list. The list is populated with a DataView, which gets its data from
> a
> >>>>> SortableDataProvider that I have implemented according to the
> repeater
> >>>>> examples provided on wicket-library.com.
> >>>>>
> >>>>> What I want to do is to change the sort order based on a user
> selection
> >>>>> from a DropDownChoice in the form. I've used an
> >>>>> AjaxFormComponentUpdatingBehavior to listen to the dropdown's
> onchange
> >>>>> event. In the onUpdate() method I change the sortparam in the
> >>>>> dataprovider according to the selection and then add the container
> div
> >>>>> to the AjaxRequestTarget.
> >>>>>
> >>>>> What I expect to happen is that the DataView updates to reflect the
> new
> >>>>> sort order. What actually happens is that the DataView completely
> >>>>> disappears from the page! (The Wicket Ajax Debug window shows me that
> >>>>> the spot where the list should be in the HTML within the
> ajax-response
> >>>>> is indeed empty.)
> >>>>>
> >>>>> I've already set the outputmarkupid and the
> outputmarkupplaceholdertag
> >>>>> properties on the container to true but that makes no difference.
> >>>>>
> >>>>> Any ideas are appreciated!
> >>>>>
> >>>>> regards,
> >>>>>
> >>>>
> >>>>
> >>>>
> >>>> ---------------------------------------------------------------------
> >>>> To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
> >>>> For additional commands, e-mail: users-h...@wicket.apache.org
> >>>>
> >>>>
> >>>
> >>> ---------------------------------------------------------------------
> >>> To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
> >>> For additional commands, e-mail: users-h...@wicket.apache.org
> >>>
> >>>
> >>
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
> For additional commands, e-mail: users-h...@wicket.apache.org
>
>

Reply via email to