What is the process for creating a two-column table in Angular?

Currently, I have an Angular template code that displays the header on the left and data on the right in a top to bottom layout.

<div class="panel-body">

        <table class="table table-striped">
            <tr ng-repeat="f in fields">
                <th>{{ f }}</th>
                <td>{{ data[f] }}</td>
            </tr>

        </table>

    </div>

However, I now want to modify this layout so that instead of displaying one field per row, I want to display two fields in one row, and then the third and fourth fields in the following row, and so on.

Essentially, I am looking for a 2-column layout with the field values distributed accordingly.

 <tr><th>{{ f }}</th>
 <td>{{ data[f] }}</td> 
 <th>{{ f }}</th>
 <td>{{ data[f] }}</td>
</tr>


field = ['id', 'name', 'username', 'email', 'age']

data = [{id:1, name: 'john', username: 'john', age: 20, email: 'test'}]

The desired output should look like:

<tr><td>id:</td><td>1</td><td>name:</td><td>john</td></tr>
<tr><td>username:</td><td>john</td><td>age:</td><td>20</td></tr>

This formatting requirement must be achieved using ng-repeat rather than hard coding each row.

Answer №1

Here is a suggestion that may meet your needs: http://plnkr.co/edit/ADKu2WEb9TyvEXASOJyz?p=preview

In order to achieve the multi-line functionality you desire, I am utilizing ng-repeat-start/ng-repeat-end as shown below:

<body ng-app="example" ng-controller="ExampleController">
  <table>
    <tr ng-repeat-start="row in data">
      <td>{{ label(0) }}:</td>
      <td>{{ value(row, 0) }}</td>
      <td>{{ label(1) }}:</td>
      <td>{{ value(row, 1) }}</td>
    </tr>
    <tr ng-repeat-end>
      <td>{{ label(2) }}:</td>
      <td>{{ value(row, 2) }}</td>
      <td>{{ label(3) }}:</td>
      <td>{{ value(row, 3) }}</td>
    </tr>
  </table>
</body>

The code snippet above demonstrates how to achieve the desired layout with AngularJS directives.

Additionally, here is a simple module and controller setup for reference:

angular.module('example', [])
  .controller('ExampleController', function ($scope) {
    var fields = [ 'id', 'name', 'username', 'email', 'age' ];
    $scope.data = [{id:1, name: 'john', username: 'john', age: 20, email: 'test'}];

    $scope.label = function (fieldNumber) {
      return fields[fieldNumber];
    };

    $scope.value = function (row, fieldNumber) {
      return row[fields[fieldNumber]];
    }
  });

Answer №2

Hey there, I made an attempt but you need to transform your reply into the following format:

$scope.data = {
    data1: {
        id: 1,
        name: 'john',
    },
    data2: {
        username: 'john',
        age: 20
    }
};

Also, the field variable is not required.

Here's how you should present it in your HTML even though I didn't provide instructions for two columns:

<table class="table table-striped">
            <tr ng-repeat="x in data">
                <td ng-repeat="(key,val) in x">{{key}} : {{val}}</td>
            </tr>

        </table>

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

How can I translate these JQuery functions into vanilla JavaScript?

I am currently in the process of building a configurator using transparent images. The image configurator functions through a basic JQuery function: I have assigned each input element a data attribute called data-Image. This function identifies the data-Im ...

Tips for automatically scrolling images horizontally in responsive layouts

Recently, I've been working on a website called . On the homepage, my portfolio images are arranged in a grid format using the <li> tag. I'm looking to align them side by side and have them scroll horizontally automatically, preferably with ...

What is the best way to add a `<div>` before or after a specific `<p>` element based on the client's height?

I am facing an issue with inserting a div before a paragraph element based on the given clientHeight. Specifically, I need to locate the closest paragraph element from the specified clientHeight and then add a div before or after that particular element. ...

upright scrolling mouse slider

In my project, I have implemented a vertical slider with mousewheel control using SwiperJS from https://swiperjs.com/. Although the slider is working perfectly fine, I am looking to fix the positions while scrolling on the page similar to the example pro ...

CSS: Create a form with two input fields where the left field is adjustable in width and the right field fills up

------------- -------------------------------------------------- | some unique text | | some more original content, varying length | ------------- -------------------------------------------------- |<---------------------- 100% width ------------------ ...

The ng-click functionality is not functioning as expected within the directive

Take a look at this snippet of my HTML: <section ng-controller="GalleryController as gallery"> <nav footer-directive></nav> </section> This is the GalleryController I'm using: angular .module('myapp') ...

Incorporate AJAX with ng2-select2 to enhance dynamic data loading

In my angular2js project, I have implemented multiple drop-downs using the ng2-select2 plugin. However, I am facing an issue with some of the dropdowns where I need to filter and retrieve data from an API based on the user input in the select2 search box. ...

The jQuery script is functioning flawlessly in one specific direction

Currently, I am utilizing a basic script to alter the color of elements based on the div being hovered over. While this works smoothly in one direction down the line, when trying to reverse order and backtrack, the colors do not function as intended. The ...

Preventing users from inputting the same number more than once

Here are a series of text input fields pertaining to a single question: <input type="text" name="{{ $answer->id }}" value="" style="width:20px; text-align:center" maxlength="1" oninput="this.value=this.value.replace(/[^0-5]/g,'');" /> & ...

Why hasn't the string been properly encoded?

Consider the scenario below: string text = "this is an example of formatted \"string\""; When I display this as a plain string in my web API method: return Ok(text) it will output: this is an example of formatted "s ...

The functionality of $viewContentLoading appears to be malfunctioning

Before the content is loaded, I'm attempting to log a value. I am using $viewContentLoading within the init function, but the value is not being logged. Can anyone help me identify the issue with my code? var app = angular.module('plunker&apos ...

When utilizing *NgIf, the button will be shown without the accompanying text being displayed

When trying to display either a confirm or cancel button based on a boolean set in my component.ts, I implemented the following code in my HTML: <mat-dialog-actions class="dialog-actions"> <button class="cancel-btn" ...

What is the best way to design a webpage that adapts to different screen heights instead of widths?

I'm in the process of designing a basic webpage for a game that will be embedded using an iframe. The game and text should always adjust to fit the height of your screen, so when the window is small, only the game is displayed. The game will be e ...

Enhancing data with AngularJS and Firebase

I enjoy using firebase, but sometimes their documentation lacks detail. When updating changes in the database, I follow their instructions. var ref= new $window.Firebase('https://mysite.firebaseio.com/arcade/'+$scope.gameID+'/scor ...

Determine the dropdown list value by analyzing the final two variables in a textfield

In my textfield, car registration numbers are meant to be entered. These registrations are based on years in the format GT 74454 12, with the last two digits "12" representing the year 2012. I am looking for a script that can automatically detect the last ...

What's the best way to ensure uniform card height in Material-UI?

Is there a way to ensure consistent card height in Material-UI without setting a fixed height? I want the card heights to dynamically adjust based on content, with all cards matching the tallest one on the website. How can this be achieved while also addin ...

Interactive Bootstrap Modals

Is it possible to create a "page" viewing experience within a bootstrap modal? This type of user interface flow is common in many mobile apps, but after reviewing Bootstrap's components, I'm unsure how to implement this or if it's even supp ...

What is the best way to seamlessly transition from responsive design to mobile design?

While developing a single-page app, I encountered an issue with the layout when the screen width reaches a specific point. On narrow screens, I prefer to utilize the full width, but for wider screens, I want to limit the width for better readability. The l ...

Using HTML format within an ionic list

Below is a list of products: <ion-list> <ion-item ng-repeat="item in productlistitems"> {{ item.name }}! </ion-item> </ion-list> The item names contain HTML tags, but they are displaying as plain text. Is there a w ...

How can I disable auto-fill for password input fields? Setting autocomplete="off" doesn't seem to be working

Hey there, I'm having some trouble with the autocomplete feature in Google Chrome. Can anyone suggest an alternative way to disable autocomplete for password fields? By the way, my project is using Vue.js. ...