Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autocomplete doesn't work #45

Open
vishal-parameswaran opened this issue Dec 15, 2017 · 2 comments
Open

Autocomplete doesn't work #45

vishal-parameswaran opened this issue Dec 15, 2017 · 2 comments

Comments

@vishal-parameswaran
Copy link

How would I go about getting a functionality similar to the function of a standard autocomplete, which would automatically update the dropdown based on what you enter. In the current method, any data entered in the input is lost to the plugin as it simply pulls all data from the array and displays them.
My current function
var multiple = $('#multipleInput').materialize_autocomplete({ multiple: { enable: true }, limit:5, appender: { el: '.ac-users' }, dropdown: { el: '#multipleDropdown' }, getData: function (value, callback) { // ... callback(value, data); } });

@settenop
Copy link

If I've understood correctly you aren't filtering your data!
You can do something like this:

var multiple = $('#multipleInput').materialize_autocomplete({
    multiple: {
        enable: true
    },
    appender: {
        el: '.ac-users'
    },
    dropdown: {
        el: '#multipleDropdown',
        itemTemplate: '<li class="ac-item" data-id="<%= item.id %>" data-text=\'<%= item.text %>\'><a href="javascript:void(0)"><%= item.highlight %></a></li>'
    },
    getData: function(value, callback) {
        var testArray = ["i am a value", "i am another value", "i am the final ultimate mega value"];
        var data = testArray.reduce(function(acc, curr, i, arr) {
            if (curr.toUpperCase().indexOf(value.toUpperCase()) != -1) {
                acc.push({
                    id: i,
                    text: curr,
                    highlight: curr.toUpperCase().replace(
                        value.toUpperCase(), "<b>" + value.toUpperCase() + "</b>")
                });
            }
            return acc;
        }, []);
        callback(value, data);
    }
});

@yagami271
Copy link

yes you should filter your data

getData: function (value, callback) {
    	    	//yourObject => [{id:10,text:'toto'}]
    	    	data = yourObject.filter(function(el){
    	    		return el.text.toLowerCase().indexOf(value.toLowerCase())> -1;
    	    	})
    	    	console.log(data);
    	        callback(value, data);
    	    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants