Ways to create different sections for ng-repeat data in AngularJS

I have a list of data elements that I am displaying using ng-repeat which currently prints as follows:

data1
data2
data3
data4
data5
data6

However, I would like to display these data elements in sections, like this:

data1    data3    data5
data2    data4    data6

Is it achievable in AngularJS?

Answer №1

This feature is a CSS trick that eliminates the need for any Angular code. Simply place your ng-repeat directive within <li> elements nested inside a <ul> element, and apply a style to the <ul> tag with columns: 3. This will fulfill your requirement effortlessly.

Illustration

<ul style="columns:3">
    <li ng-repeat="item in items">{{item}}</li>
</ul>

Answer №2

There are various methods to accomplish this goal.

  • Implementing a 3-column structure is one approach, where the content wraps to the next line after every third value.
  • Alternatively, you can incorporate this functionality directly in your code by utilizing an ng-if directive on the "br" tag when $index%3 == 2.

Answer №3

A possible way to structure data like that is by formatting the array into a 2D array for easier looping. You can refer to the Custom Example for reference.

To achieve this, you can create a filter to format your array data:

// Custom filter to loop through every n items
App.filter("customLoop", [function() {
  return function(mainArray, loopEvery) {
    var subArray = [], formattedArray = [];
    angular.forEach(mainArray, function(item, index) {
      subArray.push(item);
      if ((subArray.length == loopEvery) || (index == mainArray.length - 1)) {
        formattedArray.push(subArray);
        subArray = [];
      }
    });
    return formattedArray;
  }
}]);

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

MongooseError: The operation `users.findOne()` has encountered an issue

While working on my movie website, I encountered an issue when setting up the login feature. When trying to register using POST through Insomnia, I received an error message stating "MongooseError: Operation users.findOne() buffering timed out after 10000m ...

The ng-table grouping feature fails to function properly when identical data is retrieved from a REST service and assigned

I am encountering a perplexing problem. The ng-table groups function as expected when the input data is hardcoded, using the same data structure as the data retrieved from a JSON response via a REST service. The hardcoded JSON below works: var data = [{ ...

configuring conditions for protractor end-to-end tests

We have incorporated Protractor for testing our front-end Angular application currently under development. While using browser.get() to define the environment we intend to test (localhost:9000, staging, UAT), I am exploring options to parametrize this so ...

Is the JSON data not matching the file's content during validation?

After thorough testing, my JSON data appears to be functioning correctly with regular content. Here is a sample of the working JSON: Working Json { "language": "XYZ", "content": { "GEN": "this is test& ...

The best way to avoid routing before certain async data in the Vuex store has finished loading

I am facing a challenge in my application where I require certain data to be loaded into the VueX store before routing can commence, such as user sessions. An example scenario that showcases a race condition is as follows: // Defined routes { name: &ap ...

Creating a scrollable div in Electron

I'm currently developing a basic Markdown application using Electron. At the moment, all I have is a textarea, a div, and some Javascript code that retrieves the text from the textarea, processes it with Marked (an NPM module), and updates the content ...

Angular: The most efficient method for creating a customized HTML page with unique styles specifically for PDF generation

Struggling with PDF generation and the challenge of preparing hidden HTML is my current hurdle. The backend team has built a PDF generation service that requires passing an HTML string as a parameter. My task is to hide all HTML elements upon clicking the ...

JavaScript TypeError - missing method issue

When I try to call this code, I am encountering a TypeError: player = new Player({name:''}); Player = MeteorModel.extend({ schema:{ name:{type:String,}, value:{} }, belongsTo:{ meteorCollection:'', methodName ...

Utilizing Sequelize to Convert and Cast Data in iLike Queries

When using Sequelize for a full-text search, I encountered an issue with applying the iLike operator to columns of INTEGER or DATE type. How can I cast off a column in this case? To illustrate, here is an example of what I am trying to achieve with a Post ...

Tips for showing a DialogBox when a blur event occurs and avoiding the re-firing of onBlur when using the DialogBox

Using React and Material UI: In the code snippet provided below, there is a table with TextFields in one of its columns. When a TextField triggers an onBlur/focusOut event, it calls the validateItem() method that sends a server request to validate the ite ...

Is there a way to modify the starting position of my shapes in Three.js?

Is there a way to adjust the starting position of my geometry? Currently, it appears in the center of the canvas, but I would prefer it to be at the top left corner. Any suggestions on how to achieve this? scene = new THREE.Scene(); camera = new THRE ...

Filter input in jQuery for a textarea box

I implemented a modification of this technique in my code. The main purpose is to prevent unauthorized characters from being entered by the user (there is also a filter on the server side). $('#someinput').keyup(function() { var $th = $(this ...

Function for swapping out the alert message

I am searching for a way to create my own custom alert without interfering with the rendering or state of components that are currently using the default window.alert(). Currently working with React 15.x. function injectDialogComponent(message: string){ ...

Encountering issues with fs.writeFile function in a freshly set up Vue project

After initializing a new Vue project with vue cli, I encountered an error when attempting to write files in the main.js file. Below is the code snippet that caused the issue: const fs = require('fs'); // Data to be written to the file. let dat ...

Display a div when hovering over another div

I have a menu that looks like this: https://i.sstatic.net/WqN33.png and I want to create a hover effect where another div shows up over each item, similar to this: https://i.sstatic.net/JRaoF.png However, I can't seem to figure out how to implemen ...

Utilize Jquery to transfer a JSON API Request element to an array

Currently, I am immersed in a project where I am learning how to utilize APIs and extract data from them. However, I have encountered an issue with getting a specific element from the JSONP result that I am receiving. My goal is to extract only the address ...

Is it possible to convert an object with properties of equal length into a list of objects using JavaScript?

I am working with an object that has multiple keys, each containing a list of equal length: myobj = { 'key1' : [1, 2, 3], 'key2' : ['a', 'b', 'c'], 'key3' : [true, false, true], .. ...

Develop a professional Angular application for deployment

Help! I'm struggling to build my Angular application for production. After running the 'ng build --prod' command, I can't find all my components in the 'dist' folder. Do I need to change or configure something else? I see som ...

Arrangement of HTML file script and CSS styling

After watching Doug Crockford's Theory of DOM video where he discusses the positioning of <script> tags and CSS <link> at 16:50, I began to ponder a few things. He recommends placing <script src> closer to the bottom of the body and ...

Inserting items into a JSON array

Is there a way to add a new ID to my data array without creating a new object for each addition? Existing Data: [{"devices":{"dID":"TLSM01"},"uuid":"e863c776-f939-4761-bbce-bf0501b42ef7"}, {"devices":{"dID":"TLSM01"},"uuid":"5a0cd70d-891d-48d8-b205-e92 ...