question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

How to provide distinct values and labels?

See original GitHub issue

Hello, I retrieve autocomplete data from a remote endpoint and populate results using populateResults:

    accessibleAutocomplete({
      element: el,
      id: "search_where",
      name: "where",
      source: debounce(async (query, populateResults) => {
        const res = await fetch(`${url}?q=${encodeURIComponent(query)}`);
        const data = await res.json();
        populateResults(res.results);
      }, 100),
    });

The res.results list received is of this form:

{
    "q": "pari",
    "results": [
        {
            "id": "75",
            "text": "Paris (dƩpartement 75)"
        },
        {
            "id": "75056",
            "text": "Paris (75)"
        },
        // [ā€¦]
    ]
}

I would like to render the text property value as the option label, but retrieve the id value when an option is selected by the user (a bit like what is possible using a regular <select> and <option value="42">Answer</option>). What data structure should I pass to populateResults to achieve that, if thatā€™s even possible?

Thanks for your help!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
faithandrelcommented, Mar 31, 2021

My solution in React:

  const find = (key, array) => {
    return array.filter(item => item.text.toLowerCase().indexOf(key) !== -1);
  }

  const source = (query, populateResults) => {
    const cities = [
      {id: 1, text: 'Paris'},
      {id: 2, text: 'London'},
      {id: 3, text: 'Berlin'},
      {id: 4, text: 'Vienna'},
    ];
    const results = find(query, cities);
    populateResults(results);
  }

  const templates = {
    inputValue: (value) => {
      if (!value) {
        return '';
      }
      return value.text;
    },
    suggestion: (value) => {
      return value.text;
    }
  }

  const onConfirm = (selected) => {
    setSelected(selected.id)
  };
<Autocomplete id='autocomplete'
                      source={source}
                      templates={templates}
                      onConfirm={onConfirm}
                      confirmOnBlur={false} />
1reaction
n1k0commented, Apr 1, 2021

@faithandrel ah, using templates is the way to go indeed, totally missed that reading the docs. Thank you for the hint, thatā€™s nicely solving my problem šŸ‘

Read more comments on GitHub >

github_iconTop Results From Across the Web

Excel unique / distinct values: how to find, filter, select and ...
The tutorial demonstrates the most efficient ways to find, filter, select and highlight distinct and unique values in Excel.
Read more >
5 easy ways to extract Unique Distinct Values - Get Digital Help
This UDF lets you create and sort a unique distinct list. First you need to copy the VBA code to your workbook, instructions...
Read more >
Filter for unique values or remove duplicate values
In Excel, there are several ways to filter for unique valuesā€”or remove duplicate values: To filter for unique values, click Data > Sort...
Read more >
Create CASE WHEN labels based on DISTINCT values in a ...
select id, (case when min(category) = max(category) then min(category) || ' only' when count(distinct category) = 3 then 'All' whenĀ ...
Read more >
7 Ways To Generate Unique Values List In Excel
Apply a filter to your data and click the filter arrow to see a list showing all the unique values within that particular...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found