2016-03-24 57 views
0
sap.ui.core.Element.extend("custom.barNlineChartControl", { metadata : { 
      properties : { 
       "Job" : {type : "string", group : "Misc", defaultValue : null}, 
       "Threshold" : {type : "int", group : "Misc", defaultValue : null}, 
      } 
     }}); 

sap.ui.core.Control.extend("control.barNlinechart", { 
     /* the control API */ 
     metadata : { 
      aggregations : { 
       "items" : { type: "custom.barNlineChartControl", multiple : true, singularName : "item"} 
      }, 
      events: { 
       "select" : {}, 
       "selectEnd": {}    
      }   
     }, 

    //D3 Code below: 
    onAfterRendering: function() { 
      var that = this; 
      /* get the Items aggregation of the control and put the data into an array */ 
      var aItems = this.getItems(); 

      var data = []; 
      for (var i=0;i<aItems.length;i++){ 
       var oEntry = {}; 
       for (var j in aItems[i].mProperties) { 
        oEntry[j]=aItems[i].mProperties[j]; 
       }     
       data.push(oEntry); 
      } 
      alert(JSON.stringify(data)); 

代碼視圖&控制自定義控制Openui5

multiBarLineGraph = new control.barNlinechart({ 
    layoutData: new sap.ui.layout.GridData({span: "L12 M12 S12"}), 
    items: { 
      path : "/genericData", 
      template : new custom.barNlineChartControl({Job:"{Job}",Threshold:"{Threshold}"}), 
      } 
    }), 

    var multiBarData = { 
        "genericData":[ 
            { 
              "Job": "Doctor", 
              "Threshold": 45, 
              "Hospital1": 30, 
              "Hospital2": 100, 
              "Hospital3": 90, 
             }, 
             { 
              "Job": "Teacher", 
              "Threshold": 65, 
             "School1": 60, 
              "School2": 75, 
             }, 
             ]}; 

當D3代碼警報執行我得到工作&閾值,但來自JSON數組等數據缺失這裏只設置特性,這是顯而易見的接受工作和門檻。由於JSON是動態的,因此如何編寫自定義控件,以便無論數據的動態如何,我都可以傳遞完整的數據以進行控制。

回答

2

你可以使用類型:「任何」你的項目和不使用在所有的元素custom.barNlineChartControl:

編輯:爲聚合控制你有這個使用屬性聚合對象的生命週期案件。

sap.ui.core.Control.extend("control.barNlinechart", { 
    /* the control API */ 
    metadata : { 
     properties : { 
      "items" : { type: "any" } 
     }, 
     events: { 
      "select" : {}, 
      "selectEnd": {}    
     }   
    }, 

,然後在您的視圖:

multiBarLineGraph = new control.barNlinechart({ 
    layoutData: new sap.ui.layout.GridData({span: "L12 M12 S12"}), 
    items: { path : "/genericData" } 
}), 

this.getItems()將返回任何已被設置/綁定的數組。

+0

它給出了這個錯誤'錯誤:元素control.barNlinechart #__nlinechart0 !, – SiddP

+0

聚合項缺少模板或工廠函數是的,我的錯。您必須使用屬性而不是聚合。更新了我的答案。 – schnoedel

+0

工作。謝謝! – SiddP