Unable to populate an HTML table with JSON data

Can you assist me in populating a table using data from a JSON response?

[
  {
    "id": 1,
    "firstName": "James",
    "nickNames": [
      {}
    ]
  },
  {
    "id": 2,
    "firstName": "Linda",
    "nickNames": [
      {
        "id": 2,
        "name": "lin"
      },
      {
        "id": 3,
        "name": "l1n"
      },
      {
        "id": 1,
        "name": "Lin"
      }
    ]
  }
]

I want to populate the table as follows:

<table id="users" class="table table-striped table-hover">
  <thead>
  <tr>
    <th>#</th>
    <th>Name</th>
    <th>Nicknames</th>
  </tr>
  </thead>
  <tbody>
  <tr>
    <td>1</td>
    <td>James</td>
    <td>none</td>
  </tr>
  <tr>
    <td>2</td>
    <td>Linda</td>
    <td>lin, l1n, Lin</td>
  </tr>
  </tbody>
</table>

Here is what I have done so far:

'use strict';

angular.module('myAppControllers', [])
  .controller('UserCtrl', ['$scope', 'User', function ($scope, User) {
    User.query().$promise.then(function (data) {
        $scope.users = data;
      }, function (reason) {
        console.log('Failed: ' + reason)
      }
    )
  }]);

This is my template:

<div class="row">
  <div class="page-header">
    <h1>Users</h1>
  </div>
  <table class="table table-striped table-hover">
    <thead>
    <tr>
      <th>#</th>
      <th>Name</th>
      <th>Nicknames</th>
    </tr>
    </thead>
    <tbody>
    <tr ng-repeat="user in users | orderBy:'id' track by user.id">
      <td>{{ user.id }}</td>
      <td>{{ user.firstName }}</td>
      <td ng-repeat="nickname in user.nickNames | emptyUser">{{ nickname.name }}</td>
    </tr>
    </tbody>
  </table>
</div>

My issue lies in the fact that with the nested array, I am unable to separate entries into individual <td> tags.

p.s. I am unable to modify the API provided by the server.

I have searched for similar problems on StackOverflow, but none seem to address my specific issue.

Answer №1

Avoid redundancy with the td tag by incorporating a span inside the td:

<td><span ng-repeat="nickname in user.nickNames | emptyUser">{{ nickname.name }} </span></td>

Answer №2

For more information, you can check out this link: http://jsbin.com/vined/1/edit


  <table class="table table-striped table-hover">
    <thead>
    <tr>
      <th>#</th>
      <th>Name</th>
      <th>Nicknames</th>
    </tr>
    </thead>
    <tbody>
    <tr ng-repeat="user in data | orderBy:'id' track by user.id">
      <td>{{ user.id }}</td>
      <td>{{ user.firstName }}</td>
      <td><p ng-repeat="nickname in user.nickNames | emptyUser ">{{ nickname.name }}</p></td>
    </tr>
    </tbody>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

We are experiencing difficulties rendering flash messages in Expressjs with Handlebars(hbs) and express-messages using flash-connect

I'm working on an express app and I want to display flash messages when a user signs up using a form. The technologies I am utilizing include Node.js, Express.js, Handlebars(hbs), connect-flash, and express-messages. To make finding errors easier, I ...

Anticipated the start of an array but received a string instead at the first line and first column

Encountered an error stating "Expected BEGIN_OBJECT but was STRING at line 1 column 1". Seeking assistance in resolving this issue. I have a ListOffers class that is designed to create objects from parsed JSON data. The code for the ListOffers and Offer c ...

What are the steps for conducting a component test with material ui?

My current component is built using . import React from 'react'; import { AppBar, Toolbar } from 'material-ui'; import { Typography } from 'material-ui'; import { MuiThemeProvider, createMuiTheme } from 'material-ui/sty ...

Moving towards the target is a Three.js object

I'm struggling with an issue where the x position of my object moves quicker than the z position, or vice versa. How can I make my object slow down for the x position if the z position requires more time to move? This is the code in my animation func ...

Using JQuery to loop through elements when clicked

I've been experimenting with different methods to iterate through a series of 4 li elements, each having a class of "item_n" where n is a number from 1 to 4. I want the iteration to increase by 1 with every click. Despite my efforts, my code isn' ...

Is it possible to utilize checkbox images for type="checkbox" in ng-repeat with AngularJS?

Hi there, I'm currently attempting to add a checkbox image for input type="checkbox" using ng-repeat. I've styled everything accordingly, but when I apply ng-repeat, it only works for the first item in the list. Here is what my CSS file looks lik ...

Unexpected Issue with JavaScript Ajax (Using jQuery.post): The Promise State Turns to "Rejected"

Recently, I've been encountering some issues while trying to debug my jQuery.post() call. The responses I'm getting are quite puzzling and I'm at a loss on how to proceed next. If anyone has any suggestions or insights, I would greatly appre ...

Displaying the chosen menu item in AngularJS: A step-by-step guide

I'm facing an issue with highlighting menu items in my AngularJS application. When a menu item is clicked, it should be highlighted. Here's how my menu structure looks: <ul class="nav nav-stacked" data-ng-controller="ProjectListController" &g ...

A method in JavaScript to fetch a single variable using the GET request

Although I am new to writing JavaScript, I am currently working on an iOS application that will make use of JavaScriptCore's framework to interpret a piece of javascript code in order to obtain a specific variable. My goal is to establish a GET reques ...

How can I stop the body from scrolling to 100% height when the virtual keyboard is displayed?

My chat app has sticky header and footer elements. To handle the mobile virtual keyboard opening, I adjust the document's height using window.visualViewport.height. For example, if the browser's height is 1000px and the virtual keyboard takes up ...

Refreshing Android ViewPager with API Results

I've implemented an android ViewPager in my project. Initially, I set the viewpager with an empty list because I plan to populate it with dynamic data fetched from a REST API response. With an empty adapter list, there are no pages shown at first. T ...

Display a modal when a user is not authorized in vue-router

After stumbling upon this post on Medium, I decided to implement its concepts into my project. My goal was to verify a user's authorization to access a particular route, and if unauthorized, display a modal pop-up. In order to achieve this, I made s ...

Different Slide Library Fullpage.js

Is it feasible to have two sections with the same slide in fullpage.js? For example, if I'm currently on slide 1 of section 1 and then move to slide 2 of section 1, can section 2 automatically switch to slide 2 as well? I'm curious if this funct ...

The script ceased functioning immediately following the inclusion of a case-insensitive search feature and interactive images

In the process of creating my inaugural project featuring a collection of images, I wanted to include a filter/search bar at the top. This bar would dynamically filter the displayed pictures based on user input. For example, typing "Aatrox" into the search ...

Cancel all existing $resource $promises in AngularJS when a new request is made

One form contains two fields: username and password. When the username model changes, an API request is made for validation. If x calls are made with t milliseconds in between to a $resource factory, it is important to know : if the last $promise rece ...

What is the best approach to transition a monolithic MEAN stack application to a Microservices architecture?

Our team has created a comprehensive MEAN stack application for streamlining employee and inventory management in the workplace. What's the best way to utilize the Employees functionality as a separate service from Inventory? ...

Learn how to seamlessly transfer data between MySQL databases and JSON format, and leverage it for creating dynamic Google table charts

I have a database in MySQL with an employee table. The table columns include empno, ename, sal, etc. I am creating a custom query page where the user can input their query in a textarea and click on submit. The goal is to retrieve data from MySQL based on ...

Can you explain the process behind /^image/w+/.test(file.type)?

I came across this piece of code that I'm testing to determine if a file added to a canvas is an image. I'm curious about the functionality behind it. Just to clarify, "file" refers to a FileList obtained from an input element. if (/^image\ ...

Struggling to destructure props when using getStaticProps in NextJS?

I have been working on an app using Next JS and typescript. My goal is to fetch data from an api using getStaticProps, and then destructure the returned props. Unfortunately, I am facing some issues with de-structuring the props. Below is my getStaticProp ...

creating a JSON array within a function

I am currently developing an Angular application and working on a component with the following method: createPath(node, currentPath = []){ if(node.parent !==null) { return createPath(node.parent, [node.data.name, ...currentPath]) } else { retu ...