Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PrimeNG Table filterGlobal TS2339: Property 'value' does not exist on type 'EventTarget'

All of the examples I have found for the PrimeNG p-table show the following example for filtering a table.

<input pInputText type="text" (input)="dt.filterGlobal($event.target.value, 'contains')" placeholder="Filter" />

When I use this I get a compile error.

error TS2339: Property 'value' does not exist on type 'EventTarget'.

Note: I do have strict mode turned on.

like image 944
code Avatar asked Jan 02 '21 06:01

code


4 Answers

Your HTML input should be like this

<input pInputText type="text" (input)="applyFilterGlobal($event, 'contains')" placeholder="Filter" />

and in your TS do this

import { Table } from 'primeng/table'
...
applyFilterGlobal($event: any, stringVal: any) {
    this.dt!.filterGlobal(($event.target as HTMLInputElement).value, stringVal);
  }

if you get error for dt add below line

  @ViewChild('dt') dt: Table | undefined;
like image 133
Santosh nayak Avatar answered Nov 18 '22 11:11

Santosh nayak


try to parse the target to an HTMLInputElement first:

<input pInputText type="text" (input)="applyFilterGlobal($event, 'contains')" placeholder="Filter" />

and in your component:

applyFilterGlobal($event, stringVal) {
  this.dt.filterGlobal(($event.target as HTMLInputElement).value, stringVal);
}

event.target is an HTMLElement, because you are in strict mode, and HTMLElement doesnt have the value property, the compile engine throws the error, changing the target to HTMLInputElement solves it

like image 38
Moshezauros Avatar answered Nov 18 '22 12:11

Moshezauros


<input pInputText #textInput type="text(input)="tt.filterGlobal(textInput.value, 'contains')" placeholder="Filter" />

Adding #textInput to the tag worked for me.

like image 3
Washington Santos Avatar answered Nov 18 '22 12:11

Washington Santos


  1. write a function in component to retrieve the value of $event by passing the $event
getEventValue($event:any) :string {
  return $event.target.value;
} 
  1. apply the above function in your HTML code.
 <input pInputText type="text" (input)="dt1.filterGlobal(getEventValue($event), 'contains')" placeholder="Search keyword" />
like image 2
shajahan Avatar answered Nov 18 '22 11:11

shajahan