polymer paper-input html datalist autocomplete/suggestions list

6.2k views Asked by At

so i'm using a simple autocomplete method in polymer with

<paper-input autocomplete="on" name="stuff" list="stuffs"></paper-input>

with a simple list of items (tried both select and template elements to do this)

    <datalist id="stuffs">
        <option value="blah blah"/>
            .
            .
            .
    </datalist>

thing is, i wanna figure out a way to access the dynamic dropdown list of suggestions that appears during typing. is there actually a way for this?

2

There are 2 answers

2
miyamoto On BEST ANSWER

I think your best bet then is to not use datalist.

Here's an example of a component I wrote for my own use:

<dom-module id="paper-autocomplete">
    <style>
    iron-collapse {
        box-shadow: 6px;
    }

    paper-button {
        width: 100%;
        text-transform: none;
    }
    </style>
    <template>
        <paper-input-container>
            <label>{{label}}</label>
            <content select=".content"></content>
            <input id="searchBox" class="paper-input-input" is="iron-input" bind-value="{{searchValue::input}}"></input>
        </paper-input-container>
        <iron-collapse id="collapse">
            <paper-material>
                <div>
                    <template id="resultList" is="dom-repeat" items="{{choices}}" filter="_listFilter">
                        <paper-item>
                            <paper-button on-tap="_selectItem">{{item.name}}</paper-button>
                        </paper-item>
                    </template>
                </div>
            </paper-material>
        </iron-collapse>
    </template>
</dom-module>
<script>
(function() {
    Polymer({
        is: "paper-autocomplete",
        properties: {
            choices: Array,
            label: String,
            value: {
                type: Object,
            },
            searchValue: {
                type: String,
                value: '',
                observer: "_valueChanged"
            }
        },
        ready: function() {
            this.$.resultList.render()
        },
        _valueChanged: function(e) {
            var collapse = this.$.collapse
            if (e != '' && !collapse.opened) {
                this.$.resultList.render()
                collapse.toggle()
            } else
            if (e == '' && collapse.opened) {
                collapse.toggle()
            }
        },
        _listFilter: function(item) {
            return item.name.toLowerCase().includes(
                this.searchValue.toLowerCase()
            )
        },
        _selectItem: function(event) {
            var collapse = this.$.collapse;
            this.set('searchValue', event.model.item.name)
            this.set('value', event.model.item)
            collapse.toggle()
        }
    })
})()
</script>

_valueChanged observes changes to searchValue on input and toggles the collapse. _listFilter filters the items bound to dom-repeat based on searchValue.

The #resultList or #collapse element could be placed elsewhere for your own purposes.

edit

I should point out that choices here is an Array of Objects each of which has a name attribute which is how they are filtered. It could easily be altered to filter on a Array of Strings depending on what you're trying to achieve.

0
Marcus Hellberg On

If you prefer a ready made Polymer element, I suggest looking at https://vaadin.com/elements/-/element/vaadin-combo-box