简体   繁体   English

如何在 Angular JS 中更改 JSON 数据格式?

[英]how to change JSON data format in angular JS?

I am new to Angular JS.我是 Angular JS 的新手。

My JSON data is:我的 JSON 数据是:

{
"CheckList": [
   {
    "UnitClass": "Budget Space",
    "CheckListCategoryId": 1,
    "CheckListCategory": "DOORS",
    "CheckListItemId": 2,
    "CheckListItem": "Deadbolt, Locksets, and keys all functioning"
   }, 
   {
    "UnitClass": "Budget Space",
    "CheckListCategoryId": 2,
    "CheckListCategory": "WINDOWS",
    "CheckListItemId": 46,
    "CheckListItem": "Windows are operable"
   }, 
   {
    "UnitClass": "Budget Space",
    "CheckListCategoryId": 2,
    "CheckListCategory": "WINDOWS",
    "CheckListItemId": 13,
    "CheckListItem": "Window pane is not broken or cracked"
   }
}

And I want to change it to:我想把它改成:

{
"CheckList": [
   {
    "UnitClass": "Budget Space",
    "CheckListCategoryId": 1,
    "CheckListCategory": "DOORS",
    "CheckListItemId": 2,
    "CheckListItem": "Deadbolt, Locksets, and keys all functioning"
   }, 
   {
    "UnitClass": "Budget Space",
    "CheckListCategoryId": 2,
    "CheckListCategory": "WINDOWS",
    "CheckListItems": [
        {
            "CheckListItem": "Windows are operable",
            "CheckListItemId": 46,
        },
        {
            "CheckListItem": "Window pane is not broken or cracked",
            "CheckListItemId": 13,
        }]
   }
}

Why I am doing this:为什么我这样做:

I have a Bootstrap collapse list as我有一个 Bootstrap 折叠列表作为

DOORS
WINDOWS视窗

which i am getting using ng-repeat, adding a filter 'unique' on CheckListCategoryId我正在使用 ng-repeat,在CheckListCategoryId上添加一个“唯一”过滤器

But the problem is with my current JSON, only single CheckListItem is getting posted but in many cases there are two.但问题在于我当前的 JSON,只有一个CheckListItem被发布,但在许多情况下有两个。 like in WINDOWS.就像在 WINDOWS 中一样。

How to change the JSON data??如何更改 JSON 数据?

One more thing.还有一件事。 Is my current way correct or there is any other alternative??我目前的方法是正确的还是有其他选择?

You could use groupBy from angular-filter to group your data.您可以使用angular-filter 中的groupBy对数据进行分组。 Then you can filter your data by category like this ctrl.checkList | groupBy: 'CheckListCategory'然后你可以像这样ctrl.checkList | groupBy: 'CheckListCategory'按类别过滤你的数据ctrl.checkList | groupBy: 'CheckListCategory' . ctrl.checkList | groupBy: 'CheckListCategory'

Please have a look at the demo below or in this fiddle .请看下面的演示或在这个小提琴中

I've used a bootstrap accordion to display the data but collapse should also work.我使用引导手风琴来显示数据,但折叠也应该有效。 I'm not sure how it should look like with the collapse that's why I used an accordion.我不确定倒塌时它应该是什么样子,这就是我使用手风琴的原因。

 angular.module('demoApp', ['ui.bootstrap', 'angular.filter']) .controller('MainController', MainController); function MainController($http, $scope) { var vm = this; $http.jsonp('http://www.mocky.io/v2/56183175100000e5387222f9?callback=JSON_CALLBACK').then(function(response) { console.log(response); vm.checkList = response.data[0].CheckList; }); }
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.14.0/ui-bootstrap-tpls.js"></script> <script src="https://cdn.rawgit.com/a8m/angular-filter/master/dist/angular-filter.js"></script> <div ng-app="demoApp" ng-controller="MainController as ctrl"> <uib-accordion> <uib-accordion-group heading="{{cat[0].CheckListCategory}}" ng-repeat="cat in ctrl.checkList | groupBy: 'CheckListCategory'"> <ul> <li ng-repeat="item in cat"> {{item.CheckListItem}} </li> </ul> </uib-accordion-group> </uib-accordion> </div>

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

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