【问题标题】:How to reset combobox in lightning UI?如何在闪电 UI 中重置组合框?
【发布时间】:2018-07-29 12:39:48
【问题描述】:

**我的照明 UI 中有两个组合框和一组选项。如果其他组合框中的选项发生更改,我想重置一个组合框。

如何在 Salesforce Lightning UI 中动态重置组合框中的选项?**

【问题讨论】:

    标签: javascript salesforce salesforce-lightning lightning


    【解决方案1】:

    不确定 saleforce,但这里是如何使用 javascript 重置选择框。

    function myFn(){
    select_box = document.getElementById("myselectbox");
    select_box.selectedIndex = -1;
    }
    <select id="myselectbox" onchange="myFn()">
    <option id="">select</option>
    <option id="1">One</option>
    <option id="2">two</option>
    <option id="3">three</option>
    </select>

    【讨论】:

    • 我正在寻找 Salesforce Lightning UI
    【解决方案2】:

    这里有一个完整的自定义方法。 闪电组件:comboboxExp.cmp

    <aura:component>
    
    <aura:attribute name="options" type="List" default="[
    {'label': 'Apple', 'class': 'Apple', 'value': 'Apple'},
    {'label': 'Microsoft', 'class': 'Microsoft', 'value': 'Microsoft'},
    ]"/>
    <aura:attribute name="option2" type="List"/>
    
    <lightning:combobox name="fonts" required="true" label="Manufacturer" placeholder="Choose a Manufacturer" options="{! v.options }" onchange="{! c.handleChange }"/>
    <lightning:combobox name="fonts" required="true" label="Product" placeholder="Choose a Product" options="{! v.option2 }"/>
    

    JS 控制器:comboboxExpController.js

    ({
    handleChange: function (cmp, event, helper) {
        helper.populateValueToSecondOption(cmp, event);
    }
    })
    

    JS 助手:comboboxExpHelper.js

    ({
    populateValueToSecondOption : function(cmp, event) {
        var selectedOptionValue = event.getParam("value");
        if (selectedOptionValue == 'Apple') {
            this.createSecondOption('Apple', cmp, event);
        }
        else {
            this.createSecondOption('Microsoft', cmp, event);
        }
    },
    
    createSecondOption : function(value, cmp, event) {
        var options = [];
        if (value == 'Apple') {
            options.push(this.createObject('iphone', 'iphone', 'iphone'));
            options.push(this.createObject('macbook', 'macbook', 'macbook')); 
        }
        else {
            options.push(this.createObject('Windows OS', 'Windows OS', 'Windows OS'));
            options.push(this.createObject('Surface book', 'Surface book', 'Surface book'));
        }
        console.log(JSON.stringify(options));
        cmp.set('v.option2', options);
    },
    
    createObject : function(value, lebel, tempclass) {
        var obj = new Object();
        obj.label = lebel;
        obj.value = value;
        obj.class = tempclass;
        return obj;
    }
    })
    

    希望对您有所帮助。谢谢。

    【讨论】:

    • 感谢您的回复。我正在使用闪电组合框来显示选项。在这种情况下我们如何重置下拉菜单?
    • 您好@revanth-kumar,我已将方法从 lightning:select 更改为 lightning:combobox。基本上,逻辑是相似的,一个组合框的值的 onchange 需要用不同的一组值填充另一个组合框。
    猜你喜欢
    • 2013-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    • 2013-02-25
    • 1970-01-01
    相关资源
    最近更新 更多