Display header only once with AngularJS ng-repeat

I have two lists, namely the persons and animals, which I am displaying using ng-repeat. My goal is to present these two lists in a single table with a heading. Below is an example of how I envision presenting the data. I have attempted to achieve this using ng-show="$first", but the outcome did not meet my expectations.

https://i.sstatic.net/zGZYo.png

ID BIRTHDAY NAME
PERSONS {{ ps.id }} {{ ps.birthday | date: 'dd.MM.yyyy'}} {{ ps.name }}
ANIMALS {{ an.id }} {{ an.birthday | date: 'dd.MM.yyyy'}} {{ an.name }}

Answer №1

According to the official ng-repeat documentation, this solution should be effective.

...
<tr ng-repeat-start="ps in $ctrl.persons" ng-if="$ctrl.valuePersons == true && $first">
    <th>PERSONS</th>
    <td></td>
    <td></td>
</tr>
<tr ng-repeat-end ng-if="$ctrl.valuePersons == true && !$first">
    <td>{{ ps.id }}</td>
    <td>{{ ps.birthday | date: 'dd.MM.yyyy'}}</td>
    <td>{{ ps.name }}</td>
</tr>
...

Answer №2

Here's a detailed solution for you...

<table>
  <tr>
   <th>ID Number</th>
   <th>Date of Birth</th>
   <th>Full Name</th>
  </tr>
   <tr>
    <td>Individuals</td>
    <td></td>
    <td></td>
   </tr>
   <tr ng-repeat="person in $ctrl.people">
     <td>{{person.id}}<td/>
     <td>{{person.dob}}<td/>
     <td>{{person.name}}<td/>
   </tr>
<tr>
    <td>Creatures</td>
    <td></td>
    <td></td>
 </tr>
   <tr ng-repeat="animal in $ctrl.zooAnimals">
     <td>{{animal.id}}<td/>
     <td>{{animal.birthdate}}<td/>
     <td>{{animal.species}}<td/>
   </tr>

</table>

Answer №3

let profileApp = angular.module("MyProfile", [] );
                profileApp.controller("MyProfileCtrl", function($scope) {
                        $scope.items    = [{'title':'PERSON','data':[{'birth_day':'1/1/2011','name':'a1'},{'birth_day':'1/1/2011','name':'a2'},{'birth_day':'1/1/2011','name':'a3'}]},{'title':'ANIMALS','data':[{'birth_day':'1/1/2011','name':'a1'},{'birth_day':'1/1/2011','name':'a2'},{'birth_day':'1/1/2011','name':'a3'}]}]
                })
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="MyProfile" ng-controller="MyProfileCtrl">
                <table class="table" border="1">
                        <thead>
                                <tr>
                                        <th>ID</th>
                                        <th>BIRTHDAY</th>
                                        <th>Name</thu>
                                </tr>
                        </thead>
                        <tbody>
                                <tr ng-repeat-start="item in items" ng-init="main_ind= $index">
                                        <td  colspan="3">{{item['title']}}</td>
                                </tr>
                                <tr ng-repeat-end ng-repeat="val in item['data']">
                                        <td>{{$index+1}}</td>
                                        <td>{{val['birth_day']}}</td>
                                        <td>{{val['name']}}</td>
                                </tr>
                        </tbody>
                </table>
        </body>

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

How to efficiently handle callbacks with Angular Services for asynchronous HttpRequests?

I'm struggling with understanding how to correctly implement callback methods for ajax calls in Angular. In my Angular app, I want to display the testUser object only after the ajax call has successfully completed. Here is an example of my ng control ...

Dynamic change of table cell text color based on condition utilizing ng-if and ng-style

I am looking to create a table cell with alternating color combinations based on specific conditions. Here is the code snippet: <tr ng-repeat="x in data"> <td> <span ng-if="((x.time2 < x.time1) || (x.time2 < x.time3))" ng-style="colo ...

Avoiding 0 being mistakenly interpreted as undefined while utilizing React Conditional Rendering

I have a React component with the following code in its render function: return ( <div> {rating.score && ( <div>do something</div> )} </div> ); The prop rating.score is of type PropTypes.number. Eve ...

When a specific condition is met, Jquery can automatically execute a function contained

I am trying to slowly reveal the h1 tag using jQuery for demonstration purposes. The scroll function is working when I manually click to initiate it, but I am having trouble triggering it automatically. I feel like I might be missing something simple and ...

Executing the .click() function of jQuery within a loop

These specific elements are scattered throughout my HTML code <input id="1" type="button" value="1" > and then there's this one <input type="text" name="answer" id="answer"> In my JavaScript, I've defined a function sum =(eve)=&g ...

Struggling to achieve desired output from function in NextJS

I'm a bit confused by the code below. The itmLoop function seems to work fine when placed directly in the return section, but nothing is output when it's called as shown below? I'll eventually need to make it recursive, so I have to keep i ...

Modifying canvas border colors using AngularJS

Currently, I am in the process of learning AngularJS and have developed a website that includes a canvas element. My main objective is to change the border color after clicking on a checkbox. Here is the code snippet for canvas.html : <!DOCTYPE html&g ...

Enhanced jQuery Embed Code validation for user input using a textarea

Currently, I am developing a website that allows users to input embed codes from popular platforms such as Twitter, YouTube, Instagram, Facebook, and so on. The embed code undergoes validation checks and is saved if it meets the criteria. However, when us ...

Building a custom login page with axios in a react-redux application

I've encountered an issue with my login page. When I click the submit button, it does not redirect to the main Dashboard page. Check out the code below for authentication/login.js: import React, { Component } from 'react' import { Field, ...

Trouble arises when integrating jQuery with PHP

When using jQuery, the data passes fine but when trying to pass it to PHP, everything becomes NULL. I have tried alert boxes and it seems like the data and values are stored properly. However, when passing it to a PHP file using ajax and then using var_dum ...

JavaScript consistently returns HiddenField as NULL

After creating a Context menu and associating it with a Gridview, I noticed a problem when pressing a button from the context menu. I intended to retrieve a value from a HiddenField, but it always returns null. My assumption is that the DOM might not be fu ...

Guide to designing a unique shape in JointJs by combining multiple basic shapes together

Is there a way to combine different shapes in Joint JS, such as creating a custom shape that includes both a rectangle and a circle? I am aware of the path method, but I'm not sure if it is suitable for creating combined shapes like this. ...

Can a frontend framework be exclusively utilized on a single page within a completed project?

I have a project where I am looking to create a dashboard as the homepage, similar to this example: However, for this project, we are not using any frontend framework, only relying on JavaScript and jQuery. The backend for the project is implemented usi ...

Issue with moving files after successful upload - encountering ENOENT Error

I am encountering a frustrating issue while trying to transfer files from the temp directory to the profile_pictures directory. This seemingly straightforward task has proven to be quite challenging as I have been stuck on it for an hour! The process shou ...

I encountered a RangeError with code [ERR_HTTP_INVALID_STATUS_CODE] due to an invalid status code being undefined

I have encountered a specific error while running my note-taking app. The error seems to be related to the following piece of code - app.use((err,req,res,next)=>{ res.status(err.status).json({ error : { message : err.message ...

Utilizing the hint operator in strongloop loopback with mongoDB: A step-by-step guide

Seeking guidance on utilizing the hint operator from mongodb within Strongloop Loopback. While I have successfully implemented this operator directly in mongodb, navigating its use within Strongloop Loopback has proven challenging. Any advice on how to int ...

Verify the token to reset the password using Next.js

Please take a look at the image and the code provided below: const { push, query } = useRouter(); const [Datas, setDatas] = useState([]); const token = query.token; // axios .get(variables.API_URL + `Auth/reset-password/${token}`) .then ...

Setting up KaTeX integration (code injection) on Circle.so

In my quest to create an online hub for IB Math students, I've decided to utilize Circle.so as my platform of choice. The diverse range of features offered by Circle.so, such as code injection capabilities and sleek design, make it an appealing option ...

Using the "export default" feature in React.js is a

Is it possible to name an exported function as "default" without encountering an error? export default() => { const [width, setWidth] = useState(0); const [height, setHeight] = useState(0); useEffect(() => { setTimeout(() => { setWidth(window.i ...

Issues encountered while optimizing JSON file in a ReactJS program

I'm struggling with utilizing Array.prototype.map() in Javascript Specifically, I'm reformatting a JSON file within my react app, which looks like: { "id": 1, "title": "Manage data in Docker", "description": "How to use v ...