Using ng-repeat to create a grid in your Angular application

Is it possible to display an array in two columns per row using Angular directives, or will logic need to be written in the controller?

Controller:

// Unsure if additional logic is necessary here

Model:

$scope.fruits = ['apple', 'banana', 'grapes', 'lemon']

View:

<div class="row" ng-repeat="fruit in fruits">
  <div class="col">{{fruits}}</div>
</div>

I am utilizing the Ionic framework for this implementation

Answer №1

Based on the given code snippet, it seems like Bootstrap is being utilized. To display 2 columns in a row, you can set each column to have a width of 50% by using the class col-md-6:

<div class="row" ng-repeat="item in items">
  <div class="col-md-6">{{item}}</div>
</div>

Answer №2

If you want to organize two elements in a single row, you can make use of bootstrap's col classes. By dividing the row into 50% and 50%, each element will occupy half of the total width calculated as 12 columns.

Example Code:

<div class="row" ng-repeat="item in items">
  <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">{{item}}</div>
</div>

Answer №3

If my understanding is correct, you are looking to display data in a table view where each column contains different data. Simply create the data in JSON format as needed.

 $scope.stock=[{name:"shocks",qty:"4"},{name:"shoes",qty:"4"}]

By using the above scope, you can bind it to two columns with different properties using simple JavaScript table properties.

<div ng-repeat="node in stock">`{{node.name}} 
{{node.qty}}`

Answer №4

There is a way to accomplish the desired outcome, although it may not be recommended by everyone.

<table class='table table-bordered'>
    <tbody ng-repeat="(k,v) in fruits">
        <tr ng-if='$even'>
            <td>{{fruits[k]}}</td>
            <td>{{fruits[k+1]}}</td>
        </tr>
    </tbody>    
</table>

http://jsbin.com/hukumi/edit?html,js,output

Answer №5

If you want to tackle this issue, consider generating a temporary JSON array through some adjustments in the original array.

function MyCtrl($scope) {
   $scope.fruits = ['kiwi', 'orange', 'pear', 'strawberry'];
   $scope.temp = [];
   while ($scope.fruits.length) {
            $scope.temp.push($scope.fruits.splice(0, 2))
   }
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js"></script>
<div   ng-app ng-controller="MyCtrl">
   <table class='table table-bordered'>
        <tbody ng-repeat="fruit in temp">
            <tr >
                <td>{{fruit[0]}} </td>
                <td>{{fruit[1]}}</td>
            </tr>
        </tbody>
    </table>
</div>

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

Struggling to navigate through a PHP multi-dimensional array for information retrieval

I'm struggling to extract and display the most recent 3 news articles from an RSS feed. My current code is only showing the first article repeated 3 times... <?php $doc = new DOMDocument(); $doc->load('http://info.arkmediainc.com/CMS/UI/Mo ...

Solving the login problem with Create React App, Express.js, and Passport.js

As I integrate Create React App with Express.js on separate ports, with the front-end on 3000 and back-end on 3001, I encounter an issue with my Local Passport Strategy for the login system. The login process works seamlessly, as the register/login form su ...

Exploring the wonders of Vue.js and Laravel's type casting techniques

I have implemented DB2-style IDs for my database records in my Laravel 5.7 application, like this example: 201402241121000000000000. When trying to use it in my Vue component, I used the following syntax: <mycomponent v-bind:listing-key="{{ $listing-&g ...

Continue to receive fresh activities for finished requests in rsocket

Given a scenario where an Rsocket endpoint is set up using Spring, @MessageMapping("chat.{chatId}") Flux<Message> getChats(@DestinationVariable String chatId) { Mono<Chat> data = chatRepository.findById(chatId); ...

Are there any versions of array_keys that can handle partial matching?

Is there a way to retrieve all keys in a PHP array where the value contains a specific search element? array_keys is helpful when the value matches the search term exactly, but it doesn't work if the search term appears somewhere within the value wit ...

Save a CSV file into a list, locate a specific key within the list, and retrieve the fourth value associated with that key

After thorough revision, the question now reads as follows: Take a look at this content in database.csv: barcode,ScQty, Name , Qty ,Code 123456 , 3 ,Rothmans Blue , 40 ,RB44 234567 , 2 ,Rothmans Red , 40 ,RB30 345678 , 2 ,Rothmans Gre ...

Encountered an issue when trying to deploy NextJS application to AWS using the serverless framework

Encountered an issue while deploying my NextJS app to AWS using the serverless framework. After running npx serverless in my Next JS app directory, I received the following error: $ npx serverless error: Error: Command failed with ENOENT: node_module ...

Using Javascript to create an array filled with even numbers up to 100 and then calculating the average

Hey there, I'm struggling with a class assignment that involves finding the average of an array of even numbers that we're supposed to generate. Unfortunately, I can't seem to get it working properly. var arrayOfNumbers = []; for (var num ...

Error: Service Liferay encountered an unexpected identifier causing a syntax error

Upon clicking on the button labeled "delete save" in the Chrome console, an error is encountered: "Uncaught SyntaxError: Unexpected identifier". Additionally, when this action is performed, an object is printed to the console revealing its data ({company ...

The console does not display client-side errors in node.js

When working on a school Chromebook with disabled developer tools, debugging client-side code in node.js can be challenging. Without access to error messages, it's frustrating to encounter issues that cause the code to stop working without any indicat ...

Tips for deleting error controller files in AngularJS

I have been working on creating a basic login page with a click event on the login button. The view is displaying properly, but I am encountering an error in the console: Error: [ng:areq] http://errors.angularjs.org/1.3.13/ng/areq?p0=controllers%2FLoginCt ...

Reordering React Lists: Showcasing the Latest Addition on Top

I'm currently working on a React list and keys project. I want the latest item added to appear at the top. Can anyone offer some assistance with this? For example: import { useState } from "react"; function ListsKeys() { const [names, set ...

Is it possible to perform operations on arrays in Java similar to Matlab?

Is there a method to achieve the following task without the need to create a function or use a for loop? int[] ma = (3,4,4,5,6,7); ma += 5; This quick shortcut allows for adding 5 to all elements in the array, similar to the convenience provided by Matla ...

Synchronization issues arise when attempting to update the localStorage

Whenever I switch to dark mode, the update doesn't reflect in _app unless I have two tabs opened and trigger it in one tab. Then, the other tab gets updated with dark mode toggled, but not the tab where I pressed the toggle. I am using useSettings in ...

Utilize Nunjucks Templating to iterate through integers

I'm relatively new to nunjucks and it seems that performing a for loop based on a value rather than an object's size is not typical. However, I'm curious if anyone has found a way to accomplish this. Essentially, I want to iterate through a ...

What's the best way to align this text and icon next to each other?

I have text and an icon that are currently displaying far apart. How can I align the icon to the right of the text like shown below: Water Jug-> For reference, here is the link to a Codesandbox demonstration: https://codesandbox.io/s/responsivestack-ma ...

Converting an array value into JSON structure

I'm currently working on an email application that allows users to send messages. Everything is functioning well except for the recipients column. I temporarily hardcoded an email address to test the functionality, but in reality, the recipients field ...

Execution of Promises in sequence

In my current implementation, I have multiple promise functions structured like: return new Promise(function(resolve, reject) { //logic }); cart.getBasket(req) cart.updateBasket(req) cart.updateDefaultShipment(req) cart.getBasketObject(basket) The cod ...

What is the quickest way to accomplish this task?

Currently, I am in the process of constructing a dashboard using ASP.Net MVC, Angular.js, SQL Server, and Fusion charts. The data needed for charting is stored in a database and obtained through a stored procedure. My objective now is to convert the result ...

Guide on implementing an onClick trigger for an IconButton contained within a TableCell

As a beginner with MaterialUI, I apologize in advance if my question seems too simple. I am currently working on pulling some data, displaying it in a table, with an extra column that includes a button: <TableCell> {<IconButton> <ClearIc ...