2016-12-21 35 views
0

我有以下的JSON:如何複雜JSON結合表

[ 
{ Hour:"01:00:00", 
    Name: "Den", 
    Agent:[ 
      {Slot : 1,Enabled : 1}, 
      {Slot : 2,Enabled : 1}, 
      {Slot : 3,Enabled : 1}, 
      {Slot : 4,Enabled : 1}, 
      {Slot : 5,Enabled : 1}, 
      {Slot : 6,Enabled : 1}, 
      {Slot : 7,Enabled : 1}, 
      {Slot : 8,Enabled : 1}, 
      {Slot : 9,Enabled : 1} 
     ] 
    } 
]... 

我需要創建一個列出了「一小時」在一列和另一列的表格列出它們有物體「代理」

實施例:

________________________________________________________ 
| Hour  | Slot 1 | Slot 2 | Slot 3 | Slot 4 | Slot 5 | 
--------------------------------------------------------- 
| 10:00:00 | 1  | 2  | 3  | 4  | 5  |... 

創建一個表,但只加載時間和完全的 「代理」 對象:

表:

oTable.addColumn(new sap.ui.table.Column({ 
       label: new sap.ui.commons.Label({text: "Hour"}), 
       template: new sap.ui.commons.TextView({text: "{Hour}"}), 
       width : '125px' 
      }) 
    ); 

for(var i; i < 9; i++){ 
     oTable.addColumn(
        new sap.ui.table.Column({ 
         label: new sap.ui.commons.Label({text: "Slot "+i}), 
         template: new sap.ui.core.Icon({ 
         src: { 
          path : "Agent/", 
            formatter: function(v) { 
             if (v != null) { 
              if(v.Enabled == 1){ 
              return "sap-icon://save"; 
              }else{ 
              return "sap-icon://future"; 
              } 
             } 
            } 
         }, 
         size: "20px", 
        color: { 
         path : "Agent/", 
          formatter: function(v) { 
           if (v != null) { 
            if(v.Enabled== 1){ 
            this.attachEvent("press",function(oEvent)   {Alert("green")}) 
            return v.Slot != -1 ? "#f08e00" : "green" 
            }else{ 
            this.attachEvent("press",function(oEvent) {Alert("#bfbfbf")}) 
            return "#bfbfbf" 
            } 

           } 
         } 
        }, 
        hoverColor: "black", 
        activeColor: "black", 
        width : "100%", 
        }), 
         width: "70px" 
       }) 
       ); 
      } 
    } 

任何想法如何做到這一點? 我想把「我」當作「代理/我」,它可以工作,但是當表格啓用「滾動」時,它會返回以渲染行和無序事件的圖標:/

+0

你可以用require字段創建一個數組,然後將它傳遞給表。然後你可以得到它。 –

+0

你能舉個例子嗎?/? –

+0

相對路徑爲:「Agent」+ i。我沒有面對你提到的滾動問題,並顯示正確的圖標。您可以使用SAPUI5 CDN檢查您的UI5版本是否存在滾動問題嗎? –

回答

0

您是否正確使用"Agent/"+i作爲綁定路徑。

但是您不能在格式化程序中附加事件處理程序。初始化圖標時,您應該綁定一次事件。事件處理函數提供了一個事件對象,其中包含對已被單擊的圖標實例的引用。你可以獲得應該是行的綁定上下文。再次使用您的i獲取Column對象。

function createEventHandler(column){ 
    return function(oEvent) { 
     var context = oEvent.getSource().getBindingContext(); 
     var v = context.getProperty("Agent/" + column); 
     if (v){ 
     if (v. Enabled) { 
      Alert("green"); 
     } else { 
      Alert("#bfbfbf"); 
     } 
     } 
    } 
}; 
for(var i; i < 9; i++){ 
     oTable.addColumn(
        new sap.ui.table.Column({ 
         label: new sap.ui.commons.Label({text: "Slot "+i}), 
         template: new sap.ui.core.Icon({ 
         src: { 
          path : "Agent/"+i, 
            formatter: function(v) { 
             if (v != null) { 
              if(v.Enabled == 1){ 
              return "sap-icon://save"; 
              }else{ 
              return "sap-icon://future"; 
              } 
             } 
            } 
         }, 
         size: "20px", 
        color: { 
         path : "Agent/"+i, 
          formatter: function(v) { 
           if (v != null) { 
            if(v.Enabled== 1){ 
            return v.Slot != -1 ? "#f08e00" : "green" 
            }else{ 
            return "#bfbfbf" 
            } 

           } 
         } 
        }, 
        hoverColor: "black", 
        activeColor: "black", 
        width : "100%", 
        press: createEventHandler(i) //create a closure over the current value of i 
        }), 
         width: "70px" 
       }) 
       ); 
      }