当前位置: 动力学知识库 > 问答 > 编程问答 >

sapui5 - Not able to toggle ui5 button

问题描述:

new sap.m.Button("manualimage",{

icon : 'resources/Green.JPG',

width : "40px",

height : "40px",

press :function(e) {

var myBtn = sap.ui.getCore().byId("manualimage");

console.log(document.getElementById("manualimage").icon);

myBtn.setIcon('');

}

})

When I click on the button the Icon is not changing, any suggestions what I might be doing wrong here?

网友答案:

Below is my code which is working ( on UI5 version 1.42). I only find one mistake : You should use ToggleButton to keep the state of Button. Say remove image if pressed or set image back when pressed back ( ie. not pressed).

Code in XML :

<ToggleButton icon='./images/ICICI.png' text ='hey' pressed='false' press='handlePress' />

Code in Controller:

handlePress: function(evt) {
        var oSource = evt.getSource()
        var bPressed = oSource.getPressed();
        if(bPressed) {
            oSource.setIcon('');
        } else {
            oSource.setIcon('./images/ICICI.png');
        }
    }

Let me know if this works for you.

网友答案:

When you use:

var myBtn = sap.ui.getCore().byId("manualimage");

your var myBtn is undefined, because using sap.ui.getCore() id of button is expecting something like:

sap.ui.getCore().byId("__xmlview1--manualimage"); 

where __xmlview1-- is autogenerated by framework. So please use this code instead:

var myBtn = this.byId("manualimage");
分享给朋友:
您可能感兴趣的文章:
随机阅读: