Creating a horizontal grid with a set number of rows using ng-repeat

I'm facing a challenge where I need to showcase a set of text objects stored in an array within a grid format. I am utilizing the Ionic framework for this purpose.

Here are the specifications for the grid:

  1. It should have a fixed height
  2. Consist of exactly 3 rows
  3. Enable horizontal scrolling (infinite columns)
  4. Show 2 columns on screens narrower than 768px (phones) and 4 columns on screens 768px and wider (tablets)

I have already defined the media query for column widths and included for implementing horizontal scroll functionality.

The main question is how can I utilize ng-repeat (possibly with ngif ??) to construct the grid, starting from the first column and filling it with 3 rows before proceeding to the next column, and so on?

JS

var items = [
{text: "This is item1"},
{text: "This is item2"},
{text: "This is item3"},
.
.
.
]

CSS:

.myGridRow {
  height: 40%;
 }
 .myGridCol {
   width: 50%;
 }
 @media only screen and (min-width : 768px) {
   .myGridCol {
     width: 25%;
   }
 }

HTML

<ion-scroll direction="x">
 <div class="row myGridRow"> <!-- The single container for the whole grid -->

  <div ng-repeat="item in items"> <!-- Repeat this process 3 times before moving to the next row -->
   <div class="row"><div class="col myGridCol">{{item.text}}</div></div>
  </div>

</div>
</ion-scroll>

Desired Output

I'm currently stuck at determining the approach to switching to the next column after every 3 rows or confirming if this method aligns with the intended outcome.

Answer №1

ng-repeat is limited to looping through a collection and cannot handle an integer as input. However, you can create a placeholder collection to determine the number of repetitions needed:

$scope.items = [/* The base collection */];
$scope.iterCount = 3; // Number of iterations before a new row is created
$scope.itemCount = Math.floor($scope.items.length / $scope.iterCount);

// Fetch a collection of items equally spaced from one another. For instance: getSpacedItems([1, 2, 3, 4], 0, 2) == [1, 3]
$scope.getSpacedItems = function(collection, start, space) {
  var subColl = [];
  for(var i = start; i < collection.length; i += space) {
    result.push(collection[i]);
  }

  return result;
};    

You can then apply these values in the view like this:

<div>
  <div class="row" ng-repeat="n in [0, 1, 2] track by $index">
    <div class="col myGridCol" ng-repeat="item in getSpacedItems(items, $index, iterCount)">{{item.text}}</div>
  </div>
</div>

Answer №2

In order to tackle this issue effectively, I recommend transforming the array data on the controller side. By "rotating" your 2D array and switching rows with columns, you can then proceed with standard output procedures. This process is better suited for the controller rather than the view side.

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

Identifying IPV6 Private and Loopback Addresses in Node.js: A Guide

Right now, I am looking for a way to distinguish between private addresses and web reachable ones. I am seeking a method to test for the following: Given a list of ipv6 addresses on an interface, I need to identify which ones are web reachable. It's ...

What could be the reason for Angular showing the raw HTML code instead of

I'm currently diving into Angular and encountering an issue where my HTML isn't properly displaying the values I've set. It appears to only show the raw HTML. Here's a snippet from my HTML page: <p>to-do-items works!</p> < ...

The issue lies with Mongoose's inability to convert a string into an object key

When making an ajax call, I pass the object below to Mongoose: {category: 'name', direction: 1} To sort the query results using Mongoose, I use the following code: Site.find(query).sort(sortBy) Prior to this call, I need to format the object ...

Web pages that dynamically update without the need for reloading

Recently, I stumbled upon slack.com and was immediately captivated by its user interface. For those unfamiliar with it, navigating the site is quite simple: There's a sidebar on the left and a main content area on the right. When you click on an item ...

Troubleshooting: MongoDB/mongoose post save hook does not execute

My current setup involves the following model/schema: const InvitationSchema = new Schema({ inviter: {type: mongoose.Schema.Types.ObjectId, ref: 'Account', required: true}, organisation: {type: mongoose.Schema.Types.ObjectId, ref: 'Orga ...

Steps for assigning a $scope variable from within a different $scope variable

I am facing an issue with setting the value of a $scope variable after a promise (specifically, an $http request) is completed. Here's the code snippet in question: .controller('NewItemCtrl', function ($scope, $state, LabelFactory) { $sc ...

What is the process for configuring CORS in Wamp?

Currently, I have set up Wamp as my local server to test my Angular application. In my development process, I am utilizing $resource to fetch data from an API on my server. However, I keep encountering the following error message: XMLHttpRequest cann ...

.env file cannot be utilized in JavaScript

Currently, I am working on a project where both the front-end and server are located in one directory. I am using a .env file in the root directory, and the structure of the project looks like this: project frontend (directory) server (directory) .env (fi ...

I have a vision of creating a unique Countdown Stopwatch that meets all my

I'm looking to create a custom countdown stopwatch where I can set the time and watch it count down to 0:00. The stopwatch should have three buttons: Start, Stop, and Reset. I've searched multiple websites for what I need but haven't found ...

Is it possible to form facial features using just the vertices?

My current project involves creating a Minecraft-style terrain made up of cubes. To optimize performance, I am looking for a way to merge these cubes server-side before sending the data array to the client. The goal is to reduce the strain on both the clie ...

Retrieving exclusive npm packages from an npmjs user

I am currently facing a challenge with transferring all of our npm modules from npmjs.com to a new location. The issue is that our modules are saved under a private npm user account, making it difficult for me to programmatically access and consume all the ...

Turn on and off jquery tabs depending on user's choice

Currently, I am utilizing jQuery tabs to showcase specific data. However, I aim to offer users the flexibility to choose whether they prefer to view the content as tabs or in a sequential manner. Even after attempting to manipulate the ui-tabs classes ba ...

Click on any checkbox to select all checkboxes at once

I have created a table with each column containing a checkbox. My goal is to select all checkboxes in the table when I click on the checkbox in the top row (th). Can someone guide me on how to achieve this? Below is my code: <table style="width:100%"& ...

The semantic UI form validation I'm implementing uses a regex rule of 'regExp[/(.*[0-9 \-]){23}$/]'. Works perfectly on all devices except for iOS

Here is the code snippet provided that accepts a card number in the format 5077-1200-5007-3284-951. The card number works on all devices/browsers except for iOS. Additionally, I would like to know if there is an alternative regex pattern I can use for th ...

Exploring differences: map versus concatMap

I'm struggling to grasp the distinction between concat map and map. In a particular scenario, this difference became very confusing for me: const domainsObservable = this.auth.getAuthDomains() .shareReplay() .concatMap((authDomains) => aut ...

What is the best way to display the output of my Sudoku generator as an HTML table in Django?

Recently delving into Django, I've been working on creating a Sudoku web application. To generate the Sudoku puzzles, I developed a Python script named "Sudoku Generator.py" which produces matrices like this: [[3, 8, 2, 7, 5, 6, 1, 4, 9],[1, 4, 5, 2, ...

After showcasing every photo in the album, remember to include a line break

I am currently using the Flickr API to fetch images and display them on my website. The issue I am facing is that after displaying all the photos of each album, there needs to be a line break so that the name of the next album is displayed on the next line ...

What is the best approach to ensure mobile responsiveness and properly space the TV show div?

How can I center the searched images of shows and add spacing below each image? The 12-column grid is not behaving properly and the images are aligned correctly on desktop but not on mobile. Any suggestions on how to fix this using CSS and Bootstrap? The g ...

Blank page shown when routing with Angular in Rails

Hey there, I'm currently attempting to integrate Angular into my Rails application and unfortunately I'm encountering a problem where the HTML page shows up blank. Here's the code I have so far: app/views/index.html.erb <body> ...

Guide on adjusting the darkness of a MaterialUI Avatar component image and adding text to it

I'm currently working with the Avatar component from Material UI along with Tailwind CSS. I found that by simply adding the image URL to the src, it displays the avatar successfully. <Avatar alt="Cindy Baker" src="https://mui.com/sta ...