-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[MdAutocomplete] options with objects. #1243
Comments
Is there a workaround for this? |
Bump. I had to resort to using another autocomplete lib because I couldn't workaround this, but would rather use md-autocomplete if this were possible |
solution: |
@rkakrik your solution did not work for me. The @md-selected="onSelect" will grab the right value (object), but md-autocomplete component still throws errors if the objects in your list are not "stringy" ... so you have to implement .toLowerCase and .toString on each (or maybe you are already working with "stringy" objects - I wasn't). For example (hacky/ugly, but works, and maintained re-usability for me to just put the object mapping directly in md-options): <md-autocomplete
v-model="SelectDialog.SelectedItem"
:md-options="SelectDialog.SelectList.map(x=>({
'Id':x.Id,
'Name':x.Name,
'toLowerCase':()=>x.Name.toLowerCase(),
'toString':()=>x.Name
}))"
>
<label>Select</label>
<template slot="md-autocomplete-item" slot-scope="{ item, term }">
<md-highlight-text :md-term="term">{{ item }}</md-highlight-text>
</template>
<template slot="md-autocomplete-empty" slot-scope="{ term }">
"{{ term }}" not found!
</template>
<div class="md-helper-text" v-if="SelectDialog.SelectedItem">
<strong>Item:</strong>{{SelectDialog.SelectedItem.Name}} ({{SelectDialog.SelectedItem.Id}})
</div>
</md-autocomplete> |
Perhaps something fancy can be done with this type check and the behavior of isinstanceof used by vue prop type checks to catch linting or compile-time errors. |
... the silly thing is that the native I'd suggest leveraging that somehow with a slot. The v-model should be the selected OBJECT, and the text should come from selected object's "label" (defined by slot)? Not sure if it's possible. Alternatively, you can add a prop for one to pass a "labeler" function (the default being .toString). Labeler function is evaluated to show list items and set text in text box. V-model would still hold ref to selected OBJECT, not the label. |
@fizxmike your solution partially worked for me, instead of the item name showing it was showing the entire object. My workaround was as follows: <md-autocomplete
v-model="selectedLabel"
:md-options="getSanitizedLabels">
<label>Label</label>
<template slot="md-autocomplete-item" slot-scope="{ item, term }">
<md-highlight-text :md-term="term">{{ item.Name }}</md-highlight-text>
</template>
</md-autocomplete> computed: {
getSanitizedLabels() {
return this.labels.map(label => ({
'Id': label.id,
'Name': label.name,
'toLowerCase': () => label.name.toLowerCase(),
'toString': () => label.name
}));
}
} Although this works for me, it gives an error message when selecting an item: |
Yea... this, and other issues re-inventing (bad) wheels, have "drove" me to Angular (TypeScript) with Angular Material components. Google's "wheels" tend to work just fine (most of the time). |
Hi all, I'm facing the same issue, solution provided by @DavidHtx and @fizxmike works but both partially, as in it works passing object and remaping using the promise, but it will still throw : [Vue warn]: Invalid prop: type check failed for prop "mdTerm". Expected String, got Object. Any idea when this will be fixed or if it will be fixed? |
@vzapo try add
|
|
Any news with this issue? Including autocomplete on documentation website is not working properly with objects. (See Custom template with highlight text, where you cannot select an option and then everything breaks) |
I resorted to not using v-model, and using :value and @input and handling it myself. However now the issue is that the value prop is not watched, so when I'm updating it manually (resetting it to empty basically after I did my thing) it's not resetting also on the component. So still unusable. If this would work, at least we'd have that way of working around this bug |
Separate object field and search term, change search term onSelected, it works! If you read the source code of MdAutocomplete component you will find v-model is bind to "searchTerm". The author means to bind the search keyword to v-model of this input rather than the result string/object it self. So if we follow this idea, use xxxSearchTerm, onSelected trigger to update our actual data. Just like @rkakrik did, but you need to bind another searchTerm to v-model, not the form field it self. For the clear form issue, I don't think it's a problem. Because whether the searchTerm is part of form or not is just your choice. In my use case, I just put the search term out of form. <md-autocomplete
v-model="customerSearchTerm"
:md-options="customers"
@md-changed="getCustomers"
@md-opened="getCustomers"
@md-selected="selectCustomer"
>
<label>Customer</label>
<template slot="md-autocomplete-item" slot-scope="{ item }">
{{ item }}
</template>
</md-autocomplete>
data() {
return {
booking: { id: "", customer: {} },
customers: [],
customerSearchTerm: ""
};
}, methods: {
async getCustomers(q) {
this.customers = (await User.get({ keyword: q })).body;
},
selectCustomer(c) {
this.booking.customer = c;
this.customerSearchTerm = c.name; // !!!here's the thing!!!
}
},
async mounted() {
if (this.$route.params.id !== "add") {
this.booking = (await Booking.get({ id: this.$route.params.id })).body;
this.customerSearchTerm = this.booking.customer.name; // !!!here's the thing!!!
}
} |
This seems to work for me without any error messages! Only issue is that when I select an item the auto complete drop down stays open! Which I am sure is an easy fix! |
@uicestone After I search and select an option from the option list for the first time, I remove the search term and try again, this time option list do not show up. Any suggestion would be appreciated. |
@anurat probably a bit late, but I was able to make it work adding the following
If I return the same value for toLowerCase() and toString() the dropdown is shown only once... I didn't investigate the root cause, but this workaround works fine |
As #1215, I fixed by #1218 to make
searchTerm
sync withv-model
, but that also make it crash while the options are objects. It's could reproduce in documentation example Custom Template - With highlight text with 6f51eb9 (and it's a bug now in beta 6). If I want to make #1215 support object, I should know what to show assearchTerm
. I think it's not easy becauseMdAutocomplete
get the plain text viaNode.textContent
.As #1241, I think users prefer that the result is the thing shown on the
searchTerm
not the thing they select via autocomplete. It's good if I could still get the object while I type thesearchTerm
and match a statement.To sum up, I want to figure out a way to bind the option object and the plain text for the object.
Another issue is that
MdIcon
cannot be used inmd-autocomplete-item
slots or thesearchTerm
will show the text inMdIcon
after selected.Any idea?
The text was updated successfully, but these errors were encountered: