PolymerElements / iron-selector

Manages a list of elements that can be selected
32 stars 55 forks source link

Can't select index when using attr-for-selected #87

Closed graeme44 closed 8 years ago

graeme44 commented 8 years ago

Firstly, I understand and acknowledge that it is by design that if attr-for-selected is not null then the selected property is no longer the index and becomes the attr-for-selected variable.

Now, I came across this issue when using the paper-tabs element and the attr-for-selected property.

The context:

<paper-tabs id="tabs" selected="{{selected}}" attr-for-selected="someObject">
    <template is="dom-repeat" items="{{AjaxData.data}}">
        <paper-tab someObject$="{{item}}">{{item.name}}</paper-tab>
    </template>
</paper-tabs>

Where AjaxData would be something like:

{
  "data": [
    {
      "name": "Foo",
      "image_url": "/images/foo.png"
    },
    {
      "name": "Bar",
      "image_url": "/images/bar.png"
    }
}

I use the dictionary like object in the attr-for-selected as it can then be used in js to do some cool stuff:

var tabs = document.querySelector('#tabs');
tabs.addEventListener('iron-activate', function(e) {
    console.log(e.detail.selected);
    var jsonData = JSON.parse(e.detail.selected);
    var url = jsonData['image_url'];
    // Now we can set some ajax.url = url
}

Here is where the problem starts, I am using page.js to do client side routing - every time the user selects the page which has the paper-tabs, I would like the tabs to be 'reset' and the first tab to be selected.

One can no longer use .selected=0 and no knowledge is known of what the attr-for-selected is for the first (dynamically inserted) element.

It would be really useful to have a selectIndex(value) function (like the selectNext() and selectPrevious() functions). This function could just use the _indexToValue(index) to get the corresponding value for the given index (so that attr-for-selected will keep .selected happy). This solves the problem of someone still needing to address an index while using the attr-for-selected property.

cdata commented 8 years ago

It seems like this is a summary of the proposal: add a selectIndex method that lets the user select a value by index. How does that sound to you?

bicknellr commented 8 years ago

@cdata: @graeme44's PR: #88.

graeme44 commented 8 years ago

@cdata that sounds great to me. I put it all in a PR: #88.

MeTaNoV commented 8 years ago

would be nice to get this in master and eventually released! :)

graeme44 commented 8 years ago

@MeTaNoV this was merged a while back - see #110 (into branch issue-87). I see that hasn't been merged into master yet @bicknellr.

bicknellr commented 8 years ago

Thanks for the ping; #119 is out to bring this into master.