-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Allow accessing attributes of the data object associated with a typeahead field #114
Comments
When would you be accessing |
I want the Perhaps if there was an event triggered when the user accepted a suggestion that exposed how the suggestion was made (clicked, keyboard enter, keyboard arrow) as well as the query text, the I'm working on a (very dirty!) fix right now—in the utils.each(suggestions, function(i, suggestion) {
console.log(suggestion);
elBuilder.innerHTML = dataset.template.render(suggestion);
el = elBuilder.firstChild;
el.setAttribute("data-value", suggestion.value);
// My code
el.setAttribute("data-locationid", suggestion.locationid);
// ----
fragment.appendChild(el);
}); And then keeping it along for the ride in function formatDataForSuggestion($suggestion) {
var $suggestions = $suggestion.parents(".tt-suggestions").first();
return {
value: $suggestion.data("value"),
query: $suggestions.data("query"),
dataset: $suggestions.data("dataset"),
// My code
locationid: $suggestion.data("locationid"),
---
};
} I'm off to play a bit in the various suggestion handlers (under cursor, when clicked, etc) to grab the ID and then pass it to a function that will consume the ID and use it to update the display. I suppose the desired functionality I'm really looking for (which might not be in scope of the control itself) would be to have a field where users are forced to make a selection from the closed set of options within it -- and to be able to be aware of all of the attributes of the option that was selected. I hope I did an OK job explaining this and that my hack didn't cause you to break out in any hives. Let me know if I can provide more data or a more concrete use case. |
Seems like #68 would be of interest to you. Keep in mind that the purpose of typeahead.js is to improve the usability of search inputs – it wasn't built to be a substitute for a select menu. |
I'm going to close this as the |
I'm creating a location auto select field, but would like to be able to access the underlying ID:
I need to collect the ID of the item, while displaying a different value to the user.
Would be a handy addition (especially for location auto completes!)
The text was updated successfully, but these errors were encountered: