-
Notifications
You must be signed in to change notification settings - Fork 610
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
Capability to differentiate value from displayed text #593
Comments
👍 Can we save option values inside list array? |
Yes, there definitely should there be a better way. At the very least, we should utilize the |
👍 I would really like to be able to do this too. |
For |
So, when the user selects the autocomplete suggestion, does the displayed text get filled in or the value? |
Hi there, 👍 for this one, I'm having the same problem. I have a URL in a 'data-link' attribute (ie http://img.saika.li/a3EG) and needs to catch this attribute when an is selected. Unfortunately, the event only returns the input with : I really hope that'll be implemented one day ! Anyway, big thanks for the plugin :) |
@psaikali I have no idea what you just said. I don’t understand, are you answering my question above? If so, what's the answer? And what do you mean with the event? What should be implemented one day? |
Sorry @LeaVerou. I was just saying "+1" to support the need for this feature ;) Just used the same trick as +ThomasG77 above.
|
Same here: out of habits of writing |
A change suggestion that solves this, while making the library even more well rounded: I took a different approach, but it got to a dead end which I think can be easily relieved with a change to the library. My approach does not imply using hidden attributes or such. I override That works for all mentioned hook functions.
But it only fails with Is this an api change? I think (?!!) all existing user code that assumes the argument to be a string, should still work. Forgot to mention, I second this is a great library! Maybe this suggestion just plugs a hole in the otherwise quite perfect customization api. What do you think? |
I came up with a similar solution:
// sample data
var targetCountryInput = document.getElementById('target-country-input');
var countryNames = [{iso: "AFG",name: "Afghanistan"},{iso: "AGO", name: "Angola"}];
new Awesomplete(targetCountryInput, {
list: countryNames,
autoFirst: true,
filter : function(text, input) {
return Awesomplete.FILTER_CONTAINS(text.name, input);
},
item: function (text, input) {
return Awesomplete.$.create("li", {
innerHTML: text.name.replace(RegExp(Awesomplete.$.regExpEscape(input.trim()), "gi"), "<mark>$&</mark>"),
"data-iso": text.iso, // encode my custom field into the generated LI
"aria-selected": "false"
});
}
}); Two things happened here: Override filter : function(text, input) {
return Awesomplete.FILTER_CONTAINS(text.name, input);
} In "data-iso": text.iso, // encode my custom field into the generated LI This gets you to where @matanster got in the post above. At this point, you do have to modify Awesomplete's
select: function (selected) {
selected = selected || this.ul.children[this.index];
if (selected) {
var prevented;
$.fire(this.input, "awesomplete-select", {
text: selected.textContent,
preventDefault: function () {
prevented = true;
}
});
if (!prevented) {
this.replace(selected.textContent);
this.close();
$.fire(this.input, "awesomplete-selectcomplete", selected.dataset); // add new selected.dataset parameter
}
}
}, The update happens on line 208: $.fire(this.input, "awesomplete-selectcomplete", selected.dataset); // add new selected.dataset parameter This simply passes the Then back in targetCountryInput.addEventListener('awesomplete-selectcomplete',function(e) {
var selectedCountryName = e.target.value;
var iso = e['iso']; // got my iso back.
}); |
Thanks, will take this into consideration next time around. |
it would be nice if it just worked with things correctly. If my data list looks like
The W3C spec says the form should be using "1", "2", "3", "4" etc. But awesomeplete seems to flips this around and actually ignores the value that the spec says should be used. Could we just get it fixed so that "form submission" uses the value like the spec wants, and awesomeplete uses the text value like it currently does? Something like #16793 |
Agree that if the user provides a select menu or datalist with different values and labels, awesomplete should take advantage of that. |
Tell me please, when you planing the commit? |
@nixprosoft There is an opened PR and ongoing discussion with @LeaVerou on it. Once this PR is merged, there will be support for having value different from displayed text. After that I plan adding support for initialization from |
To everyone interested in differentiating value from displayed text, aka key/value feature, aka array of objects feature. The code is ready and it's almost 100% backward compatible. To ensure everything working fine for you we need your help with testing it in the wild before it's released. See this PR: Separate label/value for each suggestion in list |
Separate Read about different |
Your library is cool but maybe due to the habit of
<select><option>
couple for sending identifier, I would like to use a value that differs from the text when event 'awesomplete-selectcomplete' is fired.Another reason could be the fact, that I want to avoid encoding issue. With accents (I'm French), I don't want to bother about encoding client/server when sending info (although nowadays, everyone use UTF-8, in my dreams...) so an identifier is better IMO.
I don't know if it's a design choice but I didn't see any ways to achieve it.
To try to bypass the lib behaviour cleanly, I created the following example below
Unfortunately, it echoed
Python
but nevera
when choosing the first option.I could always do the below really dirty solution but I would prefer to avoid it.
What is the way to go for my requirements if any?
Thanks for your input.
The text was updated successfully, but these errors were encountered: