简体   繁体   中英

PrimeNG remove row selection from table in single selection

I tried use Table - CRUD from PRIMENG but, i've the same problem just like the example. The selection continues even when i close the dialog. I wanna clear that selection after i close the dialog. i has tried even with @ViewChild references from class Table.

@ViewChild('dtUserEmp') table: Table; 

TagID

<p-table #dtUserEmp selectionMode="single"
       [(selection)]="usuarioEmpresaSelection"
       (onLazyLoad)="lazyLoadingNat($event)"
       (onRowSelect)="rowSelect($event)"
       (onRowUnselect)="rowUnselect($event)"
       dataKey="empresa"...>... </p-table>

Link from documentation that i following: https://www.primefaces.org/primeng/#/table/crud

This is not a best way to do it but I can tell you the approach how you can achieve this. Later you can do it in better way. You also see some error in console but you can handle them easily and it will not break your app. So lets break your requirement like below:

Now whenever you will click on any row class ui-state-highlight is getting added. So the key is remove this class whenever we click on save.

Suppose below is my table and I have given #dd as reference to this table.

<p-table #dd [columns]="cols" [value]="cars" 
  selectionMode="multiple" 
  [(selection)]="selectedCars2" 
  [metaKeySelection]="true">
...
</p-table>

Now whenever you call the save button just pass the dd inside that or you can use the dd as @ViewChild in your component.

<button (click)="save(dd)">Save</button>
or
import { Table } from '../../../../node_modules/primeng/components/table/table';
export class TableComponent{
 @ViewChild('dd') dd: Table;
 save(){
 }

}

Now in component file inside your save function do the below: This is where you will find all the row e.tableViewChild.nativeElement.children[2].children

save(e:any){
    let element = e.tableViewChild.nativeElement.children[2].children;
    for(let key in element){
        (element[key].classList).remove('ui-state-highlight');
    }
  }

This will remove the class from the row in table. You can print the value and can try to fetch the value dynamically for children[2]. Which I hardCoded to 2. You need to search for tbody . I hope this position is fixed for the table. So hardcoded will work everywhere. Though you can make it dynamic.

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