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

Using autocomplete with array of objects #364

Closed
renanmoraes opened this issue Sep 1, 2017 · 1 comment
Closed

Using autocomplete with array of objects #364

renanmoraes opened this issue Sep 1, 2017 · 1 comment

Comments

@renanmoraes
Copy link

renanmoraes commented Sep 1, 2017

I have the following array objects:

captura de tela de 2017-09-01 08-31-39

I need when typing the name of the city it will offer me the ones that correspond with the typed one. With standard array {"Mines": null, "River": null} works perfectly.

I try to do it that way but it really does not work.

  private FindaAllCities() {
    this._user
      .getToken()
      .subscribe(res => {
        this._cities
          .findAll(res.data.token)
          .subscribe(response => {
            this.autocompleteInit = {
              'data': response.data,
              onAutocomplete: (val) => {
                console.log(val);
              },
            };
            console.log(this.autocompleteInit);
            this.cities = response.data;
          });
      });
  }

In reality, if I can get the id informed when it passes data {"Mines": "123456"} would serve me. When I do this, it returns me an error because I think I'm sending an image. I need to know when I clicked the _id of the clicked face.

@rubyboy
Copy link
Contributor

rubyboy commented Sep 2, 2017

@renanmoraes , unfortunately the MaterializeCSS framework only supports passing the text and getting it back in the onAutocomplete callback: http://materializecss.com/forms.html#autocomplete.

What I suggest you do is map your id (or original object) to the name, and get it using the value. Something like this:

const nameToObject = {};
this.autocompleteInit = {
  'data': {},
  onAutocomplete: (val) => {
    console.log(nameToObject[val]);
  },
};
data.forEach(o => this.autocompleteInit.data[o.name]=null);
data.forEach(o => nameToObject[o.name]=o);

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

2 participants