Enumeration within grouped set

Looking to organize an object in the following format (with numbered questions):

1. Header 1
 Subheader 1
  1.1 Question 1
  1.2 Question 2
 Subheader 2
  1.3 Question 1
  1.4 Question 2
2. Header 2
 Subheader 1
  2.1 Question 1
 Subheader 2
  2.2 Question 2

I have managed to group it like above using underscore-js. However, I am struggling to maintain continuous numbering for the questions within each header. Please check out the jsfiddle link provided below for reference.

Attempted Solution: I can determine the number of questions but face difficulty in maintaining a continuous count due to $index resetting for every loop iteration.

HTML

<div ng-controller="MyCtrl">
  <div ng-repeat="y in grid" ng-init="headerIndex = $index ;">{{headerIndex + 1 }}. &nbsp;{{headers[headerIndex]}}
  <div ng-repeat="x in y" ng-init="subHeaderIndex = $index;">&nbsp;&nbsp;&nbsp;&nbsp;{{x[0].sub_header_text}}
{{x.length}}
          <div ng-repeat="z in x " ng-init="questionIndex = $index">          
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;          
        {{headerIndex + 1 }}.{{( (subHeaderIndex) * (x.length)) + (questionIndex + 1)}}
        {{z.question_text}}
  </div>
  </div>
  </div>
</div>

JS:

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {

    $scope.grid = {"Header in English template of German Country":{"SubHeader in English template in German Country":[{"question_text":"sadsad","header_text":"Header in English template of German Country","sub_header_text":"SubHeader in English template in German Country","question_version":1,"question_id":151113,"position_id":101,"template_id":194941,"question_typ":"Text","mandatory_field":"false","status":"new","minimum_value":"null","maximum_value":"null"},{"question_text...
                   ...Maximum_Value":"null"}]}};
    
    $scope.headers = Object.keys($scope.grid);
}

For more details and code implementation, please visit the JSFiddle - http://jsfiddle.net/mdaamir1989/Lvc0u55v/10517/

Answer №1

To ensure continuous numbering of questions, keep a counter in the controller and increment it with each ng-repeat iteration. Reset the counter at subheader level.

Modify your HTML accordingly:

<div ng-repeat="x in y" ng-init="subHeaderIndex = $index;number = resetCounter()">
       &nbsp;&nbsp;&nbsp;&nbsp;{{x[0].sub_header_text}}
          {{x.length}}
   <div ng-repeat="z in x" ng-init="questionIndex = $index; number = incrementCounter()">        
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;          
            {{headerIndex + 1 }}.{{( (subHeaderIndex) * (x.length)) + (questionIndex + 1)}}
            {{z.question_text}} {{number}}  
   </div>
</div>

In the Controller:

$scope.counter = 1;
$scope.resetCounter = function(){
  return $scope.counter = 1;
}
$scope.incrementCounter = function() {
   return $scope.counter++;
}

The use of {{number}} in HTML will maintain consistent question numbering.

Check out the Updated Fiddle: http://jsfiddle.net/Lvc0u55v/10556/

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

Issue with Vuex. While this.$store.dispatch(...) is functional, ...mapAction is not working as expected

I am encountering an issue with dispatching Actions from vuex. It's puzzling to me that ...mapActions is not initiating a request to Jsonplaceholder. However, using this.$store.dispatch successfully retrieves all 10 users without any issues. Below are ...

Why isn't my CSS transition animation working? Any suggestions on how to troubleshoot and resolve

I am looking to incorporate a transition animation when the image changes, but it seems that the transition is not working as intended. Can anyone provide guidance on how to make the transition style work correctly in this scenario? (Ideally, I would like ...

Despite receiving a 200OK response, the API in GetStaticProps() is not providing any data

I'm trying to send an axios.get request to an API within getStaticProps() in next.js. Although the response is coming back with a 200OK status, I can't seem to locate the data in the response. Where should I be looking for this data? The data ...

Tips for keeping a div element at the top of the page

I am looking to have a div stick to the top of the page when someone scrolls down When the page is scrolled, the green div with class stickdiv should automatically stick to the top var left = document.getElementsByClassName("stickdiv"); for( var i = 0; ...

Where can I locate the list of events supported by CKEditor 4?

Looking for the list of available events I attempted to locate the event list in the official documentation, but unfortunately came up short. I resorted to searching through the source code using .fire("/s+") to identify all available events. However, thi ...

Troubleshooting a ThreeJS Issue: the Mystery of the Malfunction

I have a ribbon showcasing various thumbnails. These thumbnails are painted on a canvas and then added to a Texture. var texture = new THREE.Texture(textureCanvas); The mesh is generated as shown below loader.load('mesh_blender.js', functi ...

Troubleshooting: Why is Jquery unable to retrieve image height?

Having trouble using jQuery to find the actual height of the first image nested within a container. I can access the src attribute but not the height. Any suggestions on how to get the accurate height? Is it necessary to retrieve heights via CSS? Unfortu ...

Decoding JSON in AngularJS

Looking for assistance in parsing a JSON 2D array with Angular JS and fetching images from the array. Can anyone provide some guidance on this? I have updated my Plunker at this link HTML Code <!DOCTYPE html> <html lang="en" ng-app="myAp ...

How to pull out a particular element from an array within Google Sheets

Currently, I am running a function in Google Sheets that generates an array output like the one shown below Col1 Col2 A B 10 20 I am interested in retrieving only the value 20 and do not want any of the other rows or columns to be displaye ...

Ways to conceal the header and footer on specific React pages

I currently have my header and footer wrapping the content on all pages of my website. However, I am looking to hide the header and footer on specific pages like the customer and admin dashboard. Can anyone suggest the best approach to achieve this? cons ...

Utilize Meteor and Mongo to access a nested array object in a template with spacebars

I am trying to populate the content of a textarea by extracting data from a nested array. In my helper function, I have specified the document id and the element id. The goal is to extract the content of the text field from the findOne result and display i ...

Is there a way to incorporate content onto a webpage solely through the use of CSS and JavaScript?

Currently, I am in the process of developing a real estate website that utilizes a paid service for displaying real estate listings. The unique aspect of this setup is that my website operates as a subdomain of the main listings website, resulting in a URL ...

Exploring the depths of time travel in Redux without the aid of developer

Has anyone successfully achieved time traveling capabilities with Redux core? It seems that this feature is limited to the devtools and not advised for production use. I had planned on implementing Redux in a multiplayer game to assist with managing clie ...

translating file paths into JSON layout using Python

I have a list of file paths stored in a txt file that I need to convert into JSON format. For example: /src/test/org/apache/hadoop/ipc/TestRPC.java /src/test/org/apache/hadoop/ipc/TestRPC2.java I attempted the following code: for item in input: hier ...

"Exploring the differences between a singular object holding an array of objects versus a straightforward array

In my Nodejs project, I have a complex object that contains an array of objects (obtained from an API request) and a simple array of values. Here is how they are structured : // Object > Array > Objects (result of an API request) : { "data& ...

Struggling to identify every occurrence of a specific character (provided by the user) within a character array (also provided by the user) in the C

Being a first-year student, I am still grappling with the intricacies of the C language. One of my assignments requires me to create a program where a user inputs a word and a letter. The program then finds all the positions of that letter in the word. Fo ...

Trigger the "onChange" event when the input element is modified by JavaScript within a popup window

On my webpage, I have a form element and a popup window (which is opened using window.open). Both the form element and the popup window utilize jQuery. The JavaScript in the popup window has the ability to alter the form element on the main page successf ...

Troubleshooting a problem with AngularJS $http

Just diving into the world of AngularJS and running into a minor issue. Fetching JSON data from an API, I use ng-repeat to display it all. But, when I click on an Id link for a particular object, I want to show only that object's specific data on an ...

Why is the radio button not chosen in the ns-popover popup? The radio button is only selected in the popup of the last column

In my Angular controller, I am trying to set the radio model but it is only appearing in the last column popup of the table. The ns-popover is displayed when clicking on a table column. Here is the Angular Code: var app = angular.module('app', ...

What changes can I make to the script in order to display every element from a MySQL query using PHP, MySQL, JavaScript,

Enhanced Multiple Dropdown Selection Using Ajax In the following scenario, we have a webpage featuring a multiple dropdown selection that interacts with a MySQL database. By choosing options in the dropdowns labeled site, menu, and category (categ), a que ...