I am facing the dilemma of having an identical button appearing in two separate locations. How can I determine which button has been clicked?

I am currently using ng2-smart-table and have implemented a custom filter with the same button in both filters. However, I am unsure of how to determine which button is being clicked.

https://i.stack.imgur.com/b1Uca.png

Below is the component code for the button:

@Component({
  template: `
    <button (click)="onClick()">Select Range</button>
  `,
})
export class RangeFilterComponent extends DefaultFilter implements OnInit {

  inputControl = new FormControl();

  constructor(private dialogService: NbDialogService) {
    super();   
  }

  ngOnInit() {
  }

  onClick=()=>{
    this.dialogService.open(ShowcaseDialogComponent, {
      context: {
        title: 'Select Range',
      },
    });
  }
}

Here are the ng2-smart-table settings where I have rendered the button:

columns: {
      name: {
        title: 'Project Name',
        type: 'string',
      },
      description: {
        title: 'Description',
        type: 'string',
      },
      type: {
        title: 'Project Type',
        type: 'string',
      },
      scheme: {
        title: 'Scheme',
        type: 'string',
      },
      assigned_to: {
        title: 'Engineer',
        type: 'string',
      },
      assigned_contractor: {
        title: 'Contractor',
        type: 'string',
      },
      cost_disbursement: {
        title: 'Cost Disbursement',
        filter:{
          type: 'custom',
          component: RangeFilterComponent
        }
      },
      physical_progress: {
        title: 'Physical Progress',
        filter:{
          type: 'custom',
          component: RangeFilterComponent
        }
      },

Answer №1

Instructions on how to add functionality to your button.component.ts:

@Input()title:string

//Update the onClick function like this
onClick=()=>{
   console.log(this.title)
   ...
}

Make changes in the main.html as follows:

<app-button [title]="'one'">One</button>
<app-button [title]="'two'">Two</button>

If you have a fixed "input", it's recommended to use @attribute. For example, if you have in your app.component:

<app-button something="one">One</button>

Note that the "attribute" is not enclosed by [] and is simply a string. You can access the attribute in the constructor of app-button:

constructor(@Attribute('something') public something: string) {}

Check out this example on StackBlitz

This approach is more efficient because Angular does not need to query for an attribute every time.

Answer №2

perhaps you are looking to trigger a function when one of the buttons is clicked:

Within your configuration:

  cost_disbursement: {
    title: 'Cost Disbursement',
    filter:{
      type: 'custom',
      component: RangeFilterComponent,
      onComponentInitFunction: (instance) => {
      instance.click.subscribe(()=> {
        this.triggerActionA();
        });
      },
    },
  },
  physical_progress: {
    title: 'Physical Progress',
    filter:{
      type: 'custom',
      component: RangeFilterComponent,
      onComponentInitFunction: (instance) => {
      instance.click.subscribe(()=> {
        this.triggerActionB();
        });
      },
    }
  },

In your customized rendering component:

@Component({
  template: `
    <button (click)="onClick()">Select Range</button>
  `,
})
export class RangeFilterComponent extends DefaultFilter implements OnInit {

  inputControl = new FormControl();
  @Output() click = new EventEmitter();

  constructor(private dialogService: NbDialogService) {
    super();   
  }

  ngOnInit() {
  }

  onClick()=>{
    this.click.emit();
  }
}

and handle the modal opening in the triggerActionA() and triggerActionB()

Answer №3

Include an @input() attribute in your childComponent (Button Component) with the choices of 'Physical Progress' or 'Cost Disbursement' for the title, and then send it to onClick(title)

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

Steps for setting up tsconfig.json for Chrome extension development in order to utilize modules:

While working on a Chrome plugin in VS Code using TypeScript, I encountered an issue with the size of my primary .ts file. To address this, I decided to refactor some code into a separate module called common.ts. In common.ts, I moved over certain constan ...

Practice with Angular - Testing asynchronous service calls

Encountering an issue with the code while attempting to check the DOM element. I have used ngIf with a service async call in HTML, and the sample code is provided below: <div class="" *ngIf="EmployeeService.employees$ | async"> <div class="h ...

There are a pair of Ionic2 menus; one is currently visible while the other remains hidden

I am having an issue with my Ionic2 app where I have two pages, each with similar menus named XXX.html. One page displays its menu correctly, but the other does not show its menu at all. Is there a limitation in Ionic2 that prevents having two menus on the ...

Leverage properties within the storybook component template

When utilizing a class component in a narrative, it allows you to transmit properties as arguments: const Template: Story<MyComponent> = (args) => ({ props: args, component: MyComponent, }) export const Default = Template.bind({}); export co ...

Chrome Not Responding to Angular5 Debugging

I'm facing an issue where I used to be able to set breakpoints in my Angular code using developer tools, and it would pause correctly. However, recently the network files are not being mapped to my local files properly. For a detailed explanation, ple ...

What purpose does the array.pop()!(object) syntax serve within Codemirror?

Within the Codemirror 6 documentation and at line 41 of the code, there is a snippet that reads: while (pending.length) pending.pop()!(data.updates) I'm curious about the meaning of this syntax. It appears to be TypeScript specific. How would this lo ...

In my coding project using Angular and Typescript, I am currently faced with the task of searching for a particular value within

I am facing an issue where I need to locate a value within an array of arrays, but the .find method is returning undefined. import { Component, OnInit } from '@angular/core'; import * as XLSX from 'xlsx'; import { ExcelSheetsService } f ...

Changing HTML tags programmatically in Angular while inheriting a template

In my project, I have a Component called DataGrid that represents a table with expandable rows. Each row can be expanded to show a child DataGrid table, which is similar to the parent DataGrid component. To simplify this setup, I created a base class DataG ...

When working with Visual Studio and a shared TypeScript library, you may encounter the error message TS6059 stating that the file is not under the 'rootDir'. The 'rootDir' is expected to contain all source files

In our current setup with Visual Studio 2017, we are working on two separate web projects that need to share some React components built with TypeScript. In addition, there are common JavaScript and CSS files that need to be shared. To achieve this, we hav ...

Is there a disparity in capabilities or drawbacks between ViewChild and Input/Output in Angular?

As I delve into Angular ViewChild and compare it to Input/Output parameters, I can't help but wonder if ViewChild has any drawbacks or limitations compared to Input/Output. It appears that ViewChild is the preferred method, as all parameters are now ...

Change validators dynamically according to conditions

Scenario: At the start, there is a single text box named Name1, a date picker called DOB1, and a check box labeled Compare. Both Name1 and DOB1 are mandatory. When the checkbox is clicked, two new form controls are dynamically included, named Name2 and DO ...

Is the validator in my Angular reactive form malfunctioning?

I have been working on a service where I'm trying to validate the zip code field, but for some reason, the logic (result ? null : { IsInvalid: true }) is not executing. It makes me wonder if there's an issue with the reactive form or if I am usin ...

Finding the row index in an Angular material table

How can I retrieve the row index in an Angular material table? <td mat-cell *matCellDef="let row"> <mat-checkbox (click)="$event.stopPropagation()&quo ...

What is preventing me from generating Face3 in my ThreeJS Typescript project

Currently, I'm in the process of generating a Mesh using Face3 within a TypeScript project that utilizes Three.js. However, I've encountered a few issues along the way. const points = [ new Face3(-1, 1, -1),//c new Face3(-1, -1, 1),//b ...

Encountering issues accessing the key value 'templateUrl' in Angular6

I have an object in my component.ts file that I need to iterate through in the HTML template using a ui-comp-menu element. menuObject = [{ 'labels': 'Content1', 'templateUrl': 'assets/partials/sample.html ...

React component stuck in endless loop due to Intersection Observer

My goal is to track the visibility of 3 elements and update state each time one of them becomes visible. Despite trying various methods like other libraries, useMemo, useCallback, refs, etc., I still face challenges with my latest code: Endless loop scenar ...

What is the best way to bring in a file or subfolder from a folder that has been

Currently, I am facing a situation where I need to specify to TSC in the tsconfig file that I want to include specific files or subfolders from a folder while ignoring others. How can I achieve this? For instance: /. |-folder 1 |->file2.ts |-& ...

Creating an Observable from static data in Angular that resembles an HTTP request

I have a service with the following method: export class TestModelService { public testModel: TestModel; constructor( @Inject(Http) public http: Http) { } public fetchModel(uuid: string = undefined): Observable<string> { i ...

Component fails to navigate to the page of another component

Hello there. I am facing an issue where the button with routelink in the RegistrationComponent is not routing to the page of LogInComponent and I cannot figure out why. Angular is not throwing any errors. Here is the RouteComponent along with its view: im ...

Ways to convert an asynchronous operation to synchronous in JavaScript

Currently in the process of developing an eslint plugin, I have come across a particular issue. My goal is to implement real-time changes to the configuration file by making an HTTP request to retrieve the JSON configuration. When attempting to execute co ...