简体   繁体   English

来自API的语义UI下拉选择内容

[英]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. 我正在使用Semantic UI 2.0并尝试使用从其API返回的数据来构建我的下拉列表中的选项。

For the dropdown itself, I am using a code that is pratically the same as this code shown in Semantic UI's documentation: 对于下拉菜单本身,我使用一个代码,pratically同这个语义UI的文档中显示的代码:

<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: 我有一个返回json格式的城市的服务,然后在控制台中显示语义UI,结果是所有261个城市的成功:

"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: / cities端点返回一个格式为:的json:

{"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. 看起来Semantic UI并不直接理解json的格式。

I've tried many formats of json attributes, even tried to change a bit the html. 我尝试了很多格式的json属性,甚至试图改变一下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, eg,: 例如,尝试在select的底部添加一个空的<div class="menu"> ,希望语义UI可以填充它,例如:

<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 , eg, using "data-value" attribute. 我试图将属性的格式与示例中的格式相匹配,例如,使用“data-value”属性。

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. 但它也没有用,我在源代码中看到了语义UI 检查 ,所以它没有任何区别。 At the end, my problem persists and no items are inserted into the dropdown selection. 最后,我的问题仍然存在,并且下拉选项中没有插入任何项目。

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 . 如果没有你发布你正在使用的代码,我会在这里采取一些措施,但dropdown预计数据结果将被键入{ name: "Item 1", value: "value1" } ,如下所述。 文件的相关部分

If you have a different field names then you can provide a fields structure in the settings to override these: 如果您有不同的字段名称,则可以在设置中提供fields结构以覆盖这些:

$('.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: 所需的最低HTML是:

<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. <div class="menu"></div>会自动插入,但<input class="search"></input>是必需的,只有在您已经有<div class="text"></div>才会自动插入<div class="text"></div>元素。

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. 但是请注意,在我认为是下拉行为的错误时, 它将不会加载数据,直到您开始在搜索字段中键入 ,因此只需单击下拉图标是不够的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 语义 UI 反应,下拉选择 - Semantic UI React, Dropdown Selection 从 API 结果转换为影响第二个下拉菜单的下拉菜单 - 语义 UI - Transition from API results to Dropdown that affects a second dropdown - Semantic UI 语义 UI 反应:无法从 REST API 中获取下拉列表的值 - Semantic UI React: Cannot take values from REST API for dropdown 使用API​​.js的语义UI搜索选择下拉菜单-不会发生api调用 - Semantic UI Search Selection Dropdown with API.js - api call not happening 语义UI-下拉远程内容-额外数据 - Semantic UI - Dropdown Remote Content -Extra data 如何从 react-semantic-ui 的多搜索选择下拉框中获取值? - How to get the values from a Multiple Search Selection dropdown box in react-semantic-ui? React 测试库:如何从语义 UI 下拉列表中删除一个项目(在多选模式下)? - React Testing Library: How to remove an item from the Semantic UI Dropdown (in Multiple Selection mode)? 选择类的语义UI多级下拉列表不会扩展 - Semantic UI Multi-Level Dropdown with the selection class does not expand 语义 UI 多选下拉菜单阻止元素选择 - Semantic UI multi-select dropdown prevent element selection Semantic-ui-react 中的可搜索下拉菜单不会立即显示从 API 检索到的完整选项列表 - Searchable dropdown in semantic-ui-react does not immediately display full list of options retrieved from API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM