You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by JumpingIntoWicket <ac...@gmail.com> on 2017/09/03 21:13:40 UTC

Populating DataView on AJAX submit

Hi
I have a form that I add to it web markup container and attach  a data view
and then ajax button to a form.
I want to populate this DataView on ajax button onSubmit. How to do it
correctly?
I saw this answer on Stack Overflow which is almost exactly what I want but
it didn't solve my case.
https://stackoverflow.com/questions/24506426/wicket-dataview-refresh-with-ajaxbutton
Here comes the code.

List<Log> logs = new ArrayList<Log>();

		logListP = new ListDataProvider<Log>() {
			private static final long serialVersionUID = 1L;
			@Override
			protected List<Log> getData() {
				return logs;
			}
		};
		
		logListP = new ListDataProvider<Log>() {
			 protected List<Log> getData() {
			        return logs;
			    }
		};
		
		DataView<Log> dataView = new DataView<Log>("rows", logListP,
ITEMS_PER_PAGE) {
			private static final long serialVersionUID = 1L;

			@Override
			public void populateItem(final Item<Log> item) {
				Log log = item.getModelObject();
				item.add(new Label("id", log.getId()));
				item.add(new Label("id2", log.getSize()));
				item.add(new Label("id3", log.getCheckSum()));
			}
		};

		WebMarkupContainer wmc = new WebMarkupContainer("container");
		dataView.setOutputMarkupId(true);
		wmc.setOutputMarkupId(true);
		wmc.add(dataView);

		AjaxButton buttonReadDtc = new AjaxButton("buttonRead") {

			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				if (target != null) {
					try {
					List<Log> logs = getLogFilesData();
					logListP = new ListDataProvider<Log>(logs);
					} catch (IOException e) {
						e.printStackTrace();
					}
					target.add(wmc);
				}
			}
		};

		form.add(wmc);
		form.add(buttonRead);

--
Sent from: http://apache-wicket.1842946.n4.nabble.com/Users-forum-f1842947.html

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Populating DataView on AJAX submit

Posted by Maxim Solodovnik <so...@gmail.com>.
I would recommend 2 changes:

<<<<< List<Log> logs = new ArrayList<Log>();
>>>>> final List<Log> logs = new ArrayList<Log>();

<<<<< List<Log> logs = getLogFilesData();
<<<<< logListP = new ListDataProvider<Log>(logs);
>>>>> logs.clear(); logs.addAll(getLogFilesData());

I believe this will do the trick

On Mon, Sep 4, 2017 at 4:13 AM, JumpingIntoWicket <ac...@gmail.com> wrote:
> Hi
> I have a form that I add to it web markup container and attach  a data view
> and then ajax button to a form.
> I want to populate this DataView on ajax button onSubmit. How to do it
> correctly?
> I saw this answer on Stack Overflow which is almost exactly what I want but
> it didn't solve my case.
> https://stackoverflow.com/questions/24506426/wicket-dataview-refresh-with-ajaxbutton
> Here comes the code.
>
> List<Log> logs = new ArrayList<Log>();
>
>                 logListP = new ListDataProvider<Log>() {
>                         private static final long serialVersionUID = 1L;
>                         @Override
>                         protected List<Log> getData() {
>                                 return logs;
>                         }
>                 };
>
>                 logListP = new ListDataProvider<Log>() {
>                          protected List<Log> getData() {
>                                 return logs;
>                             }
>                 };
>
>                 DataView<Log> dataView = new DataView<Log>("rows", logListP,
> ITEMS_PER_PAGE) {
>                         private static final long serialVersionUID = 1L;
>
>                         @Override
>                         public void populateItem(final Item<Log> item) {
>                                 Log log = item.getModelObject();
>                                 item.add(new Label("id", log.getId()));
>                                 item.add(new Label("id2", log.getSize()));
>                                 item.add(new Label("id3", log.getCheckSum()));
>                         }
>                 };
>
>                 WebMarkupContainer wmc = new WebMarkupContainer("container");
>                 dataView.setOutputMarkupId(true);
>                 wmc.setOutputMarkupId(true);
>                 wmc.add(dataView);
>
>                 AjaxButton buttonReadDtc = new AjaxButton("buttonRead") {
>
>                         private static final long serialVersionUID = 1L;
>
>                         @Override
>                         protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
>                                 if (target != null) {
>                                         try {
>                                         List<Log> logs = getLogFilesData();
>                                         logListP = new ListDataProvider<Log>(logs);
>                                         } catch (IOException e) {
>                                                 e.printStackTrace();
>                                         }
>                                         target.add(wmc);
>                                 }
>                         }
>                 };
>
>                 form.add(wmc);
>                 form.add(buttonRead);
>
> --
> Sent from: http://apache-wicket.1842946.n4.nabble.com/Users-forum-f1842947.html
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>



-- 
WBR
Maxim aka solomax

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org