在Wicket中,如何使用Ajax分页复选框实现表单?

时间:2011-08-09 20:42:57

标签: java ajax wicket wicketstuff

在我的Wicket页面上,我有一个带有几个TextFields的表单和一个带有dataview和数据提供程序的分页复选框iplemenet列表。我有used this example of Checkgroup

我在下面列出了我的来源。问题是,当我在第1页上选择几个项目然后转到第2页然后使用AjaxPagingNavigator返回到第1页时,选择将丢失:第1页上检查的项目最初没有“复选标记” “再说了。当使用AjaxPagnigaion遍历列表时,我应该怎么做才能使页面上选择的项目不会丢失?

public final class AjaxFormWithPaginatedCheckBoxPage extends BasePage {

static Logger logger = Logger.getLogger(AjaxFormWithPaginatedCheckBoxPage.class);
private Person person = new Person();
int counter = 0;

WebMarkupContainer webMC;
static List<Person> pList=Person.getPersons(100); 
int maxListCount=5;
DataView dv;
CheckGroup group;
public AjaxFormWithPaginatedCheckBoxPage() {
    super();
    final Form form = new Form("form", new CompoundPropertyModel(person)) {

        protected void onSubmit() {
            logger.info("form recived");
            logger.info(" person name " + person.getName());

            setResponsePage(new AjaxTogglePanelTestPage());
        }
    };
    final TextField name = new TextField("name");
    final TextField age = new TextField("age");
    form.add(name);
    form.add(age);


    group=new CheckGroup("group", new ArrayList());        
    form.add(group);

    webMC= new WebMarkupContainer("webMC");
    webMC.setOutputMarkupId(true);

    //PersonDataProvider personDataProvider= new PersonDataProvider();
    SortablePersonDataProvider personDataProvider=new SortablePersonDataProvider(pList);
    dv = new DataView<Person>("persons", personDataProvider) {

        @Override
        protected void populateItem(Item<Person> item) {
            //AttributeModifier am = new AttributeModifier("src", true, item.getModel());
            //item.add(new Radio("radio", item.getModel()));
            item.add(new Check("check", item.getModel()));
            item.add(new Label("id", new PropertyModel(item.getModel(), "id")));                
            item.add(new Label("name", new PropertyModel(item.getModel(), "name")));                
            item.add(new Label("age", new PropertyModel(item.getModel(), "age")));                
        }


    };
    dv.setItemsPerPage(maxListCount);


    webMC.add(new AjaxFallbackOrderByBorder("orderByID", "id", personDataProvider)
    {
        @Override
        protected void onSortChanged()
        {
            dv.setCurrentPage(0);
        }

        @Override
        protected void onAjaxClick(AjaxRequestTarget target) {
            target.addComponent(webMC);
        }
    });

    webMC.add(new AjaxFallbackOrderByBorder("orderByName", "name", personDataProvider)
    {
        @Override
        protected void onSortChanged()
        {
            dv.setCurrentPage(0);
        }

        @Override
        protected void onAjaxClick(AjaxRequestTarget target) {
            target.addComponent(webMC);
        }
    });
    webMC.add(new AjaxFallbackOrderByBorder("orderByAge", "age", personDataProvider)
    {
        @Override
        protected void onSortChanged()
        {
            dv.setCurrentPage(0);
        }

        @Override
        protected void onAjaxClick(AjaxRequestTarget target) {
            target.addComponent(webMC);
        }
    });


    webMC.add(dv);        
    webMC.add(new AjaxPagingNavigator("navigator",dv));
    group.add(webMC);

    AjaxButton ab = new AjaxButton("ajax-button", new PropertyModel<String>(this,
            "counter"), form) {

        @Override
        protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
            counter++;
            logger.info("ajx form recived");
            logger.info("ajx person name " + person.getName());

            target.addComponent(this);

        }
    };
    form.add(ab);
    form.setDefaultButton(ab);

    add(form);        
}
}

好的 - 这是基于以下答案的更新和工作解决方案代码。感谢。

public final class AjaxFormWithPaginatedCheckBoxPage extends BasePage {
static Logger logger = Logger.getLogger(AjaxFormWithPaginatedCheckBoxPage.class);
private Person person = new Person();
int counter = 0;

WebMarkupContainer webMC;
static List<Person> pList=Person.getPersons(100); 
int maxListCount=5;
DataView dv;


Set<Person> selectedPersons= new HashSet<Person>();


public AjaxFormWithPaginatedCheckBoxPage() {
    super();
    final Form form = new Form("form", new CompoundPropertyModel(person)) {

        protected void onSubmit() {
            logger.info("form recived");
            logger.info(" person name " + person.getName());
            for(Person p: selectedPersons){
                logger.info(" selected "+ p.getName());
            }

            setResponsePage(new AjaxTogglePanelTestPage());

        }
    };
    final TextField name = new TextField("name");
    final TextField age = new TextField("age");
    form.add(name);
    form.add(age);


    //group=new CheckGroup("group", new ArrayList());        
    //form.add(group);
    /*
    group.add(new AjaxFormChoiceComponentUpdatingBehavior(){

        @Override
        protected void onUpdate(AjaxRequestTarget target) {
            logger.info(" AjaxFormChoiceComponentUpdatingBehavior invoked .. ");
             target.addComponent(webMC);

        }

    });
    */


    webMC= new WebMarkupContainer("webMC");
    webMC.setOutputMarkupId(true);


    //PersonDataProvider personDataProvider= new PersonDataProvider();
    SortablePersonDataProvider personDataProvider=new SortablePersonDataProvider(pList);
    dv = new DataView<Person>("persons", personDataProvider) {

        @Override
        protected void populateItem(final Item<Person> item) {


            //item.add(new  Check("check", item.getModel()));
            AjaxCheckBox ajc=new  AjaxCheckBox("check", new PropertyModel(item.getModel(), "id")) {

                @Override
                protected void onUpdate(AjaxRequestTarget art) {
                    logger.info("onupdate called");
                        String value=getValue();
                        if(value==null){
                            logger.info("dunno what to do ..");
                            throw new IllegalArgumentException(" ajax check box returned null");
                        }    

                        Person op=((Person)(item.getDefaultModelObject()));
                        logger.info("person "+ op.getName() +" was "+ person.isSelected() );
                            boolean bret= Strings.isTrue(value);
                            if(bret==false){
                                logger.info(" ... UN-selected");
                                //((Person)(item.getDefaultModelObject())).setSelected(false);
                                selectedPersons.remove(op);
                            }
                            else{
                                logger.info("....selected");
                                //((Person)(item.getDefaultModelObject())).setSelected(true);
                                selectedPersons.add(op);
                            }
                        }                                    

            };
            item.add(ajc);
            item.add(new Label("id", new PropertyModel(item.getModel(), "id") ) );                
            item.add(new Label("name", new PropertyModel(item.getModel(), "name")));                
            item.add(new Label("age", new PropertyModel(item.getModel(), "age")));                
        }


    };
    dv.setItemsPerPage(maxListCount);


    webMC.add(new AjaxFallbackOrderByBorder("orderByID", "id", personDataProvider)
    {
        @Override
        protected void onSortChanged()
        {
            dv.setCurrentPage(0);
        }

        @Override
        protected void onAjaxClick(AjaxRequestTarget target) {
            target.addComponent(webMC);
        }
    });

    webMC.add(new AjaxFallbackOrderByBorder("orderByName", "name", personDataProvider)
    {
        @Override
        protected void onSortChanged()
        {
            dv.setCurrentPage(0);
        }

        @Override
        protected void onAjaxClick(AjaxRequestTarget target) {
            target.addComponent(webMC);
        }
    });
    webMC.add(new AjaxFallbackOrderByBorder("orderByAge", "age", personDataProvider)
    {
        @Override
        protected void onSortChanged()
        {
            dv.setCurrentPage(0);
        }

        @Override
        protected void onAjaxClick(AjaxRequestTarget target) {
            target.addComponent(webMC);
        }
    });


    webMC.add(dv);        
    webMC.add(new AjaxPagingNavigator("navigator",dv));
  //  group.add(webMC);

    AjaxButton ab = new AjaxButton("ajax-button", new PropertyModel<String>(this,
            "counter"), form) {

        @Override
        protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
            counter++;
            logger.info("ajx form recived");
            logger.info("ajx person name " + person.getName());

            target.addComponent(this);

        }
    };

    form.add(webMC);
    form.add(ab);
    form.setDefaultButton(ab);

    add(form);        

}
}

1 个答案:

答案 0 :(得分:0)

每当您使用分页导航器离开页面时,您都不会提交表单,因此不会传输数据,因此不会更改模型,从而在返回时获得清晰的信息。最简单的方法是使用AjaxCheckbox es而不是常规的Checkbox。如果无法完成此操作,则需要更改分页链接以提交表单。