paper-listbox
paper-listbox copied to clipboard
Multi-pre-select from template is either impossible or not documented
Description
Multi-pre-select from template is either impossible or not documented. In order to do multiple preselect "reinventing bycicle" through writing custom item populating code is required.
Expected outcome
Should be able to do something like
<paper-listbox multi>
<paper-item selected>Item 1</paper-item>
<paper-item>Item 2</paper-item>
<paper-item selected>Item 3</paper-item>
</paper-listbox>
Actual outcome
The only documented way which doesn't support multiple select (or if it does, that part is not documented at all)
<paper-listbox selected="0">
<paper-item>Item 1</paper-item>
<paper-item>Item 2</paper-item>
</paper-listbox>
Live Demo
https://elements.polymer-project.org/elements/paper-listbox?view=demo:demo/index.html&active=paper-listbox
Steps to reproduce
https://elements.polymer-project.org/elements/paper-listbox?view=demo:demo/index.html&active=paper-listbox
This is an example of how hard and hacky it gets without the feature (Dart):
//...userList was loaded from server via REST
//clear listbox items
PolymerDom listboxDom = Polymer.dom(userMultiChoice);
for (Node childNode in listboxDom.childNodes)
listboxDom.removeChild(childNode);
//recreate listbox items
List<int> selectedItems = new List<int>();
for (int i = 0; i < userList.length; i++) {
User user = userList[i];
for (String username in selectedUsernames) {//~~~~
if (username == user.username) {
selectedItems.add(i);
break;
}
}
PaperItem item = new PaperItem();
item.text = user.username;
listboxDom.append(item);
}
//apply selection
for (int item in selectedItems) {
userMultiChoice.select(item);
}
You need to set the attribute on multi listbox like this <paper-listbox multi selected-values="[1, 2]">
. This is described in the list of paper-listbox
properties.
Also see #19.
This issue can probably be closed since @fcFn has clearly stated the answer...
I have tried using paper-listbox multi selected-values="[1, 2]", and for it it only worked if the numbers are provided as String: ["1","2"]
Note that I use attr-for-selected="value"
with <paper-item value="0">AAA</paper-item>
etc