Search code examples
angularautocompletetypeaheadag-gridng-bootstrap

Typeahead with ag-grid using Angular 2


I made a table with ag-grid using custom filters. I would like to add typeahead/autocomplete function to the search fields. but I'm getting an error:

Can't bind to 'ngbTypeahead' since it isn't a known property of 'input'. (" Filter: ][ngbTypeahead]="search" (ngModelChange)="onChange($event)" [ngModel]="text"> "): ProjectFilterComponent@2:8

I'm using an external library(ng-bootstrap)for the typeahead in my custom filter.

@Component({
selector: 'filter-cell',
template: `
    Filter: <input style="height: 20px"
    [ngbTypeahead]="search"
    (ngModelChange)="onChange($event)"
    [ngModel]="text">
`

})

class ProjectFilterComponent implements AgFilterComponent{
public params:IFilterParams;
public valueGetter:(rowNode:RowNode) => any;
public text;
allTextFromProject = this.order.projects

constructor(private order : WorkorderComponent){


}

@ViewChild('projectinput', {read: ViewContainerRef}) public input;




agInit(params:IFilterParams):void {
    this.params = params;
    this.valueGetter = params.valueGetter;


}

isFilterActive():boolean {
    return this.text !== null && this.text !== undefined && this.text !== '';
}

doesFilterPass(params:IDoesFilterPassParams):boolean {
    return this.text.toLowerCase()
        .split(" ")
        .every((filterWord) => {
            return this.valueGetter(params.node).toString().toLowerCase().indexOf(filterWord) >= 0;
        });
}

getModel():any {
    return {value: this.text};
}

setModel(model:any):void {
    this.text = model.value;
}

afterGuiAttached(params:IAfterFilterGuiAttachedParams):void {

    this.input.element.nativeElement.focus();
}

search = (text$: Observable<string>) =>{
    text$
        .debounceTime(200)
        .distinctUntilChanged()
        .map(term => term.length < 1 ? []
            : this.order.projects.filter(v => new RegExp(term, 'gi').test(v)).splice(0, 10));


onChange(newValue):void {
    if (this.text !== newValue) {
        console.log(newValue)

        this.text = newValue;
        this.order.filterFunction(newValue, this.params.colDef.colId, this.order.page)



    }
}

The [ngbTypeahead] works fine in other search fields that doesn't use ag-grid. I need the [ngbTypeahead] function in the search fields of ag-grid


Solution

  • Import NgbTypeaheadModule and NgbModule into your ModuleImports in your createColomnDefs() method like this:

    createColumnDefs()  {
        this.columnDefs =[
            { headerName: "Extern id", field: "externalRefId", width:150, colId:"workorder.externalRefId",
                filterFramework:{
                component: PartialMatchFilterComponent,
                moduleImports: [FormsModule, NgbModule, NgbTypeaheadModule ]
            }},