How to build table with rowspan from array of objects in AngularJS












0















I have similar problem as in this question: AngularJS repeat with table and rowspan.



I want to build table with rowspan from object.



But I use array of objects instead of single object as data for table.



Example:



[
{
"key": "key 1",
"values": [
{
"value": "value-1"
},
{
"value": "value-2",
}
]
},
{
"key": "key 2",
"values": [
{
"value": "value-3"
}
]
}


instead of



{
key1:[1,2],
key2:[3,4,5]
}




As a result I want to transform this array:



[
{
"login": "Affiliate 1",
"referrals": [
{
"login": "referral-1",
"bonusAmount": 100.00
},
{
"login": "referral-2",
}
]
},
{
"login": "Affiliate 2",
"referrals": [
{
"login": "referral-3",
"bonusAmount": 300.00
}
]
},
{
"login": "Affiliate 3",
"referrals": [
{}
]
}
]


to table:






<table border="1">
<tr>
<th scope="col">Affiliate name</th>
<th scope="col">Referral name</th>
<th scope="col">Affiliate bonus</th>
</tr>
<tr>
<td rowspan="2">Affiliate 1</td>
<td>referral-1</td>
<td>$100.00</td>
</tr>
<tr>
<td>referral-2</td>
<td></td>
</tr>
<tr>
<td>Affiliate 2</td>
<td>referral-3</td>
<td>$300.00</td>
</tr>
<tr>
<td>Affiliate 3</td>
<td></td>
<td></td>
</tr>
</table>












share|improve this question





























    0















    I have similar problem as in this question: AngularJS repeat with table and rowspan.



    I want to build table with rowspan from object.



    But I use array of objects instead of single object as data for table.



    Example:



    [
    {
    "key": "key 1",
    "values": [
    {
    "value": "value-1"
    },
    {
    "value": "value-2",
    }
    ]
    },
    {
    "key": "key 2",
    "values": [
    {
    "value": "value-3"
    }
    ]
    }


    instead of



    {
    key1:[1,2],
    key2:[3,4,5]
    }




    As a result I want to transform this array:



    [
    {
    "login": "Affiliate 1",
    "referrals": [
    {
    "login": "referral-1",
    "bonusAmount": 100.00
    },
    {
    "login": "referral-2",
    }
    ]
    },
    {
    "login": "Affiliate 2",
    "referrals": [
    {
    "login": "referral-3",
    "bonusAmount": 300.00
    }
    ]
    },
    {
    "login": "Affiliate 3",
    "referrals": [
    {}
    ]
    }
    ]


    to table:






    <table border="1">
    <tr>
    <th scope="col">Affiliate name</th>
    <th scope="col">Referral name</th>
    <th scope="col">Affiliate bonus</th>
    </tr>
    <tr>
    <td rowspan="2">Affiliate 1</td>
    <td>referral-1</td>
    <td>$100.00</td>
    </tr>
    <tr>
    <td>referral-2</td>
    <td></td>
    </tr>
    <tr>
    <td>Affiliate 2</td>
    <td>referral-3</td>
    <td>$300.00</td>
    </tr>
    <tr>
    <td>Affiliate 3</td>
    <td></td>
    <td></td>
    </tr>
    </table>












    share|improve this question



























      0












      0








      0








      I have similar problem as in this question: AngularJS repeat with table and rowspan.



      I want to build table with rowspan from object.



      But I use array of objects instead of single object as data for table.



      Example:



      [
      {
      "key": "key 1",
      "values": [
      {
      "value": "value-1"
      },
      {
      "value": "value-2",
      }
      ]
      },
      {
      "key": "key 2",
      "values": [
      {
      "value": "value-3"
      }
      ]
      }


      instead of



      {
      key1:[1,2],
      key2:[3,4,5]
      }




      As a result I want to transform this array:



      [
      {
      "login": "Affiliate 1",
      "referrals": [
      {
      "login": "referral-1",
      "bonusAmount": 100.00
      },
      {
      "login": "referral-2",
      }
      ]
      },
      {
      "login": "Affiliate 2",
      "referrals": [
      {
      "login": "referral-3",
      "bonusAmount": 300.00
      }
      ]
      },
      {
      "login": "Affiliate 3",
      "referrals": [
      {}
      ]
      }
      ]


      to table:






      <table border="1">
      <tr>
      <th scope="col">Affiliate name</th>
      <th scope="col">Referral name</th>
      <th scope="col">Affiliate bonus</th>
      </tr>
      <tr>
      <td rowspan="2">Affiliate 1</td>
      <td>referral-1</td>
      <td>$100.00</td>
      </tr>
      <tr>
      <td>referral-2</td>
      <td></td>
      </tr>
      <tr>
      <td>Affiliate 2</td>
      <td>referral-3</td>
      <td>$300.00</td>
      </tr>
      <tr>
      <td>Affiliate 3</td>
      <td></td>
      <td></td>
      </tr>
      </table>












      share|improve this question
















      I have similar problem as in this question: AngularJS repeat with table and rowspan.



      I want to build table with rowspan from object.



      But I use array of objects instead of single object as data for table.



      Example:



      [
      {
      "key": "key 1",
      "values": [
      {
      "value": "value-1"
      },
      {
      "value": "value-2",
      }
      ]
      },
      {
      "key": "key 2",
      "values": [
      {
      "value": "value-3"
      }
      ]
      }


      instead of



      {
      key1:[1,2],
      key2:[3,4,5]
      }




      As a result I want to transform this array:



      [
      {
      "login": "Affiliate 1",
      "referrals": [
      {
      "login": "referral-1",
      "bonusAmount": 100.00
      },
      {
      "login": "referral-2",
      }
      ]
      },
      {
      "login": "Affiliate 2",
      "referrals": [
      {
      "login": "referral-3",
      "bonusAmount": 300.00
      }
      ]
      },
      {
      "login": "Affiliate 3",
      "referrals": [
      {}
      ]
      }
      ]


      to table:






      <table border="1">
      <tr>
      <th scope="col">Affiliate name</th>
      <th scope="col">Referral name</th>
      <th scope="col">Affiliate bonus</th>
      </tr>
      <tr>
      <td rowspan="2">Affiliate 1</td>
      <td>referral-1</td>
      <td>$100.00</td>
      </tr>
      <tr>
      <td>referral-2</td>
      <td></td>
      </tr>
      <tr>
      <td>Affiliate 2</td>
      <td>referral-3</td>
      <td>$300.00</td>
      </tr>
      <tr>
      <td>Affiliate 3</td>
      <td></td>
      <td></td>
      </tr>
      </table>








      <table border="1">
      <tr>
      <th scope="col">Affiliate name</th>
      <th scope="col">Referral name</th>
      <th scope="col">Affiliate bonus</th>
      </tr>
      <tr>
      <td rowspan="2">Affiliate 1</td>
      <td>referral-1</td>
      <td>$100.00</td>
      </tr>
      <tr>
      <td>referral-2</td>
      <td></td>
      </tr>
      <tr>
      <td>Affiliate 2</td>
      <td>referral-3</td>
      <td>$300.00</td>
      </tr>
      <tr>
      <td>Affiliate 3</td>
      <td></td>
      <td></td>
      </tr>
      </table>





      <table border="1">
      <tr>
      <th scope="col">Affiliate name</th>
      <th scope="col">Referral name</th>
      <th scope="col">Affiliate bonus</th>
      </tr>
      <tr>
      <td rowspan="2">Affiliate 1</td>
      <td>referral-1</td>
      <td>$100.00</td>
      </tr>
      <tr>
      <td>referral-2</td>
      <td></td>
      </tr>
      <tr>
      <td>Affiliate 2</td>
      <td>referral-3</td>
      <td>$300.00</td>
      </tr>
      <tr>
      <td>Affiliate 3</td>
      <td></td>
      <td></td>
      </tr>
      </table>






      javascript angularjs html-table angularjs-ng-repeat






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 11:12







      Roman Cherepanov

















      asked Nov 14 '18 at 10:57









      Roman CherepanovRoman Cherepanov

      8291830




      8291830
























          1 Answer
          1






          active

          oldest

          votes


















          3














          One option is to put the main loop over the tbody element in the table ng-repeat="item in items" and then use a nested loop over each tr ng-repeat="ref in item.referrals" where you put the rowspan condition on the first td <td ng-if="$index == 0" rowspan={{item.referrals.length}}>{{item.login}}</td>.



          Here's a working example






          var app = angular.module('app', );
          app.controller('ctrl', function ($scope) {
          $scope.items = [
          {
          "login": "Affiliate 1",
          "referrals": [
          {
          "login": "referral-1",
          "bonusAmount": 100.00
          },
          {
          "login": "referral-2",
          }
          ]
          },
          {
          "login": "Affiliate 2",
          "referrals": [
          {
          "login": "referral-3",
          "bonusAmount": 300.00
          }
          ]
          },
          {
          "login": "Affiliate 3",
          "referrals": [
          {}
          ]
          }
          ];

          });

          <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

          <div ng-app="app" ng-controller="ctrl">


          <table border="1">
          <tr>
          <th>Affiliate name</th>
          <th>Referral name</th>
          <th>Affiliate bonus</th>
          </tr>
          <tbody ng-repeat="item in items">
          <tr ng-repeat="ref in item.referrals">
          <td ng-if="$index == 0" rowspan={{item.referrals.length}}>{{item.login}}</td>
          <td>{{ref.login}}</td>
          <td>{{ref.bonusAmount > 0 ? '$' + ref.bonusAmount:''}}</td>
          </tr>
          </tbody>
          </table>

          </div>








          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53298599%2fhow-to-build-table-with-rowspan-from-array-of-objects-in-angularjs%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














            One option is to put the main loop over the tbody element in the table ng-repeat="item in items" and then use a nested loop over each tr ng-repeat="ref in item.referrals" where you put the rowspan condition on the first td <td ng-if="$index == 0" rowspan={{item.referrals.length}}>{{item.login}}</td>.



            Here's a working example






            var app = angular.module('app', );
            app.controller('ctrl', function ($scope) {
            $scope.items = [
            {
            "login": "Affiliate 1",
            "referrals": [
            {
            "login": "referral-1",
            "bonusAmount": 100.00
            },
            {
            "login": "referral-2",
            }
            ]
            },
            {
            "login": "Affiliate 2",
            "referrals": [
            {
            "login": "referral-3",
            "bonusAmount": 300.00
            }
            ]
            },
            {
            "login": "Affiliate 3",
            "referrals": [
            {}
            ]
            }
            ];

            });

            <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

            <div ng-app="app" ng-controller="ctrl">


            <table border="1">
            <tr>
            <th>Affiliate name</th>
            <th>Referral name</th>
            <th>Affiliate bonus</th>
            </tr>
            <tbody ng-repeat="item in items">
            <tr ng-repeat="ref in item.referrals">
            <td ng-if="$index == 0" rowspan={{item.referrals.length}}>{{item.login}}</td>
            <td>{{ref.login}}</td>
            <td>{{ref.bonusAmount > 0 ? '$' + ref.bonusAmount:''}}</td>
            </tr>
            </tbody>
            </table>

            </div>








            share|improve this answer




























              3














              One option is to put the main loop over the tbody element in the table ng-repeat="item in items" and then use a nested loop over each tr ng-repeat="ref in item.referrals" where you put the rowspan condition on the first td <td ng-if="$index == 0" rowspan={{item.referrals.length}}>{{item.login}}</td>.



              Here's a working example






              var app = angular.module('app', );
              app.controller('ctrl', function ($scope) {
              $scope.items = [
              {
              "login": "Affiliate 1",
              "referrals": [
              {
              "login": "referral-1",
              "bonusAmount": 100.00
              },
              {
              "login": "referral-2",
              }
              ]
              },
              {
              "login": "Affiliate 2",
              "referrals": [
              {
              "login": "referral-3",
              "bonusAmount": 300.00
              }
              ]
              },
              {
              "login": "Affiliate 3",
              "referrals": [
              {}
              ]
              }
              ];

              });

              <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

              <div ng-app="app" ng-controller="ctrl">


              <table border="1">
              <tr>
              <th>Affiliate name</th>
              <th>Referral name</th>
              <th>Affiliate bonus</th>
              </tr>
              <tbody ng-repeat="item in items">
              <tr ng-repeat="ref in item.referrals">
              <td ng-if="$index == 0" rowspan={{item.referrals.length}}>{{item.login}}</td>
              <td>{{ref.login}}</td>
              <td>{{ref.bonusAmount > 0 ? '$' + ref.bonusAmount:''}}</td>
              </tr>
              </tbody>
              </table>

              </div>








              share|improve this answer


























                3












                3








                3







                One option is to put the main loop over the tbody element in the table ng-repeat="item in items" and then use a nested loop over each tr ng-repeat="ref in item.referrals" where you put the rowspan condition on the first td <td ng-if="$index == 0" rowspan={{item.referrals.length}}>{{item.login}}</td>.



                Here's a working example






                var app = angular.module('app', );
                app.controller('ctrl', function ($scope) {
                $scope.items = [
                {
                "login": "Affiliate 1",
                "referrals": [
                {
                "login": "referral-1",
                "bonusAmount": 100.00
                },
                {
                "login": "referral-2",
                }
                ]
                },
                {
                "login": "Affiliate 2",
                "referrals": [
                {
                "login": "referral-3",
                "bonusAmount": 300.00
                }
                ]
                },
                {
                "login": "Affiliate 3",
                "referrals": [
                {}
                ]
                }
                ];

                });

                <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

                <div ng-app="app" ng-controller="ctrl">


                <table border="1">
                <tr>
                <th>Affiliate name</th>
                <th>Referral name</th>
                <th>Affiliate bonus</th>
                </tr>
                <tbody ng-repeat="item in items">
                <tr ng-repeat="ref in item.referrals">
                <td ng-if="$index == 0" rowspan={{item.referrals.length}}>{{item.login}}</td>
                <td>{{ref.login}}</td>
                <td>{{ref.bonusAmount > 0 ? '$' + ref.bonusAmount:''}}</td>
                </tr>
                </tbody>
                </table>

                </div>








                share|improve this answer













                One option is to put the main loop over the tbody element in the table ng-repeat="item in items" and then use a nested loop over each tr ng-repeat="ref in item.referrals" where you put the rowspan condition on the first td <td ng-if="$index == 0" rowspan={{item.referrals.length}}>{{item.login}}</td>.



                Here's a working example






                var app = angular.module('app', );
                app.controller('ctrl', function ($scope) {
                $scope.items = [
                {
                "login": "Affiliate 1",
                "referrals": [
                {
                "login": "referral-1",
                "bonusAmount": 100.00
                },
                {
                "login": "referral-2",
                }
                ]
                },
                {
                "login": "Affiliate 2",
                "referrals": [
                {
                "login": "referral-3",
                "bonusAmount": 300.00
                }
                ]
                },
                {
                "login": "Affiliate 3",
                "referrals": [
                {}
                ]
                }
                ];

                });

                <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

                <div ng-app="app" ng-controller="ctrl">


                <table border="1">
                <tr>
                <th>Affiliate name</th>
                <th>Referral name</th>
                <th>Affiliate bonus</th>
                </tr>
                <tbody ng-repeat="item in items">
                <tr ng-repeat="ref in item.referrals">
                <td ng-if="$index == 0" rowspan={{item.referrals.length}}>{{item.login}}</td>
                <td>{{ref.login}}</td>
                <td>{{ref.bonusAmount > 0 ? '$' + ref.bonusAmount:''}}</td>
                </tr>
                </tbody>
                </table>

                </div>








                var app = angular.module('app', );
                app.controller('ctrl', function ($scope) {
                $scope.items = [
                {
                "login": "Affiliate 1",
                "referrals": [
                {
                "login": "referral-1",
                "bonusAmount": 100.00
                },
                {
                "login": "referral-2",
                }
                ]
                },
                {
                "login": "Affiliate 2",
                "referrals": [
                {
                "login": "referral-3",
                "bonusAmount": 300.00
                }
                ]
                },
                {
                "login": "Affiliate 3",
                "referrals": [
                {}
                ]
                }
                ];

                });

                <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

                <div ng-app="app" ng-controller="ctrl">


                <table border="1">
                <tr>
                <th>Affiliate name</th>
                <th>Referral name</th>
                <th>Affiliate bonus</th>
                </tr>
                <tbody ng-repeat="item in items">
                <tr ng-repeat="ref in item.referrals">
                <td ng-if="$index == 0" rowspan={{item.referrals.length}}>{{item.login}}</td>
                <td>{{ref.login}}</td>
                <td>{{ref.bonusAmount > 0 ? '$' + ref.bonusAmount:''}}</td>
                </tr>
                </tbody>
                </table>

                </div>





                var app = angular.module('app', );
                app.controller('ctrl', function ($scope) {
                $scope.items = [
                {
                "login": "Affiliate 1",
                "referrals": [
                {
                "login": "referral-1",
                "bonusAmount": 100.00
                },
                {
                "login": "referral-2",
                }
                ]
                },
                {
                "login": "Affiliate 2",
                "referrals": [
                {
                "login": "referral-3",
                "bonusAmount": 300.00
                }
                ]
                },
                {
                "login": "Affiliate 3",
                "referrals": [
                {}
                ]
                }
                ];

                });

                <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

                <div ng-app="app" ng-controller="ctrl">


                <table border="1">
                <tr>
                <th>Affiliate name</th>
                <th>Referral name</th>
                <th>Affiliate bonus</th>
                </tr>
                <tbody ng-repeat="item in items">
                <tr ng-repeat="ref in item.referrals">
                <td ng-if="$index == 0" rowspan={{item.referrals.length}}>{{item.login}}</td>
                <td>{{ref.login}}</td>
                <td>{{ref.bonusAmount > 0 ? '$' + ref.bonusAmount:''}}</td>
                </tr>
                </tbody>
                </table>

                </div>






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 '18 at 11:42









                Marcus HöglundMarcus Höglund

                9,83862746




                9,83862746






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53298599%2fhow-to-build-table-with-rowspan-from-array-of-objects-in-angularjs%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Xamarin.iOS Cant Deploy on Iphone

                    Glorious Revolution

                    Dulmage-Mendelsohn matrix decomposition in Python