我将一棵树作为我项目中的聚合。我找到了一些关于如何获取它们的信息。但是我仍然遇到了一个问题......每当我想调用树的方法setTitle
时,我都会遇到异常:Uncaught TypeError: Cannot read property 'setTitle' of null
我的代码如下所示:
sap.ui.core.UIComponent.extend("ui5_i.myComponent.Component", {
metadata : {
includes: ["../../resources/css/org-struct.css"],
properties : {
treeTitle: "string",
searchPlaceholder: "String",
showId: { type:"boolean", defaultValue: true },
data: "object",
height :"string",
width: "string"
},
aggregations: {
_Tree : {type:'sap.ui.commons.Tree', multiple: false}
}
},
createContent: function(){
console.log("createContent!");
this.set_Tree(new sap.ui.commons.Tree());
....
.... here comes the rest of the view
}
我的二传手法:
ui5_i.myComponent.Component.prototype.setTreeTitle = function(sTitle) {
if(typeof(sTitle) === "string" && sTitle != undefined && sTitle !=null){
this.setProperty("treeTitle", sTitle);
this.get_Tree().setTitle(sTitle);
return this;
}else{
throw("Property 'treeTitle' is not of type 'string' or 'undefined'!");
}
我不明白错误,因为内容是创建的,所以树不能是null
,我甚至在注释setter方法时看到了Tree,所以我通常可以调用setTitle方法...
谁知道出了什么问题?
Uncaught TypeError: Cannot read property 'addDelegate' of null
这是完整的代码:
jQuery.sap.require("sap.ui.core.UIComponent");
jQuery.sap.require("sap.ui.commons.Tree");
jQuery.sap.declare("ui5_i.myComponent.Component");
sap.ui.core.UIComponent.extend("ui5_i.myComponent.Component", {
metadata : {
includes: ["../../resources/css/org-struct.css"],
properties : {
treeTitle: "string",
searchPlaceholder: "String",
showId: { type:"boolean", defaultValue: true },
data: "object",
height :"string",
width: "string"
},
aggregations: {
_oTree : {type:'sap.ui.commons.Tree', multiple:false }
}
},
init : function ( ){
sap.ui.core.UIComponent.prototype.init.apply(this, arguments );
var oSearchSettings = new sap.ui.model.json.JSONModel({
"partially_search": "true",
"case_sensitive": "true",
"searchedText": "",
"aSearchResults" : [],
"indexOfSearchResults": "0"
});
sap.ui.getCore().setModel("settings", oSearchSettings);
this.set_oTree(new sap.ui.commons.Tree());
},
createContent: function(){
var oResponsiveLayout = new sap.ui.layout.form.ResponsiveGridLayout({columnsL: 3,columnsM: 2, columnS: 1});
var oFormLayout = new sap.ui.layout.form.Form({
width: this.getProperty("width"),
height: this.getProperty("height"),
layout: oResponsiveLayout,
formContainers: [ new sap.ui.layout.form.FormContainer({
formElements: [new sap.ui.layout.form.FormElement({
fields: [ this.oSearchField = new sap.ui.commons.SearchField({
placeholder : "Suchtext / ID",
search: this._onSearchRequest,
layoutData: new sap.ui.layout.GridData({span: "XL8 L8 M8 S8"})
}), this.oArrowUp = new sap.ui.commons.Button({
width: "40px",
icon: "resources/images/arrow_up.png",
layoutData: new sap.ui.layout.GridData({span: "XL1 L1 M1 S1"})
}), this.oArrowDown = new sap.ui.commons.Button({
width: "40px",
icon: "resources/images/arrow_down.png",
layoutData: new sap.ui.layout.GridData({span: "XL1 L1 M1 S1"})
}),
this.oSettingsMenuButton = new sap.ui.commons.MenuButton({
height: "80%",
width: "50px",
icon: "resources/images/settings.png",
lite: true,
layoutData: new sap.ui.layout.GridData({span: "XL2 L2 M2 S2"}),
menu: new sap.ui.unified.Menu({
items: [ new sap.ui.unified.MenuItem({
text: "ID",
submenu: new sap.ui.unified.Menu({
items: [ this.oIdMenuButton = new sap.ui.unified.MenuItem({
text: "IDs anzeigen/ausblenden",
icon: "resources/images/check.png",
select: this._onShowHideIdRequest
})]
})
}), new sap.ui.unified.MenuItem({
text: "Suche",
submenu: new sap.ui.unified.Menu({
items: [ new sap.ui.unified.MenuItem({
text: "Teilsuche aktivieren/deaktivieren",
icon: "resources/images/check.png",
select: this._onPartiallySearchRequest
}), new sap.ui.unified.MenuItem({
text: "Groß- und Kleinschreibung beachten/ignorieren",
icon: "resources/images/check.png",
select: this._onCaseSensitiveRequest
})]
})
})]
})
}) ]
}),new sap.ui.layout.form.FormElement({
fields: [ this.get_oTree()]
})]
})]
});
return oFormLayout;
},
_bindData : function(){
var oJsonModel = new sap.ui.model.json.JSONModel(this.getProperty("data"));
this.getAggregation('_oTree').setModel(oJsonModel,"org_structJSON");
this._loadNodes();
},
_loadNodes: function(){
var rootPath = this._getRootPath();
var aParentID = [];
for(key in rootPath){
var parentID = rootPath[key].parent;
if(aParentID.indexOf(parentID) < 0){
aParentID.push(parentID);
this._loadChildNodes(parentID);
}
}
},
_getRootPath : function() {
var oJSON = this.oTree.getModel("org_structJSON");
return oJSON.getProperty("/ORG_UNITS");
},
_loadChildNodes : function(parentID) {
var rootPath = this._getRootPath();
if(parentID === ""){
for(key in rootPath){
var node = rootPath[key];
if(node.parent === parentID){
var rootNode = this._createNode(node.id, node.short_text);
this.getAggregation(_oTree).addNode(rootNode);
}
}
}else{
var aNodes = this.oTree.findAggregatedObjects(true);
for(var index = 0; index < aNodes.length; index++){
var nodeID = aNodes[index].getId();
if(nodeID.slice(1,nodeID.length) === parentID){
for(key in rootPath){
if(rootPath[key].parent === parentID){
var oChildNode = this._createNode(rootPath[key].id, rootPath[key].short_text);
aNodes[index].addNode(oChildNode);
}
}
}
}
}
},
_createNode : function(id, short_text) {
var oNode = new sap.ui.commons.TreeNode({
id : "_" + id,
icon : "resources/images/organisation_unit.gif",
expanded: false
});
if(window.showId){
oNode.setText(short_text + " (" + id + ")");
}else{
oNode.setText(short_text)
}
return oNode;
},
_onSearchRequest : function(oControlEvent) {
},
_onShowHideIdRequest : function(){
},
_onPartiallySearchRequest : function(){
},
_onCaseSensitiveRequest : function(){
}
});
ui5_i.myComponent.Component.prototype.setTreeTitle = function(sTitle) {
if(typeof(sTitle) === "string" && sTitle != undefined && sTitle !=null){
this.setProperty("treeTitle", sTitle);
this.get_oTree().setTitle(sTitle);
return this;
}else{
throw("Property 'treeTitle' is not of type 'string' or 'undefined'!");
}
},
ui5_i.myComponent.Component.prototype.setData = function(oData) {
if(typeof(oData) === "object" && oData != undefined && oData !=null){
this.setProperty("data", oData);
this._bindData();
return this;
}else{
throw("Property 'data' is not of type 'object' or undefined!");
}
},
ui5_i.myComponent.Component.prototype.setSearchPlaceholder = function(sText) {
if(typeof(sText) === "string" && sText != undefined && sText !=null){
this.setProperty("searchPlaceholder", sText);
this.oSearchField.setPlaceholder(sText);
return this;
}else{
throw("Property 'searchPlaceholder' is not of type 'string' or 'undefined!'");
}
}
答案 0 :(得分:0)
createContent()
。这就是您使用sap.ui.core.UIComponent.prototype.init.apply(this, arguments );
调用的内容。
所以当你的createContent()
运行时,你还没有初始化树。将init()
函数的最后一行移到该函数的顶部。
编辑:我可以看到您的代码存在其他问题:
documentation说明了有关聚合的内容:
托管聚合可以存储对其他ManagedObjects的一个或多个引用。它们是控制聚合对象生命周期的一种手段:一个ManagedObject可以在任何时候由至多一个父ManagedObject聚合。
因此,当您在createContent()
中将树添加到表单时,它将自动从组件聚合中删除。
我并不想要你想要实现什么,但也许你必须考虑一个通过ID查找树的getMethod,或者你可以使用关联而不是聚合。