0

Saving current selection with paging

asked 2023-07-04 21:08:16 +0800

rmalik gravatar image rmalik
31 2

Hi,

We use a listbox with the parameter checkmark=true and multiple=true. On enabling these settings we get a horizontal bar with the option check multiple rows on our page:

image Checkboxes

At the moment we want to store our selection of items (items.getSelection()) and while paging from one page to another, we want to display it. Items has a datatype of ListModelList containing objects to display to the user.

This is the use case that i have:

  • On page 1: I select all checkboxes (22 in total)
  • I navigate to page 2 where i only mark the first row checked.
  • I navigate back to page 1, where i expect all my 22 rows already checked.

In my .zul file i call the following method:

<listbox model="@load(vm.items)" onselect="@command('select')"/>

In the select @command method i store my current selection:

getItemsSelection().put( getActivePage(), new HashSet(getItems().getSelection()) );

getItemsSelection is a Linked HashMap containing the pagenumber as key and Set as value.

Please note that i use a new HashSet(), since the getItems().getSelections() get cleared after i move to a new page.

Now when i navigate back and forth, if i return to the first page i set the selection manually:

getItems().setSelection( getItemsSelection().get( getActivePage() ) );

I can confirm that i set the 22 previous selected object back again on the listmodellist.

The only thing that i am missing is that my overview of page 1 that it does not show my checkboxes checked.

image Checkboxes

Can you please help us further?

Kind regards,

Remie

delete flag offensive retag edit

7 Answers

Sort by ยป oldest newest most voted
0

answered 2023-07-14 22:54:24 +0800

rmalik gravatar image rmalik
31 2

Hi,

I tried to search further. It appears that when i call getItems().getSelection() is empty at the end. Maybe that getItems().setSelection() does not keep track of its changes?

link publish delete flag offensive edit
0

answered 2023-07-14 23:30:28 +0800

rmalik gravatar image rmalik
31 2

Hi,

I found the issue.

I see that ZK calls this method in AbstractModelList class

public void clearSelection() { if (!this.selection.isEmpty()) { this.selection.clear(); this.fireEvent(4, -1, -1); } }

Which flushes my saved selection.

Please tell me how can i prevent this?

link publish delete flag offensive edit
0

answered 2023-07-18 11:16:57 +0800

hawk gravatar image hawk
3185 1 5
http://hawkphoenix.blogsp... ZK Team

when you switch the active page, Listbox still keeps your selection in the Model. Why do you want to clear the selection when switching to the next page?

which zk version do you use?

I try to simulate your case (in a simplified way) at https://zkfiddle.org/sample/32tfdlr/1-saving-current-selection-with-paging but cannot reproduce the issue you mentioned.

could you provide a reproducible case at zkfiddle?

link publish delete flag offensive edit
0

answered 2023-07-18 19:00:10 +0800

rmalik gravatar image rmalik
31 2

Hi Hawk,

We are using ZK version 9.6.0.2

I updated the fiddle with my code (use-case):

Fiddle example

It seems to be working, as expected. When i switch back to my code base (company application) i try the same strategy for keeping track of the selection, it does not work.

I do not know why, but after running the code (in my company application):

model.setSelection( selection );

In AbstractModelList.class (ZK class) the following method gets executed:

public void clearSelection() { if (!this.selection.isEmpty()) { this.selection.clear(); this.fireEvent(4, -1, -1); } }

This methods removes my previous selection. If i try to debug the method i can only see that the stack is coming from ZK:

Strack trace part 1

Stack trace part 2

Do you have any more ideas?

Kind regards,

Remie

link publish delete flag offensive edit
0

answered 2023-07-18 19:24:50 +0800

rmalik gravatar image rmalik
31 2

updated 2023-07-18 19:25:46 +0800

Hi Hawk,

I think i found it.

I can reproduce my issue at:

ZK updated fiddle

Notice that i use the setting in my .zul file:

selectedItem="@load(vm.selected")

This causes my listbox to reset its selection. I really need this functionality too. How can i prevent this behaviour?

link publish delete flag offensive edit
0

answered 2023-07-24 18:39:53 +0800

hawk gravatar image hawk
3185 1 5
http://hawkphoenix.blogsp... ZK Team

Since you have controlled the selection with model API like model.setSelection(), you should not bind on selectedItem. This binding will change the selected item again based on vm.selected

I fixed the code: https://zkfiddle.org/sample/32tfdlr/4-saving-current-selection-with-paging-remie

link publish delete flag offensive edit
0

answered 2023-09-12 17:51:54 +0800

rmalik gravatar image rmalik
31 2

Thanks for the explanation. We fixed it like in your example and it works as expected.

Much appreciated.

Kind regards,

Remie

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2023-07-04 21:08:16 +0800

Seen: 13 times

Last updated: Sep 12

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More