Value being used as label on selectcomplete
See original GitHub issueI have a JSON endpoint that’s returning data in the following format:
[{
id: 1,
name: "Bob"
}]
And I’m using the following code:
window.addEventListener("awesomplete-selectcomplete", function(e) {
window.location.href = '/my-path/' + e.text.value;
}, false);
var ajax = new XMLHttpRequest();
ajax.open("GET", "/data.json", true);
ajax.onload = function() {
new Awesomplete(document.querySelector("#search"), {
list: JSON.parse(ajax.responseText).map(function(i) { return i; }),
data: function (item, input) {
return { label: item.name, value: item.id };
}
});
}
ajax.send();
When I select an option, the value (id) is showing up in the textbox rather than the label (name). I’d still like it to use the label, just to pass the id through as the value to the listener. Is there some way to do this?
Issue Analytics
- State:
- Created 6 years ago
- Comments:11 (2 by maintainers)
Top Results From Across the Web
Awesomplete set label text field and hidden field value
I have an array with label and value, and when something is selected I wan't it to compleate the text and ad the...
Read more >How to Customize an Awesomplete Text Input's Functionality ...
My first thought was to create a variable to store the value of the text input as soon as it had been selected...
Read more >Usage Documentation - Tom Select
The name of the property to use as the value when an item is selected. string, 'value'. optgroupValueField, The name of the option...
Read more >6.3 Labels as Values - GCC, the GNU Compiler Collection
*/ ptr = &&foo;. To use these values, you need to be able to jump to one. This is done with the computed...
Read more >Awesomplete - Lea Verou
getElementById("myinput"); // Show label but insert value into the input: new ... listLabel, data-listlabel, Denotes a label to be used as aria-label on...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@jfi @joezsweet @LeaVerou If I understood correctly what needs to be done, no need for fixes to Awesomplete.
What’s inserted into the input is controlled by the
replace
method. By default it insertsvalue
, you’ll need to provide a custom method which will insertlabel
instead:See demo here: https://jsfiddle.net/1agrns7n/
I feel like the current default behavior for this is very counter intuitive. A combobox is supposed to mimic the behavior of a select with added features. Ideally, a user who doesn’t use the added features should not even realize it isn’t a normal select. A select uses the value as an underlying id of sorts that you can get and send to the server. As such, the value in Awesomplete should be underlying while the label is put in the input box, just like a select.
The difficulty is Awesomplete is an autocomplete first and a combobox second, meaning there is no current functionality to track a selected index. Ideally, when you select an item in the list, or just fully type out an item so that it matches a value in the list, there should be an underlying index and possible related value stored within the Awesomplete. If the input does not match anything in the list, the index should be -1 or false. This might represent a significant change though, so maybe there’s an easier way to do it.
For example, we could add an id field or maybe we allow users to attach whatever they want in each data object instead of just label and value. Then, they can use
awesomplete-selectcomplete
to pop it in a hidden object. The two problems with this approach though are:If the user types an entire option without selecting it, it should still get the id on blur. This means there would need to be a function to call to see if the current value in the input matches any labels in the autocomplete list (which there probably should be anyway, for cases such as when you want to enforce that the final value matches something in the list). The function would need to return the data object so you could get the id value of the matched element.
This represents a sizeable amount of extending for what seems like it should be built in if Awesomplete is to support combobox functionality. You would have to use
awesomplete-selectcomplete
to store the value and attach another onBlur event to check if the typed value is in the list. As @jfi mentioned, this is behavior that is expected, which means many people might turn away from Awesomplete without even realizing this functionality could be implemented. An extended combobox example in the docs could help.