Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Semantic UI dropdown selection content from API

I am using Semantic UI 2.0 and trying to use data returned from its API to build the options inside my dropdown.

For the dropdown itself, I am using a code that is pratically the same as this code shown in Semantic UI's documentation:

<div class="ui search selection dropdown select-city">
  <input type="hidden" name="city">
  <i class="dropdown icon"></i>
  <div class="default text">Select City</div>
</div>

I have a service that returns json-formatted cities, then Semantic UI shows in the console that the result was successful with all 261 cities:

"Using specified url"   ["/cities/"]    1648
"Querying URL"  ["/cities/"]    0
"Sending request"   [undefined, "get"]  0
"Successful API Response"   [Object { success=true, results=[261]}]

The /cities endpoint return a json formatted as:

{"success":true,"results":[{"description":"Opole","data-value":1},{"description":"Wrocław","data-value":2},{"description":"Warszawa","data-value":3},{"description":"Budapest","data-value":4},{"description":"Köln","data-value":5}, ...]}

It looks like that Semantic UI does not directly understand the format of the json.

I've tried many formats of json attributes, even tried to change a bit the html. For instance, tried to add an empty <div class="menu"> in the bottom of the select, hoping that Semantic UI would fill it in, e.g.,:

<div class="ui search selection dropdown select-city">
  <input type="hidden" name="city">
  <i class="dropdown icon"></i>
  <div class="default text">Select City</div>
  <div class="menu"></div>
</div>

I am trying to match the format of the attributes with the ones from the example, e.g., using "data-value" attribute.

But it did not work either, I've seen Semantic UI checks for that in the source code, so it does not make any difference. At the end, my problem persists and no items are inserted into the dropdown selection.

like image 482
Eduardo Avatar asked Aug 03 '15 13:08

Eduardo


People also ask

Which UI component allows used to select an item from dropdown menu?

The dropdown list we have used here may appear similar to the dropdowns we have used in our jQuery menu component. The key difference with the dropdown list component is that the currently selected item is always exposed and the purpose is to indicate that the user can toggle between options.

How do I add a search drop down in HTML?

Use any element to open the dropdown menu, e.g. a <button>, <a> or <p> element. Use a container element (like <div>) to create the dropdown menu and add the dropdown links inside it. Wrap a <div> element around the button and the <div> to position the dropdown menu correctly with CSS.

Is it dropdown or drop down?

On the other hand, Merriam-Webster lists “drop-down” and “dropdown” as equal variants for the noun form (the adjective form is always hyphenated), so maybe there's hope for you.


1 Answers

Without you posting the code that you're using I'm taking a bit of a stab here, but the dropdown expects data results to be keyed as { name: "Item 1", value: "value1" } as is explained in the relevant part of the documentation.

If you have a different field names then you can provide a fields structure in the settings to override these:

$('.ui.dropdown').dropdown({
    fields: { name: "description", value: "data-value" },
    apiSettings: {
        mockResponse: {
            success: true,
            results: [
                {"description":"Opole","data-value":1},
                {"description":"Wrocław","data-value":2},
                {"description":"Warszawa","data-value":3},
                {"description":"Budapest","data-value":4},
                {"description":"Köln","data-value":5}
            ]
        }
    }
});

The minimum HTML required is:

<div class="ui search selection dropdown">
    <div class="text">Search</div>
</div>

or:

<div class="ui search selection dropdown">
    <input class="search"></input>
</div>

The empty <div class="menu"></div> is automatically inserted, but an <input class="search"></input> is required and is only automatically inserted if you already have a <div class="text"></div> element.

Note however that, in what I believe to be a fault with the dropdown behaviour, it will not load the data until you start typing into the search field and so just clicking on the dropdown icon is not sufficient.

like image 111
Parakleta Avatar answered Sep 23 '22 07:09

Parakleta