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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With