Angular 6 - Accessing row information by clicking a toggle button

To retrieve the row details upon clicking a specific toggle button, I need to access the "row" data.

Below is the snippet of my HTML code:

<div class="container">
  <div>
    <h3>Manage Announcements</h3>
  </div>
  <div>
    <table mat-table [dataSource]="announcementsDataSource" class="mat-elevation-z8">
      <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
      <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
    
      <!-- Rest of the table structure goes here -->

This is my Typescript code for handling the row click event:

export class AnnouncementManageComponent implements OnInit {

  announcementsDataSource: AnnouncementResponse[];
  displayedColumns: string[] = ['title', 'text', 'type', 'for','fromDate','toDate','createdBy','updatedBy','createdDate','lastModifiedDate','displayOn'];

  constructor(private announcementManageService: AnnouncementManageService,
              private announcementRetrieveService: AnnouncementRetrieveService) {

  }

  ngOnInit() {
    this.announcementRetrieveService.getAllAnnouncements().then(announcements => {
      this.announcementsDataSource = announcements.announcements;
    });
  }

  displayModified(event: MatSlideToggleChange, modifiedAnnouncement:any) {
    console.log(event.checked);
    console.log(modifiedAnnouncement.title);
    console.log(modifiedAnnouncement.text);
    console.log(modifiedAnnouncement.displayOn);
  }

All the outputs in the console log from the displayModified function appear as undefined.

true
undefined
undefined
undefined

I am seeking guidance on how to extract the entire row data. Any suggestions?

Answer №1

Check out the code snippet below:

1. Insert the following function into your toggle button

(click)="updateRowData(element)"

2. Add the code below to your Typescript file

displaySelectedTask(selectedItem: any){
 var dataObject = JSON.stringify(selectedItem);

      var obj = JSON.parse(dataObject);
console.log(obj)
}

Answer №2

It would be beneficial to utilize an interface for the modifiedAnnouncement variable. An example showcasing this can be found at this link

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

Assigning variables within Redux saga generators/sagas

Consider this scenario: function* mySaga(){ const x = yield call(getX) } The value of const x is not determined directly by the return value of call(getX()). Instead, it depends on what is passed in mySaga.next(whatever) when it is invoked. One might a ...

Watching videos is quite a time-consuming process due to the long loading times

I recently created a website at , and I am facing an issue with the homepage video taking too long to play. While it works fine on a PC, it seems to load very slowly on mobile browsers. Any suggestions on how I can improve its loading speed? <video cl ...

Using a function as an argument to handle the onClick event

I have a function that generates a React.ReactElement object. I need to provide this function with another function that will be triggered by an onClick event on a button. This is how I call the main function: this._createInjurySection1Drawer([{innerDra ...

Creating an observable in rxjs6 Angular6 that automatically unsubscribes itself

I'm attempting to create an observable that will update the database status to 'away' when the mouse hovers over the document. Then, if the mouse becomes active again, it should update the status back to 'online'. Here is my curre ...

Error 404 encountered when updating packages in Angular2 tutorial: platform-browser-dynamic.umd.js

I recently started following an Angular2 tutorial, but upon returning to it and reaching the Routing chapter, I realized that the tutorial had been slightly updated. This required me to go back and update the package.json file to match the current version, ...

The 'posts' binding element is assumed to have a type of 'any' by default

Currently, I'm working on a code project that involves graphql, react, and typescript. In the middle of the process, I encountered an error message stating "Binding element 'posts' implicitly has an 'any' type." I am unsure about w ...

Centering on request, Google Maps adjusts its view to focus on

When I select a row, I want to set the map center to the provided coordinates in Primeng. The issue is that while this.options works fine in ngOnInit, it doesn't work when called in the showCords() function. Below is my code: gmap.component.ts im ...

Searching for an Angular component in Selenium

After reviewing the HTML code snippet below, <div class="dx-filterbuilder-action-icon dx-icon-plus dx-filterbuilder-action" tabindex="0"> ::before </div> I attempted to locate this element using Selenium C# with the followin ...

Organize data in the store using @ngrx/data and @ngrx/entity, rather than within the component (subscriber)

When working with the ngrx/data library, I often find myself having to manipulate and format data from the store in various parts of my application. While I have utilized the filterFn function to retrieve only the relevant entities for my use case, I am st ...

Multiple Invocations of Angular Observable Service

I am facing an issue with my service method that returns an observable. In my component, I subscribe to this observable when it is loaded and have added a console.log in the service to track each time it is called. Upon running my application and checking ...

Ensure that child components' property types are enforced in TypeScript

I am trying to enforce the type of a property in a child component. I expected the code below not to compile because Child's property name is not correctly typed inside Parent within the app. However, there is no compiler warning displayed. Is there ...

Navigating back to previous pages in an Angular(6) project connected to a Firebase Firestore database is a common requirement for

While there is an abundance of resources on how to paginate to the next page when using Angular with Cloud Firestore (Firebase), I have been unable to find a solution for pagination to a previous page. The closest I have come is returning to the first page ...

"The custom input component still displays a required error message even after all fields have been filled

After implementing the ControlValueAccessor interface to create a custom input component and setting the required property to true for form validation, I am facing an issue where the input field still shows a "required" error even when it is filled. The mo ...

Changing the function to operate asynchronously

How can I convert the following code into an asynchronous function? It is currently returning referralUrl as undefined: controller async createReferralUrls() { this.referralUrl = await this.referralService.generateReferralUrl(this.userData.referral ...

Transform the MUI Typescript Autocomplete component to output singular values of a specific property rather than a complete object

When utilizing MUI Autocomplete, the generic value specified in onChange / value is determined by the interface of the object set in the options property. For instance, consider an autocomplete with the following setup: <Autocomplete options={top ...

React Native is throwing an error message saying that the Component cannot be used as a JSX component. It mentions that the Type '{}' is not assignable to type 'ReactNode'

Recently, I initiated a new project and encountered errors while working with various packages such as React Native Reanimated and React Navigation Stack. Below is my package.json configuration: { "name": "foodmatch", "version ...

The power of negative multiplication in TypeScript and React

I am working with a state variable called sortDirection const [sortDirection, setSortDirection] = useState<1 | -1>(1); My goal is to allow a button to toggle the state variable like this setSortDirection(sortDirection * -1); However, I encounter a ...

Spring Boot - The Ultimate Guide to Hosting Angular2 Compiled Files

Currently, I am using a Spring Boot restful server alongside an Angular2 front-end application. In an attempt to streamline the process, I have been trying to host the front-end files on Tomcat in order to avoid serving them separately. Unfortunately, desp ...

The third-party SDK no longer includes names and autocomplete functionality

I am encountering an issue where my previously working SDK environment has suddenly stopped recognizing names and providing autocomplete. I am wondering what could have caused this problem - is it related to SDK maintenance or is the SDK offline? The SDK ...

Angular resolver does not return anything if the outcome is an observable generated using the .asObservable() method

While working on my project, I wanted to simulate my http calls by using a BehaviorSubject along with Observable properties in my resolver service. However, I faced an issue and I am not sure why this code snippet is not functioning as expected: schedule- ...