Tips for arranging alternating elements in a column layout using an array data model

In the $scope there is a dynamic array and in the HTML template, there are three columns. The goal is to render elements from the array in the HTML like this:

<div class="first-column">
  <div><!--element of (index + 3) % 3 === 0 will be placed here--></div>
</div>

<div class="second-column">
  <div><!--element of (index + 3) % 3 === 1 will be placed here--></div>
</div>

<div class="third-column">
  <div><!--element of (index + 3) % 3 === 2 will be placed here--></div>
</div>

It's important to note that the three columns should only appear once. Each element needs to be placed in a specific column based on its index matching the given expression.

While it is possible to use three separate ng-repeat declarations to achieve this, for better performance, I am seeking a solution that involves just a single ng-repeat.

If you have any suggestions or ideas, please feel free to share them. Thank you!

Answer №1

Utilize nested ng-repeat directives to condition each item within the columns; following a model like this:

columns: [1,2,3],
items: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']

Your visualization will appear as shown below:

<div class="col-xs-4" ng-repeat="c in columns">
    <h1>Column {{ (outerIndex = $index) && c || c }}</h1>
    <div class="item" ng-repeat="i in items" 
                      ng-if="$index % columns.length === outerIndex">
        <!-- whatever -->
    </div>
</div>

Notice the introduction of a new variable (outerIndex) into the outer loop scope, which is used alongside the inner loop's $index for the alternating conditional.

For a visual demonstration, click on the demo button below (bootstrap-css is referenced only for illustrating column layout).


TL;DR, Show me the demo!


     Demo Button      
 

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

Move to the following array when clicking PHP (using Ajax, perhaps?)

How can I trigger php code when a button is clicked? (I understand the distinction between client-side and server-side. However, I believe I have encountered a similar scenario before.) Consider the following array : $test_id = [ 1 => '15 ...

The data retrieval process with angularjs is not functioning properly

While working on my web application project, I encountered an issue with integrating angularjs. I have a table named city which was created using entityframework and database first. To address this, I included the angularjs nuget package in my project. Ad ...

Monitor the console log in the Android Browser for any potential issues

My website runs smoothly on all browsers, except for the Android browser when using JavaScript. I am aware that I can activate JavaScript debugging with about:debug, but I am unsure of where to find the console log/errors. How can I locate and check my b ...

Working with nested arrays in Mongoose/Javascript: Adding an Object to an Array within another Array

I have been attempting to add an object to an array that is nested inside another Array in Mongoose. Essentially, it's like having comments for the comments. Below is the structure of my schema: const Schema = new mongoose.Schema ({ name: {type: Str ...

Issue: Unhandled rejection TypeError: Unable to access properties of an undefined variable (retrieving 'data')

Currently, I am developing applications using a combination of spring boot for the backend and react for the frontend. My goal is to create a form on the client side that can be submitted to save data in the database. After filling out the form and attemp ...

Troubleshooting a problem with selecting options in Jquery

Looking for assistance with a jquery script. I have a select dropdown that fetches a list of options via Ajax. When a user clicks on an option, if a certain variable equals 1, an HTML div is added. If the variable changes to another value, the HTML div dis ...

Modal's ng-click not triggering component's function

I am currently working on resolving an issue. My choice to use AngularJS is intentional, as I prefer not to load the entire NPM of Angular. Additionally, we heavily rely on Razor Syntax for the Web Layer. Implementation in Create.cshtml file <button ...

A guide on extracting information from Cloud Firestore and converting it into a format that can be easily utilized on a local level using Cordova and Angular

In my Ionic/Cordova App, I am utilizing Cloud Firestore as the database to store and retrieve data. One challenge I am facing is extracting the stored information from Firestore and incorporating it into my calculation functions. The structure of my Fire ...

Looking to retrieve selections when the inputValue changes in react-select?

I'm working with a react-select component and I would like to implement a feature where an API request is triggered as soon as the user starts typing in the react-select field. This request should fetch items related to the keyword entered by the user ...

What is the best way to create an `isHook` function in my code?

Is there a way to determine if a function passed is a React Hook? isHook(useState); // returns true isHook(() => {}); // returns false; I am looking for a method to distinguish whether a function attached to an object property is a hook or not. In cas ...

In AngularJs, use ng repeat and ng switch to dynamically generate and display tables

I need help rendering a table with two columns using angularjs directives. <table> <tr ng-repeat="message in messages" > <td ng-switch-on="message.network" ng-switch when="twitter" ng-controller="TweetController"> <span> ...

Stopping video playback when hovering over it

Can anyone help me modify this code so that a video plays only when hovering over it, and stops playing once the hover ends? What changes should I make to the JavaScript portion of the code for this functionality? <div class="padding-box height-40"&g ...

Activate onbeforeunload when the form is not submitted

Currently, I have a form that is submitted using PHP with three different submit actions: Save and Continue Save and Exit Exit without Saving The goal is to trigger an "OnBeforeUnload" alert if the user does not click on any of the form actions. This al ...

Using Vue.js to Authenticate Users with JWT Tokens Sent in Registration Emails

Hey everyone, I could really use some assistance with JWT tokens. I created a registration form using Vue.js and sent the data to the database using axios. Now, I need help figuring out how to save the token I receive in the confirmation email so that I ca ...

Utilizing Nginx to Reverse Proxy to Node.js and Implement Rewrites

I have various applications running in the background behind an Nginx reverse proxy. One of these applications is a Node server using Express.js. I am forwarding domain.com/demo/app/<path> to localhost:7003/<path> through this Nginx configurati ...

Transmit information to the controller using jQuery in a C# MVC environment

Here is my jQuery script code snippet. The script works perfectly and stores the data array/object in a variable called dataBLL. var dataBLL = []; $('#mytable tr').each(function (i) { dataBLL.push({ id: $(this).find('td:eq(0)').text( ...

jquery is showing up in the browserify bundle.js file, however, it is not functioning properly

Currently, I am trying to follow a brief tutorial on how to use Browserify. Despite following the instructions precisely, jQuery seems to not be working properly when bundled. Specifically, the button element in my app.js code is not appended to the body. ...

Callback Method Ajaxify

Just started using Ajaxify and so far it's been a great experience. However, I'm wondering if there is a callback function available that doesn't require any specific inputs in the script. I've searched around but haven't found wh ...

Ways to define a variable based on a CSS class attribute?

I need help figuring out how to create a variable in my script that is defined by the width attribute of a CSS class. The snippet I have currently doesn't seem to work as expected. var dist = $('.nav').css(width()); The goal of my script i ...

Having trouble with Jquery Ajax call in IE8?

I have a dynamic data loading from the database, with each row containing links that perform various actions. Most of them work perfectly fine, but I've noticed an issue with the last one I added – it doesn't seem to be functioning properly on ...