在SAPUI5中取消选择组合框

时间:2019-01-16 14:52:10

标签: javascript sapui5

我有一个组合框,我想抛出一个带有后备逻辑的消息,以防万一他选择了某个东西,他应该得到一个警告,如果他按下确定,它将执行该逻辑,否则,将其取消。

<ComboBox id="id1" change="onChange">
    <core:Item id="id2" text="A"></core:Item>
    <core:Item id="id3" text="B"></core:Item>
    <core:Item id="id4" text="C"></core:Item>
</ComboBox>

然后我的messageboxonChange函数中:

onChange: function (oEvent) {
    sap.m.MessageBox.show("Are you sure you want to do that?", {
        icon: sap.m.MessageBox.Icon.Information,
        title: "Info",
        actions: [sap.m.MessageBox.Action.YES, sap.m.MessageBox.Action.NO],
        defaultAction: sap.m.MessageBox.Action.NO,
        onClose: function (sButton) {
            if (sButton === sap.m.MessageBox.Action.YES) {
                //execute my logic in here => that works
            } else {
                oEvent.stopPropagation(); //I tried this but this does not work
            }
        }
    });
}

我该如何实现?

换句话说,我真正需要做的就是知道combobox的前一个选择是什么。

我没有找到如何将其从oEvent中删除的方法。

3 个答案:

答案 0 :(得分:1)

方法是正确的(onChange内的对话框),但是您需要进行一些更改。 当SAPUI5调用onChange方法时,该值已经写入控件中,因此oEvent.stopPropagation();根本不执行任何操作。

您可以做的是使用ComboBox方法setSelectedItem(null),该方法应重置当前选择(恢复用户选择)。

更新:为了满足您的要求,我已经更新了代码。

    __selectedItem: null,

    onChange: function (oEvent) {
        var that = this;
        var source = oEvent.getSource();
        var selectedItem = oEvent.getSource().getSelectedItem();
        MessageBox.show("Are you sure you want to do that?", {
            icon: MessageBox.Icon.Information,
            title: "Info",
            actions: [MessageBox.Action.YES, MessageBox.Action.NO],
            defaultAction: MessageBox.Action.NO,
            onClose: function (sButton) {
                if (sButton === MessageBox.Action.YES) {
                    //execute my logic in here => that works
                    that.__selectedItem = selectedItem;
                } else {
                    source.setSelectedItem( that.__selectedItem );
                }
            }
        });
    }

答案 1 :(得分:0)

InputBase.js中的GitHub上查看SAP代码,触发附加事件时,新值已写入属性中。我的建议是将所选值保存在控制器内部的类变量中,并在用户取消实现目标后恢复为该值。

答案 2 :(得分:-1)

我决定采用以下解决方案,无论如何都要感谢其他方法

 onChange: function (oEvent) {
    var source = oEvent.getSource();
    MessageBox.show("Are you sure you want to do that?", {
        icon: MessageBox.Icon.Information,
        title: "Info",
        actions: [MessageBox.Action.YES, MessageBox.Action.NO],
        defaultAction: MessageBox.Action.NO,
        onClose: function (sButton) {
            if (sButton === MessageBox.Action.YES) {
                //execute my logic in here => that works
            } else {
                var oldSelection = oModel.getProperty(oSource.getBindingContext().getPath() + "/PropertyPath");
                switch (oldSelection) {
                case "A":
                    oSource.setSelectedItem(oSource.getItems()[0], true, true);
                    break;
                case "B":
                    oSource.setSelectedItem(oSource.getItems()[1], true, true);
                    break;
                case "C":
                    oSource.setSelectedItem(oSource.getItems()[3], true, true);
                    break;
                }
            }
        }
    });
}
相关问题