简体   繁体   English

语义 - UI动态下拉列表

[英]Semantic-UI Dynamic Dropdown

Got a problem with semantic-ui dropdown. 语义下拉有问题。
I've been using Semantic-Ui, and wanted to change the dropdown item dynamically. 我一直在使用Semantic-Ui,并希望动态更改下拉项。 That is, when i choose the value from the first dropdown, the second dropdown's item will change. 也就是说,当我从第一个下拉列表中选择值时,第二个下拉列表的项目将会更改。 But the thing is, when the items are changed, the second dropdown cannot be chose, the value won't change. 但问题是,当项目发生变化时,无法选择第二个下拉列表,该值不会改变。 The dropdown won't collapse back. 下拉列表不会倒退。

The HTML HTML
The First Dropdown 第一次下拉

<div id="programmetype" class="ui fluid selection dropdown">
    <input type="hidden" name="programmetype">
    <div class="text">Choose..</div>
    <i class="dropdown icon"></i>
    <div class="menu">
       <div class="item" data-value="val1">Car</div>
       <div class="item" data-value="val2">Tank</div>
       <div class="item" data-value="val3">Plane</div>
    </div>
</div>

Second Dropdown 第二次下拉

<div id="servicetype" class="ui fluid selection dropdown">
    <input type="hidden" name="servicetype">
    <div class="text">Choose..</div>
    <i class="dropdown icon"></i>
    <div class="menu">
        <div class="item" data-value="select">Choose..</div>
    </div>
</div>

.......................... ..........................
The jQuery jQuery

$("#programmetype").dropdown({
            onChange: function (val) {
                if (val == "val1")
                {
                    $('#servicetype .menu').html(
                        '<div class="item" data-value="val1">Saloon</div>' +
                        '<div class="item" data-value="val2">Truck</div>'
                        );
                };

                if (val == "val2")
                {
                    $('#servicetype .menu').html(
                        '<div class="item" data-value="val1">Abraham</div>' +
                        '<div class="item" data-value="val2">Leopard</div>' +
                        );
                };

                if (val == "val3")
                {
                    $('#servicetype .menu').html(
                        '<div class="item" data-value="val1">Jet</div>' +
                        '<div class="item" data-value="val2">Bomber</div>' +
                        );
                };
            }
        });

Found it, 找到了,

I put $('#servicetype').dropdown(); 我把$('#servicetype').dropdown(); after assigning the values: 分配值后:

$("#programmetype").dropdown({
                onChange: function (val) {
                    if (val == "fertility")
                    {
                        $('#servicetype').dropdown({'set value':'something'});
                        $('#servicetype .menu').html(
                            '<div class="item" data-value="acp">ACP</div>' +
                            '<div class="item" data-value="art">ART</div>'
                            );
                        $('#servicetype').dropdown();
                    };
});

A workaround approach: 解决方法:

function setDinamicOptions(selector, options) {
    var att = "data-dinamic-opt";
    $(selector).find('[' + att + ']').remove();
    var html = $(selector + ' .menu').html();
    for(key in options) {
        html += '<div class="item" data-value="' + options[key] + '" ' + att + '>' + key + '</div>';
    }
    $(selector + ' .menu').html(html);
    $(selector).dropdown();
}
$("#programmetype").dropdown({
    onChange: function (val) {
        if (val == "val1")
            setDinamicOptions('#servicetype', {Saloon:'val1', Truck:'val2'});
        if (val == "val2")
            setDinamicOptions('#servicetype', {Abraham:'val1', Leopard:'val2'});
        if (val == "val3")
            setDinamicOptions('#servicetype', {Jet:'val1', Bomber:'val2'});
    }
});

Try this. 尝试这个。

If you want hover effect to open dropdown menu then you don't need to use javascript, instead you can add class simple to your parent div 如果你想悬停效果打开下拉菜单,那么你不需要使用javascript,而是你可以添加类简单到你的父div

this is a demo http://jsfiddle.net/BJyQ7/30/ 这是一个演示http://jsfiddle.net/BJyQ7/30/

<div class="ui simple dropdown item">
    <i class="fa fa-users"></i> Members <i class="fa fa-caret-down"></i>
    <div class="menu">
        <a class="item"><i class="fa fa-users"></i> Players</a>
        <a class="item"><i class="fa fa-user-md"></i> Staff</a>
    </div>
</div>

Another approach for setting dropdown content just in time (for example based on some dynamic criteria) is using the remoteApi, that is a particularly unfortunate name for the data binding features of the SemanticUI dropdowns. 另一种及时设置下拉内容的方法(例如基于某些动态标准)是使用remoteApi,这是SemanticUI下拉列表的数据绑定功能的一个特别不幸的名称。

Follow instructions here: http://semantic-ui.com/behaviors/api.html#mocking-responses 按照以下说明操作: http//semantic-ui.com/behaviors/api.html#mocking-responses

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

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