Looping through multiple table rows with AngularJS ng-repeat

I am currently working on making the code below functional, which involves data being retrieved from an API:

...
<tbody ng-repeat="verbinding in data.connection" style="border-bottom:2px solid black;">

    <tr>
        <td></td>
        <td></td>
        <td>{{verbinding.departure.time  * 1000 | date:'HH:mm'}}</td>
        <td>{{verbinding.departure.platform}}</td>
        <td><a href="/liveboard/{{verbinding.departure.stationinfo.id}}">{{verbinding.departure.station}}</td>
    </tr>

    <tr ng-if="verbinding.departure.vehicle == verbinding.arrival.vehicle">
        <td colspan = "5" style="text-align:center;">{{verbinding.departure.vehicle}}</td>
    </tr>
<span ng-if="verbinding.departure.vehicle != verbinding.arrival.vehicle" ng-repeat="via in verbinding.vias.via">
    <tr>
        <td>{{via.arrival.time * 1000 | date:'HH:mm'}}</td>
        <td>{{via.arrival.platform}}</td>
        <td>{{via.departure.time * 1000 | date:'HH:mm'}}</td>
        <td>{{via.departure.platform}}</td>
        <td><a href = "/liveboard/{{via.stationinfo.id}}">{{via.station}}</a></td>
    </tr>
    <tr>
        <td colspan = "5">{{via.vehicle.id}}</td>
    </tr>
</span>
...

The above code is not functioning as intended. I am incorporating the span element to accommodate two tr elements. The code snippet below works properly:

...
<tr ng-if="verbinding.departure.vehicle != verbinding.arrival.vehicle" ng-repeat="via in verbinding.vias.via">
    <td>{{via.arrival.time * 1000 | date:'HH:mm'}}</td>
    <td>{{via.arrival.platform}}</td>
    <td>{{via.departure.time * 1000 | date:'HH:mm'}}</td>
    <td>{{via.departure.platform}}</td>
    <td><a href = "/liveboard/{{via.stationinfo.id}}">{{via.station}}</a></td>
</tr>
...

However, this setup only includes one tr, whereas I require two.

Answer №1

If you're searching for a way to repeat HTML code, consider using ng-repeat-start and ng-repeat-end.

According to the details provided in the documentation:

The ng-repeat-start directive functions similarly to ng-repeat, but it duplicates all the HTML code (including the tag it's defined on) up to and including the ending HTML tag where ng-repeat-end is placed.

You can implement something like this...

<tbody ng-repeat="binding in data.connections" style="border-bottom:2px solid black;">

    <tr>
        <td></td>
        <td></td>
        <td>{{binding.departure.time * 1000 | date:'HH:mm'}}</td>
        <td>{{binding.departure.platform}}</td>
        <td><a href="/liveboard/{{binding.departure.stationinfo.id}}">{{binding.departure.station}}</td>
    </tr>

    <tr ng-if="binding.departure.vehicle == binding.arrival.vehicle">
        <td colspan="5" style="text-align:center;">{{binding.departure.vehicle}}</td>
    </tr>
    <tr ng-repeat-start="via in binding.vias.via">
        <td>{{via.arrival.time * 1000 | date:'HH:mm'}}</td>
        <td>{{via.arrival.platform}}</td>
        <td>{{via.departure.time * 1000 | date:'HH:mm'}}</td>
        <td>{{via.departure.platform}}</td>
        <td><a href="/liveboard/{{via.stationinfo.id}}">{{via.station}}</a></td>
    </tr>
    <tr ng-repeat-end>
        <td colspan="5">{{via.vehicle.id}}</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

Guidelines on concealing the parent component while the child component is loading in Angular 2

In my latest project, the view setup is as follows: https://i.sstatic.net/VxJ9U.png Upon page load, the Parent Item Description should be visible while the Selected sub item description remains hidden. When a Sub Item x is selected, the Parent Item Desc ...

Filter through the array of objects using the title key

I'm attempting to extract specific data by filtering the 'page_title' key. Below is a snippet of my JSON object: { "page_components": [ { "page_title": "My Account", "row_block": [ { "heading": "", "sub_headi ...

What is the best way to simulate our services for controller testing?

Currently delving into angular js and experimenting with testing a controller. Here is the service I am using: angular.module('test'){ service.getAllServices = function() { var fullPath = url var deferre ...

Real-time updating fails to trigger the ng-change event in the text field

I am encountering an issue with a textbox in angularjs. Whenever I update the data in the text field using some method (such as clicking a button), the ng-change event is not triggered. Please take a look at this Plnkr example: [https://plnkr.co/edit/32eE ...

Is there a viable substitute for websockets that can be utilized on shared hosting platforms?

Are there any viable alternatives for websockets that can be used with shared hosting? While I'm aware of node.js, socket.io, and Express.js, I'm unable to use them in a shared hosting environment. If there are other options available for creatin ...

What is the best way to store the properties of an object retrieved from the backend in separate variables?

I am currently working on retrieving the attributes of my objects (events) from the backend and storing them in separate variables for a calendar. Here is the code snippet I have been using: EDIT: function fetchEvents(object){ TimeSlotsModel.all ...

Configuring the "trust proxy" setting in Express for Node.js with CloudFront

I am utilizing an Express backend with AWS Cloudfront. How can I properly configure the trust proxy setting for AWS Cloud Front? app.set('trust proxy', function (ip) { if ( ???????????? ) return true; // trusted IPs else return false; }); A ...

The curious conduct of wild safari creatures

I have been using JavaScript to split my ePub chapter into pages by wrapping the code with new divs that represent separate pages. I have also created CSS styles for these new divs. Everything works perfectly when the file has a filename extension of &apos ...

`Is there a way to retrieve the ID of an element upon clicking on it?`

Can you help me with a JavaScript query? I have two HTML div elements with ids of 'div1' and 'div2'. When I click on any of the divs, I want to display the id of the clicked div. Any thoughts? <div id="div1"></div> <div ...

Update the useMemo state value after the initial function block has been executed

Currently, I have a list of characters from Dragon Ball Z that users can filter based on gender, race, or both using useMemo. let dbzCharacters = useMemo<CharacterObj[]>(() => { if (filterGenderValue && filterRaceValue) { retur ...

What are the most optimal configurations for tsconfig.json in conjunction with node.js modules?

Presently, I have 2 files located in "./src": index.ts and setConfig.ts. Both of these files import 'fs' and 'path' as follows: const fs = require('fs'); const path = require('path'); ...and this is causing TypeScr ...

Adjust the Material UI card to fill the maximum available height

I'm currently working with Material UI Card components. You can find more information here. My goal is to set a maximum height for these cards, ensuring that the text and images are displayed nicely. How should I approach this? Below is a snippet of ...

Combining Various Data Types in a Flexible List

I'm looking for a way to dynamically add rows to a table. Right now, I have the input type on the server (whether it's int, bool, string, etc), but I want to know how to implement a field accept combobox. Here is the code in cshtml: <tr ng-r ...

Utilizing PHP and jQuery to dynamically populate Google Maps with multiple markers

I am currently working on integrating a Google map with a database to dynamically display multiple markers using PHP and MySQL. Below is the code I have been using: <?php //This section retrieves data from the database for later use in jQuery //CREATE ...

Using Node.js to access the public stream of app.net for streaming purposes

Exploring the world of streaming and HTTP long living connections for the first time. This is where I'm at: const accessToken = require('./config.js').accessToken; const https = require('https'); const req = https.request({ ...

We are currently only accepting GET requests, please refrain from making any POST requests at this

I've been facing an issue while trying to make an HTTP call from AngularJS to a Spring controller and post JSON data. The error that I'm encountering in AngularJS is: angular.js:10661 POST http://localhost:8080/shopng/product/add 404 (Not Found ...

Counting the occurrence of a specific class within an element using jQuery

Within my table, each row is assigned one or more classes based on its region. This is the structure of my table: <table> <thead> <th>Title</th> <th>Name</th> </thead> <tbody> <tr class="emea ...

Issues with Input Functionality in Angular and Internet Explorer 11

I am facing a critical problem with ng-model inputs in Internet Explorer (version 11 and earlier,) while they work perfectly fine in all other browsers. This issue started just last week, without any changes made to this part of our application or any prio ...

Replicate a click using jQuery to retrieve a filtered multigallery based on the URL

I have a website that was built using Elementor for a photographer. The site features an Elementor Multigallery with a filter at the top. I am looking to create external links that will direct users to specific subpages showing only filtered items. For ex ...

Guide to creating a synchronous wrapper for jQuery ajax methods

I've made the decision to switch from synchronous ajax calls to asynchronous ones due to lack of support in most modern browsers. My code is currently reliant on synchronous (and dynamic) ajax calls to client-side functions that must be completed befo ...