SAPUI5自定义控件getAggregation()为空

编程入门 行业动态 更新时间:2024-10-09 00:52:35
本文介绍了SAPUI5自定义控件getAggregation()为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在sapui5中编写了一个自定义控件。它是一个虚拟键盘(屏幕键盘),具有MatrixLayout和许多按钮作为聚合。我添加了一个公共功能的控制,应该允许我禁用一些按钮。但是当我试图在这个函数中访问它时,除了我的MatrixLayout-Aggregation之外,我所有的聚合都是空的。

有人可以帮我解释我如何访问我的聚合(按钮)? 我已经用可见性隐藏和公开的方式尝试过它。我确实看到生成的访问器,但那些返回null。 如果我试图获得聚合_layout,我的MatrixLayout被返回。

我试图以这种方式访问​​它:

onVirtualKeyboardClickBase:function(oEvent,oController){ var virtualKeyboard = oEvent.getSource()。disableButton(_ btnMinus); $ / code $ 这是自定义控件的代码:

(function(){use strict; jQuery.sap.declare(de.vw。 timerec.controls.VirtualKeyboard); $ .sap.includeStyleSheet(css / VirtualKeyboard.css); jQuery.sap.require(sap.ui。 commons.Button); sap.ui.core.Control.extend(de.vw.timerec.controls.VirtualKeyboard,{ //控制API : metadata:{ properties:{ / * Business Object properties * / title:{type:string},width: {type:sap.ui.core.CSSSize,defaultValue:253px},buttonWidth:{type:sap.ui.core.CSSSize,defaultValue:57px},buttonHeight:{type:sap.ui.core.CSSSize,defaultValue:57px} }, 聚合:{_layout:{type:sap.uimons.layout.MatrixLayout,multiple:false,visibility:hidden},_btn1:{type:sap.uimons .Button,multiple:false,visibility:hidden},_btn2:{type:sap.uimons.Button,multiple:false,visibility:hidden},_btn3:{type:sap.uimons.Button,multiple:false,visibility:hidden},_btnDel:{type:sap.uimons.Button, multiple:false,visibility:hidden},_btn4:{type:sap.uimons.Button,multiple:false,visibility:hidden},_btn5 :{type:sap.uimons.Button,multiple:false,visibility:hidden},_btn6:{type:sap.uimons.Button,multiple:false, visibility:hidden},_btnAlpha:{type:sap.uimons.Button,multiple:false,visibility:hidden} ,_btn7:{type:sap.uimons.Button,multiple:false,visibility:hidden},_btn8:{type:sap.uimons .Button,multiple:false,visibility:hidden},_btn9:{type:sap.uimons.Button,multiple:false,visibility:hidden},_btnBack:{type:sap.uimons.Button,multiple:false,visibility:hidden},_btn0:{type:sap.uimons.Button, multiple:false,visibility:hidden},_btnComma:{type:sap.uimons.Button,multiple:false,visibility:hidden},_btnMinus :{type:sap.uimons.Button,multiple:false,visibility:public},_btnNext:{type:sap.uimons.Button,multiple:false,能见度:隐藏} }, 协会:{}, 事件:{c lick:{enablePreventDefault:true} } }, init:function(){ var oControl = this; var oMatrixLayout; var oBtn1,oBtn2,oBtn3,oBtnDel,oBtn4,oBtn5,oBtn6,oBtnAlpha, oBtn7,oBtn8,oBtn9,oBtnBack,oBtn0,oBtnComma,oBtnMinus,oBtnNext; oMatrixLayout = new sap.uimons.layout.MatrixLayout({ id:this.getId()+-matrixLayout, layoutFixed:true,栏:4,宽度:100%}); this.setAggregation(_ layout,oMatrixLayout); oBtn1 = this._createButton(1,1,oControl,true); this.setAggregation(_ btn1,oBtn1); oBtn2 = this._createButton(2,2,oControl,true); this.setAggregation(_ btn2,oBtn2); oBtn3 = this._createButton(3,3,oControl,true); this.setAggregation(_ btn3,oBtn3); oBtnDel = this._createButton(,delete,oControl,false); oBtnDel.setIcon(sap-icon:// undo); this.setAggregation(_ btnDel,oBtnDel); oMatrixLayout.createRow(oBtn1,oBtn2,oBtn3,oBtnDel); oBtn4 = this._createButton(4,4,oControl,true); this.setAggregation(_ btn4,oBtn4); oBtn5 = this._createButton(5,5,oControl,true); this.setAggregation(_ btn5,oBtn5); oBtn6 = this._createButton(6,6,oControl,true); this.setAggregation(_ btn6,oBtn6); oBtnAlpha = this._createButton(Alpha,alpha,oControl,false); this.setAggregation(_ btnAlpha,oBtnAlpha); oMatrixLayout.createRow(oBtn4,oBtn5,oBtn6,oBtnAlpha); oBtn7 = this._createButton(7,7,oControl,true); this.setAggregation(_ btn7,oBtn7); oBtn8 = this._createButton(8,8,oControl,true); this.setAggregation(_ btn8,oBtn8); oBtn9 = this._createButton(9,9,oControl,true); this.setAggregation(_ btn9,oBtn9); oBtnBack = this._createButton(,back,oControl,false); oBtnBack.setIcon(sap-icon:// arrow-top); this.setAggregation(_ btnBack,oBtnBack); oMatrixLayout.createRow(oBtn7,oBtn8,oBtn9,oBtnBack); oBtn0 = this._createButton(0,0,oControl,true); this.setAggregation(_ btn0,oBtn0); oBtnComma = this._createButton(,,,,oControl,true); this.setAggregation(_ btnComma,oBtnComma); oBtnMinus = this._createButton( - , - ,oControl,true); this.setAggregation(_ btnMinus,oBtnMinus); oBtnNext = this._createButton(,next,oControl,false); oBtnNext.setIcon(sap-icon:// arrow-bottom); this.setAggregation(_ btnNext,oBtnNext); oMatrixLayout.createRow(oBtn0,oBtnComma,oBtnMinus,oBtnNext); $, onAfterRendering:function(){ }, _createButton:function(text,buttonValue,oControl,isDigitButton) { var styleClassName =virtualKeyboardButtonDigit; if(!isDigitButton)styleClassName =virtualKeyboardButtonSpecial; var buttonIdHelper = buttonValue; if(buttonValue ==,){ buttonIdHelper =comma; } if(buttonValue == - ){ buttonIdHelper =minus; } var buttonId = this.getId()+virtualKeyboardButton_+ buttonIdHelper; var oBtn = new sap.uimons.Button({ id:buttonId, text:text, width:this.getProperty(buttonWidth), $ height:this.getProperty(buttonHeight),按:function(oEvent){ oControl.fireClick({ buttonValue:buttonValue }); } })。addStyleClass(virtualKeyboardButton+ styleClassName); return oBtn; }, disableButton:function(buttonName) { // this.getAggregation(<任意按钮聚合>)返回null // this.getAggregation(_ layout)返回MatrixLayout-Aggregation this.getAggregation(buttonName).setEnabled(false); }, 渲染器:{ 渲染器:函数(oRm,oControl){ oRm.write(< div ); oRm.writeControlData(oControl); oRm.addStyle(width,oControl.getWidth()); oRm.addStyle(margin-left,44px); oRm.writeStyles(); oRm.write(>); oRm.renderControl(oControl.getAggregation(_ layout)); oRm.write(< / div>); } } }); de.vw.timerec.controls.VirtualKeyboard.prototype.exit = function(){ }; }());

解决方案

如果您将按钮添加到MatrixLayout中的聚合,它们会自动从控件的聚合中删除。聚合内容只能同时在一个聚合中。你将不得不通过ID访问它们。这就是UI5关联的目的(它们基本上是拥有控件实例ID的组)。

I have written a custom control in sapui5. Its a virtual keyboard (onscreen keyboard) having a MatrixLayout and a lot of buttons as aggregations. I added a "public" function to the control which should allow me to disable some of the buttons. But when I am trying to access it within this function all my aggregations except my MatrixLayout-Aggregation is null.

Can somebody help me out how I can access my aggregations (buttons)? I have tried it with visibility hidden and public. I do see the generated accessors then, but those return "null" too. If I try to get the aggregation "_layout" my MatrixLayout is returned.

I am trying to access it this way:

onVirtualKeyboardClickBase : function(oEvent, oController){ var virtualKeyboard = oEvent.getSource().disableButton("_btnMinus"); }

This is the code of the custom control:

(function () { "use strict"; jQuery.sap.declare("de.vw.timerec.controls.VirtualKeyboard"); $.sap.includeStyleSheet("css/VirtualKeyboard.css"); jQuery.sap.require("sap.uimons.Button"); sap.ui.core.Control.extend("de.vw.timerec.controls.VirtualKeyboard", { // the control API: metadata : { properties : { /* Business Object properties */ "title" : {type : "string"}, "width" : {type: "sap.ui.core.CSSSize", defaultValue: "253px" }, "buttonWidth" : {type: "sap.ui.core.CSSSize", defaultValue: "57px" }, "buttonHeight" : {type: "sap.ui.core.CSSSize", defaultValue: "57px" } }, aggregations : { "_layout" : {type : "sap.uimons.layout.MatrixLayout", multiple : false, visibility: "hidden"}, "_btn1" : {type : "sap.uimons.Button", multiple : false, visibility: "hidden"}, "_btn2" : {type : "sap.uimons.Button", multiple : false, visibility: "hidden"}, "_btn3" : {type : "sap.uimons.Button", multiple : false, visibility: "hidden"}, "_btnDel" : {type : "sap.uimons.Button", multiple : false, visibility: "hidden"}, "_btn4" : {type : "sap.uimons.Button", multiple : false, visibility: "hidden"}, "_btn5" : {type : "sap.uimons.Button", multiple : false, visibility: "hidden"}, "_btn6" : {type : "sap.uimons.Button", multiple : false, visibility: "hidden"}, "_btnAlpha" : {type : "sap.uimons.Button", multiple : false, visibility: "hidden"}, "_btn7" : {type : "sap.uimons.Button", multiple : false, visibility: "hidden"}, "_btn8" : {type : "sap.uimons.Button", multiple : false, visibility: "hidden"}, "_btn9" : {type : "sap.uimons.Button", multiple : false, visibility: "hidden"}, "_btnBack" : {type : "sap.uimons.Button", multiple : false, visibility: "hidden"}, "_btn0" : {type : "sap.uimons.Button", multiple : false, visibility: "hidden"}, "_btnComma" : {type : "sap.uimons.Button", multiple : false, visibility: "hidden"}, "_btnMinus" : {type : "sap.uimons.Button", multiple : false, visibility: "public"}, "_btnNext" : {type : "sap.uimons.Button", multiple : false, visibility: "hidden"} }, associations: { }, events : { click : {enablePreventDefault : true} } }, init : function() { var oControl = this; var oMatrixLayout; var oBtn1, oBtn2, oBtn3, oBtnDel, oBtn4, oBtn5, oBtn6, oBtnAlpha, oBtn7, oBtn8, oBtn9, oBtnBack, oBtn0, oBtnComma, oBtnMinus, oBtnNext; oMatrixLayout = new sap.uimons.layout.MatrixLayout({ id : this.getId() + "-matrixLayout", layoutFixed : true, columns : 4, width : "100%" }); this.setAggregation("_layout", oMatrixLayout); oBtn1 = this._createButton("1", "1", oControl, true); this.setAggregation("_btn1", oBtn1); oBtn2 = this._createButton("2", "2", oControl, true); this.setAggregation("_btn2", oBtn2); oBtn3 = this._createButton("3", "3", oControl, true); this.setAggregation("_btn3", oBtn3); oBtnDel = this._createButton("", "delete", oControl, false); oBtnDel.setIcon("sap-icon://undo"); this.setAggregation("_btnDel", oBtnDel); oMatrixLayout.createRow(oBtn1, oBtn2, oBtn3, oBtnDel); oBtn4 = this._createButton("4", "4", oControl, true); this.setAggregation("_btn4", oBtn4); oBtn5 = this._createButton("5", "5", oControl, true); this.setAggregation("_btn5", oBtn5); oBtn6 = this._createButton("6", "6", oControl, true); this.setAggregation("_btn6", oBtn6); oBtnAlpha = this._createButton("Alpha", "alpha", oControl, false); this.setAggregation("_btnAlpha", oBtnAlpha); oMatrixLayout.createRow(oBtn4, oBtn5, oBtn6, oBtnAlpha); oBtn7 = this._createButton("7", "7", oControl, true); this.setAggregation("_btn7", oBtn7); oBtn8 = this._createButton("8", "8", oControl, true); this.setAggregation("_btn8", oBtn8); oBtn9 = this._createButton("9", "9", oControl, true); this.setAggregation("_btn9", oBtn9); oBtnBack = this._createButton("", "back", oControl, false); oBtnBack.setIcon("sap-icon://arrow-top"); this.setAggregation("_btnBack", oBtnBack); oMatrixLayout.createRow(oBtn7, oBtn8, oBtn9, oBtnBack); oBtn0 = this._createButton("0", "0", oControl, true); this.setAggregation("_btn0", oBtn0); oBtnComma = this._createButton(",", ",", oControl, true); this.setAggregation("_btnComma", oBtnComma); oBtnMinus = this._createButton("-", "-", oControl, true); this.setAggregation("_btnMinus", oBtnMinus); oBtnNext = this._createButton("", "next", oControl, false); oBtnNext.setIcon("sap-icon://arrow-bottom"); this.setAggregation("_btnNext", oBtnNext); oMatrixLayout.createRow(oBtn0, oBtnComma, oBtnMinus, oBtnNext); }, onAfterRendering: function () { }, _createButton : function (text, buttonValue, oControl, isDigitButton) { var styleClassName = "virtualKeyboardButtonDigit"; if (!isDigitButton) styleClassName = "virtualKeyboardButtonSpecial"; var buttonIdHelper = buttonValue; if (buttonValue == ",") { buttonIdHelper = "comma"; } if (buttonValue == "-") { buttonIdHelper = "minus"; } var buttonId = this.getId() + "virtualKeyboardButton_" + buttonIdHelper; var oBtn = new sap.uimons.Button({ id: buttonId, text: text, width: this.getProperty("buttonWidth"), height: this.getProperty("buttonHeight"), press: function (oEvent) { oControl.fireClick({ buttonValue : buttonValue }); } }).addStyleClass("virtualKeyboardButton " + styleClassName); return oBtn; }, disableButton : function (buttonName) { // this.getAggregation(<any button aggregation>) returns "null" // this.getAggregation("_layout") returns the MatrixLayout-Aggregation this.getAggregation(buttonName).setEnabled(false); }, renderer : { render : function(oRm, oControl) { oRm.write("<div"); oRm.writeControlData(oControl); oRm.addStyle("width", oControl.getWidth()); oRm.addStyle("margin-left", "44px"); oRm.writeStyles(); oRm.write(">"); oRm.renderControl(oControl.getAggregation("_layout")); oRm.write("</div>"); } } }); de.vw.timerec.controls.VirtualKeyboard.prototype.exit = function () { }; }());

解决方案

If you add a Button to the Aggregations in the MatrixLayout, they are automatically removed from your control's aggregations. Aggregation contents can only be in one aggregation at the same time. You will have to access them via ID. That's what UI5 Associations are for (they are basically groups that hold the IDs of the control instances).

更多推荐

SAPUI5自定义控件getAggregation()为空

本文发布于:2023-11-22 08:09:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616623.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   控件   为空   getAggregation

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!