Exploring agGrid data search capabilities with Angular 2 and Typescript

How do I implement filtering in my agGrid component? I came across an example from agGrid, but it's written in JavaScript. https://www.ag-grid.com/angular-grid-filtering/index.php

However, I'm having trouble getting it to work with my TypeScript code below.

agGrid.component.ts

import {Component} from 'angular2/core';
import {AgGridNg2} from 'ag-grid-ng2/main';
import {GridOptions} from 'ag-grid/main';

import 'ag-grid-enterprise/main';

@Component({
    selector: 'app',
    templateUrl: 'app/partials/agGrid/agGrid.html',
    directives: [AgGridNg2]
})
export class AgGridComponent {

    columnDefs = [
        { headerName: "Contact Name", field: "make" },
        { headerName: "Company Name", field: "model" },
        {
            headerName: "Last Event",
            field: "price",
            cellClass: 'rightJustify',
            cellRenderer: function (params: any) {
                return '$' + params.value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); //thanks http://stackoverflow.com/users/28324/elias-zamaria
            }
        },
        { headerName: "Contact Email", field: "model" },
        { headerName: "Work Phone", field: "model" }
    ];
    // assign data directly onto the controller
    rowData = [
        { make: "Toyota", model: "Celica", price: 35000 },
        { make: "Ford", model: "Mondeo", price: 32000 },
        { make: "Porsche", model: "Boxter", price: 72000 }
    ];

    gridOptions = {
        columnDefs: this.columnDefs,
        rowData: null,
        enableFilter: true
    };



    values='';
    onKey(event:any) {
        this.values = event.target.value;
        this.gridOptions.columnDefs.setQuickFilter(this.values);
    }

    searchValue='';

}

grid.html

<input placeholder="Filter..." type="text"
   [value]="searchValue"
   (input)="searchValue = $event.target.value"
/>

<ag-grid-ng2
class="ag-fresh"
enable-sorting
enable-filter
style="height: 300px" 
[gridOptions]="gridOptions" 
(cellClicked)="onCellClicked()" 
[columnDefs]="columnDefs"
[rowData] = "rowData"> 
</ag-grid-ng2>

Answer №1

Link the ngModel variable to the "quickFilterText" attribute. The name of the attribute will be changed to "quickFilter" in upcoming updates.

<input type="text" 
    [(ngModel)]="searchValue" placeholder="quick filter..." /> 
<ag-grid-angular  
    class="data-grid ag-theme-balham"
    [rowData]="rowData | async" 
    [columnDefs]="columnDefs"
    [defaultColDef]="defaultColDef"
    [quickFilterText]="searchValue"
    >
</ag-grid-angular>

Answer №2

If you haven't already found a solution, consider binding your input box to a variable in your code using the 2-way method.

Here's how your HTML would appear:

<input type="text" (keyup)="onQuickFilterChanged()" 
    [(ngModel)]="quickSearchValue" placeholder="quick filter..." /> 

And here is an example of TypeScript code for your reference:

quickSearchValue: string = '';

private onQuickFilterChanged() {
    this.gridOptions.api.setQuickFilter(this.quickSearchValue);
}

I hope this explanation proves useful.

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

The error message "TypeError: Unable to access the 'getFullWidth' property of an undefined value when using TSLint and TypeScript" was

I have been using Dan Wahlin's tutorials and online examples to set up Gulp and Typescript, but I am facing an issue with the tslint() function. The problem occurs in my code as follows: node_modules\tslint\lib\language\walker&bso ...

use ".otherwise" or /** with the latest version of Angular2 Router to redirect non-routes using wildcards

Can anyone provide guidance on using wild cards to route in the new Router when a non functional route is specified? Similar to the .otherwise method in Angular 1.X router or /** in previous beta router. Additionally, any example or Plunker code would be ...

Conceal the footer on IONIC2 app when the keyboard is displayed

I am looking to hide the footer in Ionic2 when the keyboard appears. I have searched various forums for a solution but have not found the correct one yet. Here is my current footer code: <ion-footer> <div class="footer1" > <p>Do ...

Issue encountered during the creation process of a new component within Angular 4

While attempting to create a new component named signup-form using the command: ng generate component signup-form / ng g component signup-form An error is being thrown that reads: Unexpected token / in JSON at position 1154 The source of this error i ...

Using *ngFor in Angular for Parent-Child component interaction

Is there a way to iterate through a child component multiple times with different data each time? For instance, in my 'sitelist' component (parent), I aim to loop through my 'summary' component (child) as many times as needed based on ...

What are the steps to integrate FabricJs into a TypeScript project?

Recently started learning JavaScript and I am interested in rotating, changing scale of images on canvas and coding in TypeScript using Fabric.js. As the import method for TypeScript differs from JavaScript, I am looking for guidance on how to import it in ...

How to retrieve a value from a base64-decoded string in Angular 6?

I successfully decoded a Base64 string using the xml2js library and obtained the following XML value: <?xml version="1.0" encoding="UTF-8" standalone="no"?> <svg width="293" height="102" viewBox="0 0 293 102" xmlns="http://www.w3.org/2000/svg" ...

"Error occurs as a result of an unspecified attribute in the map

Hello, I am currently traversing a tree structure recursively. To handle undefined nodes in the tree, I have implemented guards to prevent any failures. However, during the mapping of children nodes, I encountered the following error: Error Output Adri ...

Issue with maintaining variable state in Angular 7 service component

I currently have 2 components and a single service file in my Angular project, which consist of a login component and a dashboard component. The issue arises when I try to access the user data from the service file. In the login component, the user data i ...

Ways to determine if two types are identically matched

My initial trial is demonstrated here: (play area link) /** Trigger a compiler error when a value is _not_ an exact type. */ declare const exactType: <T, U extends T>( draft?: U, expected?: T ) => T extends U ? T : 1 & 0 declare let ...

what are the steps to incorporate TypeScript definitions when using Google Maps?

When loading the Google Maps API in the browser through a <script> tag instead of the @google/maps node package, how can TypeScript types be assigned to it? For example, if utilizing the @types/googlemaps package or a similar one, the types may seem ...

Sending Object as Payload in a Node.js GET Request

I am encountering a challenge in passing an object from my angular application to my NodeJS server. While I am able to successfully read the object on the client-side, I am facing difficulties in doing so on the server-side. Below is the snippet of my cli ...

Step-by-step guide to creating a reverse index in ngFor with Angular

I am currently using the most up-to-date version of Angular and have an array named "myarray" containing 3 objects. My goal is to have div elements structured like this: <div id="num2"> <div id="num1"> <div id="num0"> Typically, I woul ...

Is it possible to interchange the positions of two components in a routing system?

driver-details.component.ts @Component({ selector: 'app-driver-details', templateUrl: './driver-details.component.html', styleUrls: ['./driver-details.component.css'] }) export class DriverDetailsComponent implements OnI ...

When ts-loader is used to import .json files, the declaration files are outputted into a separate

I've encountered a peculiar issue with my ts-loader. When I import a *.json file from node_modules, the declaration files are being generated in a subfolder within dist/ instead of directly in the dist/ folder as expected. Here is the structure of my ...

Issue with action creator documentation not displaying comments

We are exploring the possibility of integrating redux-toolkit into our application, but I am facing an issue with displaying the documentation comments for our action creators. Here is our old code snippet: const ADD_NAME = 'ADD_NAME'; /** * Se ...

Is there a deeper philosophical rationale behind choosing to use (or not use) enums in TypeScript, along with string union types?

Recently, I delved into the world of enum and const enum in Typescript, causing some confusion. I grasped that const enum gets transpiled into simple values while regular enums do not. I also recognized certain distinctions between using string union type ...

The export named 'Types' was not located. The module 'mongoose' that was requested is a CommonJS module, which may not fully support all module.exports as named exports

In my setup, I have an express server built in TypeScript with the "module": "es2020" configuration set in its tsconfig. Additionally, I have created another module for my GraphQL API also utilizing TypeScript and targeting es2020. Thi ...

The TSX file is encountering difficulty rendering an imported React Component

Upon attempting to import the Day component into the Week component (both .tsx files), an error is thrown: Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. ...

Having trouble retrieving the user-object within tRPC createContext using express

I'm encountering an issue with my tRPC configuration where it is unable to access the express session on the request object. Currently, I am implementing passport.js with Google and Facebook providers. Whenever I make a request to a regular HTTP rout ...