Generating a table with two columns utilizing ng-repeat

I am currently dealing with an array of objects that I need to showcase in a two-column layout. The challenge arises because some columns have more data than others, requiring equi-height rows.

Despite utilizing Bootstrap and clearfix in my attempts to display the data, I have not been able to achieve equi-height rows.

<ng-include src="itemTemplateSrc" ng-repeat-start="tag in item.fields | filter:filterTagsByTagName" class="col-md-12"></ng-include>
<div class="clearfix" ng-if="$index%2===1"></div>
<div ng-repeat-end=""></div>

My goal now is to present the data in HTML using a two-column layout without splitting the array due to additional filters.

How can I effectively showcase the data from a single object array in an HTML table?

Answer №1

If you approach the issue as 2 tables with a single column rather than 1 table with 2 columns, it becomes much simpler to solve. Utilize the $odd and $even properties.

<div>
    <div class="col-md-6">
        <table class="table table-striped">
            <tbody>
                <tr ng-repeat="item in items">
                    <td ng-if="$even">...</td>
                </tr>
            </tbody>
        </table>
    </div>
    <div class="col-md-6">
        <table class="table table-striped">
            <tbody>
                <tr ng-repeat="item in items">
                    <td ng-if="$odd">...</td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

Answer №2

Displaying data in a table using ng-repeat is quite straightforward. Simply repeat a set of rows:

<table>
   <thead>
      <tr>
         <th>Name</th>
         <th>Age</th>
      </tr>
   </thead>
   <tbody>
      <!-- Use ng-repeat for each row -->
      <tr ng-repeat="user in $ctrl.users">
         <td>{{user.name}}</td>
         <td>{{user.age}}</td>
      </tr>
   </tbody>
</table>

If you want all row heights to be equal, CSS can help achieve that.

For equal height based on content within the rows, JavaScript will be necessary.

Answer №3

Check out this fiddler here.

Controller

function MyCtrl($scope) {
  $scope.name = 'Superhero';
  $scope.obj = ["data-1", "data-2", "data-3", "data-4"];
  $scope.arr = Array.apply(null, {
    length: $scope.obj.length/2
  }).map(Number.call, Number);
}

HTML

<div ng-controller="MyCtrl">
  <table>
  <tr ng-repeat="index in arr">
    <td>{{obj[index*2]}}</td>
    <td> {{obj[index*2+1]}}</td>
  </tr>
  </table>
</div>

I've created an index array based on half the length of your original array to generate a table with extracted items from your object.

EDIT With Filter Support

HTML

<div ng-controller="MyCtrl">
<input type="text" ng-model="filterText"/>
  <table>
  <tr ng-repeat="item in items = (obj | filter: filterText)" ng-show="$index < arrLength">
    <td>{{items[$index*2]}}</td>
    <td> {{items[$index*2+1]}}</td>
  </tr>
  </table>
</div>

Controller

function MyCtrl($scope) {
  $scope.name = 'Superhero';
  $scope.obj = ["data-1", "data-2", "data-3", "data-4"];
  $scope.arrLength = $scope.obj.length / 2;
  $scope.arr = Array.apply(null, {
    length: $scope.obj.length/2
  }).map(Number.call, Number);  
}

View the updated filter here.

This version includes support for basic filters.

Answer №4

If you're looking to ensure uniform row height, consider using a table. Tables automatically align row heights.

It's unclear if you want each individual row to be the same height, but utilizing a table will at least align columns on the same row.

This solution incorporates bootstrap classes. Place this code within a div.row>div.col-##-xx structure for the desired result with some additional styling.

To address odd numbers of objects in your array, I included an ng-if="vm.array[$index + 1]" on the second column to hide the last box in the table.

<div class="table-responsive">
      <table class="table table-striped">
        <tr ng-repeat="whatever in vm.array" ng-if="!($index % 2)">
          <td>
            {{vm.array[$index]}}
          </td>
          <td ng-if="vm.array[$index + 1]">
            {{vm.array[$index + 1]}}
          </td>
        </tr>
      </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

Generate interactive jQuery slideToggle containers that monitor user clicks via Google Analytics

I have been working on a PHP and mySql project where I need to retrieve a list of businesses from a database. Once I have the desired businesses, I want to display them consecutively inside their own "clickDiv" container using jQuery slideToggle to reveal ...

Utilizing JSON for HTML conversion in Codeigniter

public function getCrew(){ $search = $this->input->post('name'); if($this->input->post('ajax') && (!empty($search))){ $result = $this->model->getNames($search); foreach($result as $r){ ...

React and Material UI: troubleshooting problems with layout columns

I'm working on a project with three columns and I want to include a column for removing each row. Is it possible to add a "removing" column on the right? If so, how can I go about doing it? VIEW CODESANDBOX: HERE const CustomTableRow = ({ row, index ...

Running Javascript based on the output of PHP code

I have been working on my code with test.php that should trigger some Javascript when my PHP code, conditional.php, detects input and submits it. However, instead of executing the expected "Do Something in Javascript," it outputs "Not empty" instead. I fin ...

unusual problem with referencing jQuery mobile pages and implementing click functionality

I am facing a strange issue with my index.html page, which is a website built using jQuery Mobile. The problem arises when I click on a button multiple times within a form on this page - it generates an alert to confirm that the button is working and then ...

How can I use CSS to change the background color of a fixed navbar when scrolling?

Is it possible to use only CSS to alter the background color of a fixed navbar once the user has scrolled past a specific point vertically? I am curious if this can be accomplished in CSS by targeting the ID of another element, or possibly its height? Alt ...

Utilize AJAX to update the database through a bootstrap modal interface

My current project involves creating a webpage to display database records with edit buttons that trigger bootstrap modals for user input and status changes. The goal is to use AJAX to update the database with any modifications made. However, I'm enco ...

Having trouble getting the header div to span the entire screen

I'm currently working on a project where I have set the header div to 100% width and the main container to also be 100% width. However, I am facing an issue where the right and left sides of the header are not filling the entire space. I managed to fi ...

Online Database for Hybrid Mobile Applications

Currently, I am in the process of developing a Hybrid mobile app with ionicframework, angularjs, and cordova. In my app, I will require data storage for all user details. One major aspect I am trying to resolve is how to update the database for all users. ...

A directive containing a template

I'm facing a unique challenge where I have to nest a template inside another template within my directive. The issue arises because AngularJS doesn't recognize ng-repeat code inside attributes. In an ideal scenario, here is how I envision my cod ...

Show a notification on the screen if the source of the iframe is blank

Is there a way to show a message "Please click on a match to get started" when no match link has been clicked? For example, when the iframe does not have an src attribute. My backend is built on Django. Below is the HTML code snippet: </form><b ...

Is there a way to disable a dropdown menu when clicking on a different one?

[![ Dashboard Admin Admin Profile Change Password Doctors Doctors List Doctors Appointments Doctors Requests Add a Doctor Patients ...

Issue with button press causing malfunction in (Service) function

I am facing a problem with using a particular service. I have set up a controller file and added the service to it. After injecting the service into the controller, I am attempting to access one of its functions by clicking a button. However, despite all m ...

Struggling to position the newest messages at the bottom of the list

I'm working on creating a web chat system similar to IRC where the latest messages should always be at the bottom of the chat window. Below is my attempt, but unfortunately it's not working as expected: ...

Determine if a specific identifier is already present using Javascript or Jquery

Is there a way to determine if an html tag with its specific id is present more than once in the code? This is my pseudocode for checking: if($('#myId').length > 1) { // It exists twice } ...

Display the information stored in an array onto the console using console.log in Angular with Typescript

.html file <div class="container" *ngFor="let info of (this.info || [])"> <h5 class="card-title" (click)="getInfo()">{{info.paragraph1}}</h5> </div> .ts file getInfo () { *****console.lo ...

Cut an image using CSS while maintaining its original quality

I am currently working on an upload page where users can upload images. Here is the link to the page: MyPage. However, I would like to implement image cropping functionality that displays more of the image rather than just a small portion as it does now. A ...

Steps to launching a URL in a new window on Internet Explorer

I want it so that when button1 is clicked, the URL opens in a new window using Internet Explorer. ...

Is it possible to rotate just the camera in ThreeJS by dragging the mouse within the scene?

Currently, I am involved in a project using ThreeJS and I am looking to implement camera rotation using the mouse. Although I have come across OrbitControls that allow me to rotate the camera around a point or object, I am unable to achieve the camera rota ...

Is there a way to ensure that the images in the slider all remain consistent in size?

I recently created a slider using bootstrap and encountered an issue with image display on mobile screens compared to laptop screens. On a Laptop screen: On a Mobile screen: The images on the mobile screen are not fitting properly within the slider, and ...