AngularJs使用自定义Json中的ng-repeat创建表

时间:2015-11-29 10:04:18

标签: javascript html angularjs html-table angularjs-ng-repeat

我想创建html表。我使用AngularJs指令 - ng-repeat。我有从服务器加载的数据。数据类型是text / json。我的Json结构在下面

String jsonResult = db.getResult(keyword);
if (jsonResult == null) {
    jsonResult = runSearch(keyword);
}
final String finalJsonResult = jsonResult;

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent newActivity = new Intent(ResultsActivity.this, MoviePage.class);
        newActivity.putExtra("index", position);
        newActivity.putExtra("json result", finalJsonResult); // Note use of "final" copy
        startActivity(newActivity);
    }
});

我试图创建表体,但它不起作用。我没有结果。

[  
   {  
      "branch":"wdads",
      "name":"STANDART",
      "Tags":[  

      ],
      "Rooms":[  
         {  
            "roomNo":"11",
            "branch":"wdads",
            "Schedule":[  
               {  
                  "id":"",
                  "status":"free",
                  "currentDate":"2015-10-31T20:00:00.000Z"
               },
               {  
                  "id":"",
                  "status":"free",
                  "currentDate":"2015-10-31T20:00:00.000Z"
               }
            ]
         }
      ]
   },
   {  
      "branch":"wdads",
      "name":"VIP",
      "Tags":[  

      ],
      "Rooms":[  
         {  
            "roomNo":"1",
            "branch":"wdads",
            "Schedule":[  
               {  
                  "id":"",
                  "status":"free",
                  "currentDate":"2015-12-29T20:00:00.000Z"
               },
               {  
                  "id":"",
                  "status":"free",
                  "currentDate":"2015-12-29T20:00:00.000Z"
               }
            ]
         },
         {  
            "roomNo":"2",
            "branch":"wdads",
            "Schedule":[  
               {  
                  "id":"",
                  "status":"free",
                  "currentDate":"2015-12-29T20:00:00.000Z"
               },
               {  
                  "id":"",
                  "status":"free",
                  "currentDate":"2015-12-29T20:00:00.000Z"
               }
            ]
         }
      ]
   }
] 

2 个答案:

答案 0 :(得分:1)

基本上你写的html是无效的html。

div的{​​{1}},tabletbodythead元素中,您无法tfoot个元素,如果您尝试这样做,那么该HTML将被视为无效的HTML。

您可以在表格的tr / th元素中包含该元素。基本上,这些元素用于在表格上显示数据。

要解决问题,你需要做几件事。

  1. 您应将td放在ng-repeat上,并多次重复tbody表格。
  2. 在显示tbody标记的表格行数据时,您应该使用tr元素。您直接使用td时应更改为tr
  3. <强>标记

    <tr><td ng-bind="$first ?caterory.name : ''"></td><tr>

    更详细的答案How to structure html while using table

    Working Plunkr

答案 1 :(得分:1)

做点什么

<tbody>
     <tr ng-repeat="caterory in categories">{{caterory.name}}<tr>
     <tr ng-repeat="room in caterory.Rooms">
        <td>{{room.roomNo}}</td>
        <td ng-repeat="schedule in room.Schedule">{{schedule.status}}</td>
     </tr>
</tbody>