首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用数组迭代JSON对象并使用角JS填充表

如何使用数组迭代JSON对象并使用角JS填充表
EN

Stack Overflow用户
提问于 2016-11-02 17:57:46
回答 2查看 340关注 0票数 0

我有一个REST服务,它返回类似于以下格式的JSON数据:

代码语言:javascript
运行
复制
{
    "Africa" : [{
            "country" : "Nigeria",
            "capital" : "Abuja"
        }, {
            "country" : "Kenya",
            "capital" : "Nairobi"
        }, {
            "country" : "Ghana",
            "capital" : "Accra"
        }
    ],
    "Asia" : [{
            "country" : "India",
            "capital" : "New Delhi"
        }, {
            "country" : "China",
            "capital" : "Beijing"
        }
    ],
    "Europe" : [{
            "country" : "France",
            "capital" : "Paris"
        }
    ]
}

我应该如何解析这个结果并填充一个与下面的图像类似的表:

http://jsfiddle.net/p7yLztwg/

EN

Stack Overflow用户

发布于 2016-11-02 18:19:07

你可以试试这样的方法:

代码语言:javascript
运行
复制
<table>
  <thead>
    <tr>
      <th>Continent</th>
      <th>Country</th>
      <th>Capital</th>
    </tr>
  </thead>
  <tbody ng-repeat="(continent, countries) in yourjsondata">
    <tr ng-repeat="country in countries">
      <td ng-if="$first" rowspan={{countries.length}}>{{ continent }}</td>
      <td>{{ country.country }}</td>
      <td>{{ country.capital }}</td>
    </tr>
  </tbody>
</table>

这为每个大陆创造了一个tbody,为每个国家创造了一个tr。只有大陆的第一行才能得到该大陆名称的单元格。该单元格还会获得一个rowspan属性,以便它跨越多个行。

演示

票数 3
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40386680

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档