简体   繁体   English

语义 UI 下拉选项数据属性

[英]Semantic UI Dropdown Option Data Attribute

I've been trying to attach a data-* attribute in a Semantic UI dropdown option but to no success (the data attributes are not being copied to the resulting dropdown options) .我一直在尝试在语义 UI 下拉option附加data-*属性,但没有成功(数据属性没有被复制到生成的下拉选项中)

Here's the structure of my select :这是我的select的结构:

HTML HTML

<select id='my-dropdown' name='department'>
    <option value='1' data-wsg='something'>Department 1</option>
    <option value='2' data-wsg='another'>Department 2</option>
    . . .
    <option value='n' data-wsg='custom data'>Department N</option>
</select>

jQuery jQuery

$("#my-dropdown").dropdown({
    allowAdditions: false,
    fullTextSearch: true,
    onHide: function() {
        // Some codes.
    },
    onChange: function(value, text, choice) {
        // Access the data-wsg attribute of the selected option.
    }
});

I've been reading around a bit but all I saw regarding data attribute support was storing the settings in there.我一直在阅读,但我所看到的关于数据属性支持的所有内容都是将设置存储在那里。 Not really what I need.不是我真正需要的。

Hopefully someone has done something similar and let me know what the solution is.希望有人做过类似的事情,让我知道解决方案是什么。

Its not pretty, but you can use the details in the onChange to hunt down the data attribute. 它不漂亮,但您可以使用onChange中的详细信息来搜索数据属性。 The functionality you are after was specifically rejected here - https://github.com/Semantic-Org/Semantic-UI/issues/931 你所追求的功能在这里被特别拒绝了 - https://github.com/Semantic-Org/Semantic-UI/issues/931

onChange: function(value, text, choice) {
    $(this).children('option[value=' + $(choice).data('value') + ']').data('wsg')
}

I was able to get this to work pretty consistently by attaching a data attribute to the main dropdown object, then just going two more levels to get the selection I want, like this 通过将数据属性附加到主下拉对象,然后再去两个级别来获得我想要的选择,我能够非常一致地工作,就像这样

'[data-testid="region-dropdown"] > div.visible.menu.transition > div:nth-child(4)'

Semantic UI wraps all the dropdown options in a div with the visible.menu.transition class and then each option is it's own div, so the nth-child lets you select a specific option as long as the order of your options is consistent. 语义UI使用visible.menu.transition类包装div中的所有下拉选项,然后每个选项都是它自己的div,因此只要选项的顺序一致, nth-child允许您选择特定选项。

I've found this works with pretty much any input type, it's just a matter of figuring out where to set the data attribute, then finding the path to the selector past that. 我发现这几乎适用于任何输入类型,只需要找出设置数据属性的位置,然后找到选择器的路径。

For example, a specific tab can be selected with something like 例如,可以使用类似的选项选择特定选项卡

'[data-testid="tabs-menu"] > div.ui.attached.tabular.menu > a:nth-child(1)'

The best way I could use for this case is that to save option value as can object.我可以用于这种情况的最好方法是将选项值保存为可以对象。 This solved my problem这解决了我的问题

JS JS

$(option).val(`{ id: '${item._id}', validTimestamp: ${validTimestamp} }`);

HTML HTML

<div class="item" data-value="{ id: '61ccf18e9d865424e7e0dd89', validTimestamp: 7776000000 }">3 months</div>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM