简体   繁体   中英

How to get table's td value by checkbox checked in angular 8

MMy question is siple.I am trying to get td value by checking checkbox.When i click the checkbox i want to get same row product name.How to do it?

Demo: https://stackblitz.com/edit/angular-ol2sir?file=src/app/app.component.ts

app.component.ts:

 getProoduct(e){ 
     if(e.target.checked) 
      { 
       const target = e.originalEvent.toElement.closest('tr'); 
       let tdProduct= target.querySelector('td:nth-child(3)').innetText; 
       console.log(tdProduct); 
      }  
 }

Just replace your checkbox td with

    <td> <input type="checkbox" [(ngModel)]="data.isSelected" (change)="getProoduct(data.isSelected, data.product_name)"> </td>

And now the function with

  getProoduct(isSelected, product){
    console.log(isSelected, product)
  }

Full working example: https://stackblitz.com/edit/angular-glxfq5

Actually, you could directly pass data to the function and have all fields available in case you also need p_id or any other field in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM