javascript - 如何从另一个函数触发选择事件 [SAPUI5]
问题描述
我想知道如何从另一种方法中选择和触发(CheckBox)选择事件,就好像在使用 selectionFunction 的情况下模拟 CheckBox 的选择一样。
我确实有以下复选框:
<VBox>
<CheckBox select="onSelectFirstCheckBox" />
<CheckBox select="onSelectSecondCheckBox" />
</VBox>
有两种伴随的方法:
onSelectionFirstCheckBox: function(oEvent) {
console.log("Hello World");
//From here select the second checkBox + trigger it's selection method
},
onSelectionSecondCheckBox: function(oEvent) {
console.log("Hello World");
}
问题:选择第一个CheckBox
并随后在onSelectionFirstCheckBox
我想选择第二个CheckBox
并触发其方法时onSelectionSecondCheckBox
。怎么做?
解决方案
您始终可以获取“其他”复选框的实例并触发事件:
oOtherCheckBox = ...;
// select the other checkbox
oOtherCheckBox.setSelected(true);
// fire select event so event handlers can handle the change
oOtherCheckBox.fireSelect({selected:true});
至于如何获取另一个复选框,您可以根据视图的结构从父级遍历到子oEvent.getSource()
级,或者给它一个 id 并通过 id 从视图中获取:this.getView().byId(id of the other checkbox)