paper-listbox icon indicating copy to clipboard operation
paper-listbox copied to clipboard

Multi-pre-select from template is either impossible or not documented

Open codekrolik opened this issue 8 years ago • 4 comments

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

codekrolik avatar Dec 21 '16 02:12 codekrolik

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);
    }

codekrolik avatar Dec 21 '16 02:12 codekrolik

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.

fcFn avatar Nov 22 '17 13:11 fcFn

This issue can probably be closed since @fcFn has clearly stated the answer...

kylefarris avatar Feb 09 '18 19:02 kylefarris

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

yglodt avatar Feb 20 '18 13:02 yglodt