Tips for capturing an event from a bespoke button component integrated within an ng2-smart-table

Currently, my task involves triggering an event in Angular2 by clicking a button within a child component that is displayed within a ng2-smart-table located in the parent component as a column. Unfortunately, I am facing the challenge that I cannot add an event listener directly in the tags of the child component that I am rendering. This limitation prevents me from giving any attributes. Any tips or suggestions on how to solve this would be greatly appreciated. Thank you!

Answer №1

In order to capture the data from a custom component, you need to define it in the settings of a smart table within the parent component.

setting.columns.button: {
            title: 'Actions',
            filter: false,
            type: 'custom',
            renderComponent: ActionButtonComponent,
            onComponentInitFunction(instance) {
              instance.save.subscribe(row => {
                console.log(this)
              });
            }
          }

As for the child component:

@Component({
  selector: 'app-action-button',
  template: `
 <span ngbDropdown>
    <button class="btn btn-danger btn-raised mr-1" id="dropdownRaised4" ngbDropdownToggle>Actions</button>
    <div ngbDropdownMenu class="dropdown-menu-right" aria-labelledby="dropdownRaised4">
      <button class="dropdown-item ontop" (click)="onClick(event)">
        <i class="icon-x"></i>Cancel</button>
    </div>
  </span>`,
  styleUrls: ['./action-button.component.scss']
})
export class ActionButtonComponent implements ViewCell, OnInit {
  renderValue: string;
  @Input() value: string | number;
  @Input() rowData: any;

  @Output() save: EventEmitter<any> = new EventEmitter();

  ngOnInit() {
    this.renderValue = this.value.toString().toUpperCase();
  }

  onClick(row) {
    this.save.emit(row);
  }
}

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

npm WARNING: The package @angular-devkit/[email protected] is in need of a peer dependency xxxx, however no installation for this dependency has

Attempting to launch an angular project and encountering the following errors: $ npm install npm WARN @angular-devkit/[email protected] requires a peer of @angular/compiler-cli@^14.0.0 but none is installed. You must install peer dependencies yoursel ...

Errors encountered during the Angular project build

I need help figuring out what's happening. I keep getting the same error while trying to build my project. I've already attempted deleting typings, angular directory, and performing typings install but nothing seems to be working. All the necess ...

Retrieve route parameters in Angular 6 without using promises

I am currently utilizing Angular 6. When working with route parameters, I typically use the following code snippet: this.route.params.subscribe(params => { // params can now be utilized as an object variable }); However, I find myself needing to a ...

There was a problem locating the animation entry named "routeAnimation" within the app: [ERROR ->]<app></app>. This issue occurred at Directive e within the e_Host@0:0

I'm currently working on implementing animations during route changes. Here is my code snippet: @Component({ selector: 'app', encapsulation: ViewEncapsulation.None, styleUrls: [ './app.css' ], template: totTemplate, ...

What's the best way to restrict characters in a paragraph?

I am working on an Angular application and I need to restrict the number of characters in a paragraph (p) to 50, after which it should break to a new line. Here is the template I am using: <section class="echeq-element-html-display border-box" [ng ...

In the search for "action.payload" using Redux Toolkit

const userSlice = createSlice({ name: 'user', initialState, reducers: { // Setting the user setUser: (state, action: PayloadAction<string | null>) => { state.user.email = action.payload; }, setLoading: (state, ...

Utilizing ngx-translate in Angular6 to dynamically load translations by making an API request to the backend

Using ngx-translate in my frontend, I aim to dynamically load translations upon app launch. The backend delivers a response in JSON format, for example: { "something: "something" } Instead of utilizing a local en.json file, I desire to integrate thi ...

What is the best way to combine individual function declarations in TypeScript?

In my project, I am currently developing a TypeScript version of the async library, specifically focusing on creating an *-as-promised version. To achieve this, I am utilizing the types provided by @types/async. One issue I have encountered is that in the ...

What strategies can I use to address the issue of requiring a type before it has been defined?

Encountered an intriguing challenge. Here are the types I'm working with: export interface QuestionPrimative { question : string; id : string; name : string; formctrl? : string; formgrp? : string; lowEx ...

Ways to toggle checkboxes to show or hide their child items and subitems

I'm working on creating a straightforward menu that allows users to click on a parent checkbox to expand or collapse its children. The challenge I'm facing is how to make the parent checkboxes expand while hiding the children items within them wh ...

Leveraging angular2-material with systemjs for Angular2 development

After completing the TUTORIAL: TOUR OF HEROES on this link, I attempted to integrate angular2-material into my project. Unfortunately, I am having issues with the CSS not displaying correctly. Can anyone provide insight into what I may be missing or doing ...

Issue customizing static method of a subclass from base class

Let me illustrate a simplified example of my current code: enum Type {A, B} class Base<T extends Type> { constructor(public type: T) {} static create<T extends Type>(type: T): Base<T> { return new Base(type); } } class A exte ...

A guide on specifying the data type for functions that receive input from React Child components in TypeScript

Currently, I am faced with the task of determining the appropriate type for a function that I have created in a Parent Component to retrieve data from my Child Component. Initially, I resorted to using type: any as a solution, although it was not the corr ...

Solutions for repairing data storage variables

I am trying to access the value dogovor from session_storage and assign it to variable digi. However, I seem to be encountering an issue with this process. Can anyone help me identify where I am going wrong? loadCustomer(){ return new Promise(resolve =& ...

Switching themes on Angular's ag-grid while maintaining infinite scrolling capabilities

In my Angular 9 application, I have successfully implemented an Ag-grid with infinite scroll. Now, I am looking to add a theme chooser similar to the one showcased in the demo. Simply changing the CSS class does not update the row heights when switching t ...

Prevent touching the pseudo content of an element

My issue involves a toggle button component where I have utilized the ::before pseudo class to add text. The problem arises when clicking on the text within the toggle button causes the button's state to change. How can this be prevented? Here is the ...

What is the best way to activate a file input element in my component and define the accepted file types at the same time?

My goal is to display a file upload dialog when a specific option is selected from a drop-down menu using ng-select. Each menu option corresponds to different file types, such as PDFs. Initially, I attempted to achieve this directly within the ng-select ta ...

I am encountering an issue with Angular where the following error message is displayed: "src/app/app.component.html:18:20 - error TS2339: Property 'DepScreen' does not exist on type 'AppComponent'"

This code snippet is from my app.component.html file: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0" ...

Typescript is failing to perform type checking

I'm encountering an issue while trying to utilize TypeScript type checking with the following code snippet: abstract class Mammal { abstract breed(other: Mammal); } class Dog extends Mammal { breed(other: Dog) {} } class Cat extends Mammal { ...

Encountering an Issue with Vue 3 and Vue Router 4: Uncaught TypeError - Trying to Access Undefined Properties (specifically 'push')

I'm currently working with Vue 3, Vue Router 4, and TypeScript in my project. However, I've encountered an issue while trying to utilize router.push(). Every time I attempt this, I receive a console error stating: Uncaught (in promise) TypeError: ...