Counting on AngularJS that is nested within ng-repeat

I need help figuring out how to count the number of 'td' elements generated in a loop.

Using $index isn't working for me because it resets on each row, causing confusion with setting 'i' for each iteration. What is the best and simplest way to ensure that the first column value is 1 and the first column count starts at 0?

This is what my code looks like currently:

        <table class="calendar">
            <thead>
                <tr>
                    <th>M</th>
                    <th>T</th>
                    <th>W</th>
                    <th>T</th>
                    <th>F</th>
                    <th>S</th>
                    <th>S</th>
                </tr>
            </thead>
            <tbody ng-click="bindCellValue($event)">
                <tr ng-repeat="week in (days.length/7 | array)">
                    <td ng-repeat="day in days.slice(7*$index, 7*$index + 7) track by $index">
                        {{ day }}
                        <i class="icon ion-checkmark answer-correct" ng-if="submitted && answers[i].correct"></i>
                        <i class="icon ion-close answer-wrong" ng-if="submitted && !answers[i].correct"></i>
                    </td>
                </tr>

            </tbody>
        </table>

In my controller, I have defined the following:

$scope.days = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, null, null, null, null ];

Answer №1

One possible solution is to utilize the ng-init Angular directive.

<tr ng-repeat="week in (days.length/7 | array)" ng-init="w = $index">
    <td ng-repeat="day in days.slice(7*$index, 7*$index + 7) track by $index" ng-init="i = w*7 + $index">
        {{ day }}
        <i class="icon ion-checkmark answer-correct" ng-if="submitted && answers[i].correct"></i>
        <i class="icon ion-close answer-wrong" ng-if="submitted && !answers[i].correct"></i>
    </td>
 </tr>

Answer №2

It could look something similar to this:

<i class="icon ion-checkmark answer-correct" ng-if="submitted && answers[$parent.$parent.$index * 7 + $parent.$index].correct"></i>

In essence, you should take the $parent's index (the one where the week is created), multiply it by 7, and then add the other $index in. I recommend generating that output until you're confident it's accurate.

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

Retain values of JavaScript variables acquired by their IDs even after a page refresh

I have a JavaScript function that retrieves HTML input values and inserts them into a table. However, when I refresh the page, those values are reset to null. How can I prevent this and keep the values visible after a refresh? I believe setting and getting ...

Encountering error ORA-12514 when utilizing the node oracle-db npm package

At the moment, I am immersed in a project that relies on utilizing oracle for its backend. Following the instructions provided in this link, I successfully installed node-oracledb on my Mac using npm. The contents of my file are as follows: var oracledb = ...

How can I identify duplicate values within two distinct javascript objects?

Consider two sets of JavaScript arrays containing objects: var allUsers=[{name:"John",age:25},{name:"Emily", age:30},{name:"Michael",age:22}] and var activeUsers=[{name:"John",status:"active"},{name:"Sarah", status:"active"}] I am attempting to iterat ...

Using the oidc-client library in Angular to create a customized isLoggedIn() function within *ngIf implementation

Objective: I'm trying to toggle the visibility of a Login and Logout button in an Angular application using IdentityServer4 and OIDC client. Issue: The isLoggedIn function in my code returns undefined before it returns true, causing the *ngIf directi ...

The div's width shifts upon reaching the top of the page

Creating a blog site example and trying to lock the position of a div once it reaches the top of the page. Utilizing materialize.css for this task. <div class="row"> <div class="col m8 s12"> <!-- content goes here --> < ...

`How can I fetch external JSON data using an AJAX request in an HTML document?`

Testing the functionality of this HTML page by creating a dummy JSON file, but encountering an issue with loading the data via AJAX request. The specific error message received is: Uncaught ReferenceError: data is not defined. Struggling to call and in ...

An issue with the THREE.js TextureLoader not functioning properly when deployed

After implementing the code below, my project is successfully rendering my background and avatar. However, when deploying on Netlify, the rendering does not function as expected. I have attempted using "../../", "./", and other variations, but the issue p ...

Rotation snapping feature 'control.setRotationSnap' in TransformControls.js (Three.js) is not functioning properly

Attempting to utilize the functionality of "control.setRotationSnap" from the script "TransformControls.js", but unfortunately, it is not working as expected. After conducting some research, I came across a forum post suggesting that the code might not be ...

Nodejs registration and user verification process for accessing account features

I am facing a decision on how to handle two types of users - vendors and buyers. Should I develop separate APIs for registering and authenticating each user type, or should I create a single API to manage both? When designing my database model, should I h ...

Highlighted option selection in Material UI dropdown using Cypress

Can someone explain how to select Material-UI dropdown options using Cypress? I'm looking for a simple explanation, thanks! ...

What is the best way to create a general getter function in Typescript that supports multiple variations?

My goal is to create a method that acts as a getter, with the option of taking a parameter. This getter should allow access to an object of type T, and return either the entire object or a specific property of that object. The issue I am facing is definin ...

Infinite loop triggered by jQuery dropdown menu on page resize was causing an issue

I have been working on developing a navigation menu for a website that displays as a horizontal bar on larger screens, but transforms into a jQuery dropdown menu when the window width is less than 980px. During initial page load with a window width below ...

When enclosing my Javascript code in CDATA tags within my SVG files, it does not execute

Having an SVG file in this format: <svg id="test" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" viewBox="0 0 1435 1084"> &l ...

ReactJS - Common Mistakes in Writing Code

I'm a beginner in Javascript and struggling to resolve this issue. When I declare my 'params' variable with var, I encounter an error stating "[js] Unexpected token. A constructor, method, accessor, or property was expected." You can take a ...

Incorporating and modifying a component's aesthetics using styled-components: A comprehensive guide

My OverviewItem component has 2 props and is a styled-component. I want to change just one style on this component, which can be done by using the technique of styling any component. Does creating a wrapper component remain the only option for sharing st ...

Tips for saving an array from the server into a script variable

I'm currently working with the express js framework along with Handlebarsjs as the templating engine. My aim is to pass an array from the router to the view, and then store this array within a script tag. //users is an array res.render('chatRoom ...

Connecting an HTML box to a JavaScript function for the desired outcome: How to do it?

My goal is to connect the input box with id="b" (located inside the div with id="but1") with a JavaScript function that calculates values within an array. Although my junior logic review didn't detect any issues in the code, what mistakes could I have ...

Discover a technique to display every individual "echo" statement from PHP in sequence, rather than waiting for the entire script to finish executing

I have developed a PHP script that takes some time to execute and displays multiple "echo" statements as the progress is being made. The script connects to an FTP server, deletes all contents, and then uploads new files. Everything is functioning correctly ...

Dividing blocks of text into individual sentences

Seeking a solution to splitting a paragraph into individual sentences, I initially attempted the following code: var sentences = paragraph.split('.'); While this method was effective in most cases, it encountered issues with sentences like: ...

How to retrieve data from a JavaScript array in a separate JavaScript file

In my checklistRequest.js file, I populate an array and then try to access it in my Termine_1s.html file, which includes JavaScript code. Although I can access the array, when I attempt to iterate through it, only single digits are returned instead of stri ...