Retrieve a list of attributes from an Angular 4 array

In my Angular 4 application, I have an array of tasks that are displayed in a table. The JSON data structure for these tasks is as follows:

[
    {
        "id": "353610d2-4a6d-4dc3-9468-b88732d98397",
        "dueDate": "20/12/2017",
        "claimNumber": "19875677",
        "actionType": "Admission",
        "actionName": "Call TP Insurer",
        "owner": "Ben Clarke",
        "tags": [
            {
                "id": "78ef9592-7ed6-4192-aecc-4be8bb561f67",
                "description": "Third Party 2",
                "colour": "#df9626"
            }
        ]       
    }
]

The tasks are then displayed in a table structured like this:

<table>
  <thead>
    <tr>
      <th>Date Due</th>
      <th>
        <span (click)="onDisplayContext($event, 'ClaimNumber')">Claim Number</span>
      </th>
      <th>
        <span (click)="onDisplayContext($event, 'ActionType')">Action Type</span>
      </th>
      <th>
        <span (click)="onDisplayContext($event, 'ActionName')">Action Name</span>
      </th>
      <th>
        <span (click)="onDisplayContext($event, 'Owner')">Owner</span>
      </th>
      <th>
        <span (click)="onDisplayContext($event, 'Tags')">Tags</span>
      </th>
      <th></th>
    </tr>
  </thead>

  <tbody>
    <tr *ngFor="let task of Tasks; let i = index">
      <td>{{task.dueDate}}</td>
      <td>{{task.claimNumber}}</td>
      <td>{{task.actionType}}</td>
      <td>{{task.actionName}}</td>
      <td>{{task.owner}}</td>
      <td>
        <div fxLayout="row">

          <div *ngFor="let tag of task.tags; let r = index">
            <span class="tag" [style.background-color]="tag.colour">{{tag.description}}</span>
          </div>
        </div>

      </td>
      <td>
        <div class="chk_round">
          <input type="checkbox" id="chk_{{task.id}}" />
          <label for="chk_{{task.id}}"></label>
        </div>
      </td>
    </tr>
  </tbody>

</table>

Each <th> element in the table header is clickable to open a popup that shows all distinct values from the corresponding column.

If a user clicks on a specific column header, how can I create another array from the original tasks array containing only the values of that particular column?

Answer №1

function displayContext($input, selectedColumn) {
  let columnArray = []
  this.Items.map(item => {
    columnArray.push(item[selectedColumn]);
  });
  //additional code can be added here
}

Hopefully this solution is useful to you

Answer №2

Your data structure and the function you reference in the onDisplayContext method seem to be slightly off, but here's a suggested adjustment:

Make sure the property name matches the actual data:

onDisplayContext($event, 'claimNumber')

Apply this change throughout your template for all properties.

Retrieve the necessary data within your component:

onDisplayContext($event, key: string) {
  // Filter out unique values
  const uniques = this.Tasks
    .map(row => row[key])   // Extract property values
    .filter((val, idx, arr) => arr.indexOf(val) === idx); // Identify unique values
  this.displayData(uniques);
}

Add the displayData method:

displayData(data: any[]) {
  // Implement popup or other action here.
  console.log(data);
}

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

Is it possible to host both PHP and an Angular 4 CLI site on the same port?

I am currently working on developing a frontend web application in Angular 4 that will be connecting to an existing webservice. The goal is to eventually have both the frontend and backend sit within the same domain. I am using Visual Studio Code for front ...

As I iterate through a MySQL array, JavaScript is able to manipulate the initial displayed data

I'm struggling to achieve the desired outcome with my code. It seems that when I iterate through an array of data, JavaScript only works on the first echoed data. Here is a snippet of the code: <?php $ids = array(); ...

Display the spinner until the data is successfully updated in the DOM using Angular

Dealing with a large dataset displayed in a table (5000 rows), I am attempting to filter the data using column filters. When applying the filter, I have included a spinner to indicate that the filtering operation is in progress. However, due to the quick ...

What is the best way to dynamically populate a list in Angular when a button is clicked?

Currently, I'm in the process of developing a website that will serve as the interface to control a robot. My goal is to create a user-friendly system where users can input latitude and longitude coordinates, click a designated button, and have those ...

Pass the chosen value from an Angular material select component to a specified function

I'm having trouble figuring out how to pass the selected value to a function in a mat-select without using ngModel. In the desktop version of my app, I have a list with clickable items, but on the iPad version, I am using a mat-select element. In the ...

Is ngModel the appropriate method for connecting a form to server-side scripts?

Just diving into the world of Angular, I'm embarking on my very first project - a hybrid mobile app using Ionic/Angular. Within my app, each user has their unique notification settings. There are ten different types of notifications that users can to ...

Exploring TypeScript methods for manipulating Apollo Client query results that have been transformed into props

After following a blog tutorial on GraphQL, I successfully integrated my GraphQL query with a React component to display the returned results. Everything seemed to be working fine as I could log this.props. However, when I tried to access individual data ...

Altering the order or structure of an array or object in Vue

At this moment, Vue is calling a NAPI within the mounted function to retrieve the value for teambytime2. The API call appears like this: https://i.sstatic.net/FvL2A.png Shown below is the Axios GET URL used to fetch the data and assign it to this.teamByT ...

Retrieving Headers from a POST Response

Currently, I am utilizing http.post to make a call to a .NET Core Web API. One issue I am facing is the need to extract a specific header value from the HTTP response object - specifically, the bearer token. Is there a method that allows me to achieve thi ...

Are JavaScript closures supposed to prevent the array from being editable through getArray()?

Primary objective: Allow arr information retrieval exclusively through the getArray method. Secondary objective: Enable arr data modification solely through the addToArray method. function TestObj(){ var arr = []; var b = 3; this.getArray = funct ...

Is there a way to selectively download a single Angular Material component without needing to download the entire library?

For my current project, I am utilizing Angular Material specifically for the DatePicker component. I am working with the most recent version of Angular. Is it necessary to download the entire Angular Material library, or can I just download the DatePicker ...

Dialog box obscuring PrimeNG dropdown menu

I'm working on an Angular2 app that utilizes PrimeNG components. My issue arises when trying to include a dropdown inside a dialog box. Despite my implementation, the dropdown ends up being cut off due to the constraints of the dialog box, as visible ...

Creating a customized HTTP class for Bootstrap in Angular 2 RC 5

During my experience with Angular 2 RC 4, I encountered a situation where I needed to create a class called HttpLoading that extended the original Http class of Angular2. I managed to integrate this successfully into my project using the following bootstr ...

Utilizing React to seamlessly transfer API data stored in the parent component's state to a child component for efficient mapping and display

Having recently started learning React, I find myself encountering challenges whenever I try to work with state. One particular issue I'm facing involves a parent component that consists of a form with a child component that is a select dropdown. The ...

When using TypeScript, the reducer function may not be recognized, causing the type to display as 'any

I am a beginner in Typescript and we are implementing hooks in our React application. We have a shared thunk action creator that triggers one of the actions. appSlice.ts type ThunkOptions = { method: number, api_url: string, body: any | null } ...

Error encountered due to an unset offset on an array that was previously displayed?

I am encountering an issue with the behavior of my code. Within a loop, I am checking if the array contains an 'data' index. If it does not exist, I attempt to identify the previous, next, and current values in the array. What is confusing me is ...

Having trouble with Ionic 4 navigation not triggering after a single click, requiring multiple clicks to navigate

I have a long list of items, around 40 in total, that load a page describing each item with photos, URLs, and other content. However, I find that I need to click two to three times before reaching this page. I suspect that the excessive use of HTML compone ...

Timepicker component being used within an ngFor loop, with each instance having its inputs updated by the ngModel binding

Currently, I have implemented an ngb timepicker within an ngFor loop. Here is the HTML code for the timepicker: <tr class="d-flex" *ngFor="let ct of workingTimingList.controls; let i = index; [formGroup]="ct" [attr.id]="'tr'+i"> <td&g ...

How to Use a For Each Loop with Google Maps DrawingManager to Create Polygons?

My Ionic 4 Application using Angular 8 incorporates a google maps component where I need to draw and edit multiple polygons, eventually saving their vertices in a database. Hard coding some polygons is easy with getPath() or getPaths(), but I'm utiliz ...

Using SCSS based on the browser language in Angular: A Step-by-Step Guide

Is there a way to implement SCSS that is dependent on the user's browser language? When I checked, I found the browser language specified in <html lang = "de"> and in the CSS code as html[Attributes Style] {-webkit-locale: "en&quo ...