简体   繁体   English

如何将复杂的JSON绑定到表

[英]How to binding complex JSON to table

I have the following 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} 
         ]
  }
]...

I need to create a table that lists the "Hour" in a column and the other columns list them with the object "Agent" 我需要创建一个表,该表在一个列中列出“小时”,其他列与对象“代理”一起列出

Example : 范例:

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

Create a table but only load the time and the complete "Agent" object: 创建一个表,但只加载时间和完整的“ Agent”对象:

Table: 表:

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"
                })
                );  
            }
     }

Any idea how to do it? 知道怎么做吗? It occurred to me to put the "i" as "Agent / i", it works, but when the table enables the "Scroll" it returns to render the rows and of disorder the events by icons :/ 我想到将“ i”设置为“ Agent / i”,它可以工作,但是当表启用“ Scroll”时,它返回以通过图标来渲染行和混乱的事件:/

You are right to use "Agent/"+i as the binding path. 您可以使用"Agent/"+i作为绑定路径。

But you cannot attach the event handlers inside of the formatters. 但是您不能在格式化程序内部附加事件处理程序。 You should bind once to the event when initializing the icon. 初始化图标时,应将一次绑定到事件。 The eventhandler function is supplied with an event object containing a reference to the icon instance that has been clicked. eventhandler函数随事件对象一起提供,其中包含对已单击图标实例的引用。 with that you can get the binding context which should be the row. 有了它,您可以获得应该是该行的绑定上下文。 Use your i again to get the Column object. 再次使用您的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"
                })
                );  
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM