Filtro de búsqueda de cuadrícula de árbol de Web Components

    The Ignite UI for Web Components Search Filter feature in Web Components Tree Grid enables the process of finding values in the collection of data. We make it easier to set up this functionality and it can be implemented with a search input box, buttons, keyboard navigation and other useful features for an even better user experience. While browsers natively provide content search functionality, most of the time the IgcTreeGridComponent virtualizes its columns and rows that are out of view. In these cases, the native browser search is unable to search data in the virtualized cells, since they are not part of the DOM. We have extended the Web Components Material table-based grid with a search API that allows you to search through the virtualized content of the IgcTreeGridComponent.

    Web Components Search Example

    The following example represents IgcTreeGridComponent with search input box that allows searching in all columns and rows, as well as specific filtering options for each column.

    Web Components Search Usage

    Tree Grid Setup

    Comencemos creando nuestra cuadrícula y vinculándola a nuestros datos. ¡También agregaremos algunos estilos personalizados para los componentes que usaremos!

    <igc-tree-grid id="treeGrid" auto-generate="false" primary-key="ID" foreign-key="ParentID" allow-filtering="true" height="100%" width="100%">
        <igc-column field="Name" data-type="string" sortable="true"></igc-column>
        <igc-column field="ID" data-type="number" sortable="true"></igc-column>
        <igc-column field="Title" data-type="string" sortable="true"></igc-column>
        <igc-column field="Age" data-type="number" sortable="true"></igc-column>
        <igc-column field="HireDate" data-type="date" sortable="true"></igc-column>
    </igc-tree-grid>
    
    private treeGrid: IgcTreeGridComponent;
    
    constructor() {
        this.treeGrid = document.getElementById('treeGrid') as IgcTreeGridComponent;
        this.treeGrid.data = new EmployeesFlatData();
    }
    

    Great, and now let's prepare for the search API of our IgcTreeGridComponent! We can create a few properties, which can be used for storing the currently searched text and whether the search is case sensitive and/or by an exact match.

    private treeGrid: IgcTreeGridComponent;
    
    private searchBox: IgcInputComponent;
    
    private icon: IgcIconComponent;
    private nextIconButton: IgcIconButtonComponent;
    private prevIconButton: IgcIconButtonComponent;
    
    private caseSensitiveChip: IgcChipComponent;
    private exactMatchChip: IgcChipComponent;
    

    Web Components Search Box Input

    Now let's create our search input! By getting the input element we can get its current value. This will allow us to use the IgcTreeGridComponent's FindNext and FindPrev methods to highlight all the occurrences of the SearchText and scroll to the next/previous one (depending on which method we have invoked).

    Both the FindNext and the FindPrev methods have three arguments:

    • Text: string (the text we are searching for)
    • (optional) CaseSensitive: boolean (should the search be case sensitive or not, default value is false)
    • (optional) ExactMatch: boolean (should the search be by an exact match or not, default value is false)

    When searching by an exact match, the search API will highlight as results only the cell values that match entirely the SearchText by taking the case sensitivity into account as well. For example the strings 'software' and 'Software' are an exact match with a disregard for the case sensitivity.

    The methods from above return a number value (the number of times the IgcTreeGridComponent contains the given string).

    <igc-input id="searchBox" name="searchBox">
    </igc-input>
    
    constructor() {
        this.searchBox = document.getElementById('searchBox') as IgcInputComponent;
        this.caseSensitiveChip = document.getElementById('caseSensitiveChip') as IgcChipComponent;
        this.exactMatchChip = document.getElementById('exactMatchChip') as IgcChipComponent;
    }
    
    public nextSearch() {
        this.treeGrid.findNext(this.searchBox.value, this.caseSensitiveChip.selected, this.exactMatchChip.selected);
    }
    

    Add Search Buttons

    In order to freely search and navigate among our search results, let's create a couple of buttons by invoking the FindNext and the FindPrev methods inside the buttons' respective click event handlers.

    <igc-icon-button id="prevIconBtn" variant="flat" name="prev" collection="material" ></igc-icon-button>
    <igc-icon-button id="nextIconBtn" variant="flat" name="next" collection="material"></igc-icon-button>
    
    constructor() {
        this.nextIconButton = document.getElementById('nextIconBtn') as IgcIconButtonComponent;
        this.prevIconButton = document.getElementById('prevIconBtn') as IgcIconButtonComponent;
        this.nextIconButton.addEventListener("click", this.nextSearch);
        this.prevIconButton.addEventListener("click", this.prevSearch);
    }
    
    public prevSearch() {
        this.treeGrid.findPrev(this.searchBox.value, this.caseSensitiveChip.selected, this.exactMatchChip.selected);
    }
    
    public nextSearch() {
        this.treeGrid.findNext(this.searchBox.value, this.caseSensitiveChip.selected, this.exactMatchChip.selected);
    }
    

    We can also allow the users to navigate the results by using the keyboard's arrow keys and the ENTER key. In order to achieve this, we can handle the keydown event of our search input by preventing the default caret movement of the input with the PreventDefault method and invoke the FindNext/FindPrev methods depending on which key the user has pressed.

    <igc-input id="searchBox" name="searchBox">
    </igc-input>
    
    constructor() {
        this.searchBox = document.getElementById('searchBox') as IgcInputComponent;
    
        this.searchBox.addEventListener("keydown", (evt) => { this.onSearchKeydown(evt); });
        this.searchBox.addEventListener("igcInput", (evt) => {
            this.treeGrid.findNext(evt.detail, this.caseSensitiveChip.selected, this.exactMatchChip.selected);
        });
    }
    
    public onSearchKeydown(evt: KeyboardEvent) {
        if (evt.key === 'Enter' || evt.key === 'ArrowDown') {
            evt.preventDefault();
            this.treeGrid.findNext(this.searchBox.value, this.caseSensitiveChip.selected, this.exactMatchChip.selected);
        } else if (evt.key === 'ArrowUp') {
            evt.preventDefault();
            this.treeGrid.findPrev(this.searchBox.value, this.caseSensitiveChip.selected, this.exactMatchChip.selected);
        }
    }
    

    Case Sensitive and Exact Match

    Now let's allow the user to choose whether the search should be case sensitive and/or by an exact match. For this purpose we can use simple selectable Chips and bind to the igcSelect event to determine when the user interacts with them.

    <igc-chip selectable="true" id="caseSensitiveChip">Case Sensitive</igc-chip>
    <igc-chip selectable="true" id="exactMatchChip">Exact Match</igc-chip>
    
    constructor() {
        this.caseSensitiveChip = document.getElementById('caseSensitiveChip') as IgcChipComponent;
        this.exactMatchChip = document.getElementById('exactMatchChip') as IgcChipComponent;
    
        this.caseSensitiveChip.addEventListener("igcSelect", (evt) => {
            this.treeGrid.findNext(this.searchBox.value, evt.detail, this.exactMatchChip.selected);
        });
        this.exactMatchChip.addEventListener("igcSelect", (evt) => {
            this.treeGrid.findNext(this.searchBox.value, this.caseSensitiveChip.selected, evt.detail);
        });
    }
    

    Persistence

    What if we would like to filter and sort our IgcTreeGridComponent or even to add and remove records? After such operations, the highlights of our current search automatically update and persist over any text that matches the SearchText! Furthermore, the search will work with paging and will persist the highlights through changes of the IgcTreeGridComponent's PerPage property.

    Adding icons

    Al utilizar algunos de nuestros otros componentes, podemos crear una interfaz de usuario enriquecida y mejorar el diseño general de toda nuestra barra de búsqueda. Podemos tener un bonito icono de búsqueda o eliminación a la izquierda de la entrada de búsqueda, un par de fichas para nuestras opciones de búsqueda y algunos iconos de diseño de materiales combinados con bonitos botones de estilo ondulado para nuestra navegación a la derecha.

    import { defineComponents, IgcInputComponent, IgcChipComponent, IgcIconComponent, IgcIconButtonComponent, registerIconFromText } from "igniteui-webcomponents";
    
    defineComponents(IgcInputComponent, IgcChipComponent, IgcIconComponent, IgcIconButtonComponent);
    

    Finalmente, ¡actualicemos nuestra plantilla con los nuevos componentes!

    <igx-input-group type="search" class="offset">
        <igx-prefix>
            <igx-icon *ngIf="searchText.length == 0">search</igx-icon>
            <igx-icon *ngIf="searchText.length > 0" (click)="clearSearch()">clear</igx-icon>
        </igx-prefix>
    
        <input #search1 id="search1" igxInput placeholder="Search" [(ngModel)]="searchText" (ngModelChange)="@@igObjectRef.findNext(searchText, caseSensitive, exactMatch)"
            (keydown)="searchKeyDown($event)" />
    
        <igx-suffix *ngIf="searchText.length > 0">
    
        </igx-suffix>
    </igx-input-group>
    

    We will wrap all of our components inside an IgcInputComponent. On the left we will toggle between a search and a delete/clear icon (depending on whether the search input is empty or not). In the center, we will position the input itself. In addition, whenever the delete icon is clicked, we will update our SearchText and invoke the IgcTreeGridComponent's ClearSearch method to clear the highlights.

    <igc-input id="searchBox" name="searchBox">
        <igc-icon id="icon" slot="prefix" name="search" collection="material"></igc-icon>
        <div slot="suffix">
            <igc-chip selectable="true" id="caseSensitiveChip">Case Sensitive</igc-chip>
            <igc-chip selectable="true" id="exactMatchChip">Exact Match</igc-chip>
        </div>
        <div slot="suffix">
            <igc-icon-button id="prevIconBtn" variant="flat" name="prev" collection="material" ></igc-icon-button>
            <igc-icon-button id="nextIconBtn" variant="flat" name="next" collection="material"></igc-icon-button>
        </div>
    </igc-input>
    
    constructor() {
        const prevIconText = "<svg width='24' height='24' viewBox='0 0 24 24'><path d='M15.41 7.41 14 6l-6 6 6 6 1.41-1.41L10.83 12z'></path></svg>";
        const nextIconText = "<svg width='24' height='24' viewBox='0 0 24 24'><path d='M10 6 8.59 7.41 13.17 12l-4.58 4.59L10 18l6-6z'></path></svg>";
        const searchIconText = "<svg width='24' height='24' viewBox='0 0 24 24'><path d='M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z' /></svg>";
        const clearIconText = "<svg width='24' height='24' viewBox='0 0 24 24' title='Clear'><path d='M19 6.41 17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z'></path></svg>";
    
        registerIconFromText('prev', prevIconText, 'material');
        registerIconFromText('next', nextIconText, 'material');
        registerIconFromText('search', searchIconText, 'material');
        registerIconFromText('clear', clearIconText, 'material');
    
        this.icon = document.getElementById('icon') as IgcIconComponent;
        this.searchBox = document.getElementById('searchBox') as IgcInputComponent;
    
        this.searchBox.addEventListener('igcInput', (evt) => {
            this.icon.name = evt.detail ? 'clear' : 'search';
        });
        this.icon.addEventListener('click', this.clearSearch);
    }
    
    public clearSearch() {
        this.searchBox.value = '';
        this.icon.name = 'search';
        this.treeGrid.clearSearch();
    }
    

    A la derecha de nuestro grupo de entrada, creemos tres contenedores separados con los siguientes propósitos:

    • For displaying a couple of chips that toggle the CaseSensitive and the ExactMatch properties. We have replaced the checkboxes with two stylish chips. Whenever a chip is clicked, we invoke its respective handler.
    <div slot="suffix">
        <igc-chip selectable="true" id="caseSensitiveChip">Case Sensitive</igc-chip>
        <igc-chip selectable="true" id="exactMatchChip">Exact Match</igc-chip>
    </div>
    
    constructor() {
        this.caseSensitiveChip = document.getElementById('caseSensitiveChip') as IgcChipComponent;
        this.exactMatchChip = document.getElementById('exactMatchChip') as IgcChipComponent;
    
        this.caseSensitiveChip.addEventListener('igcSelect', (evt) => {
            this.treeGrid.findNext(this.searchBox.value, evt.detail, this.exactMatchChip.selected);
        });
        this.exactMatchChip.addEventListener('igcSelect', (evt) => {
            this.treeGrid.findNext(this.searchBox.value, this.caseSensitiveChip.selected, evt.detail);
        });
    }
    
    • For the search navigation buttons, we have added two ripple styled buttons with material icons. The handlers for the click events remain the same - invoking the FindNext/FindPrev methods.
    <div slot="suffix">
        <igc-icon-button id="prevIconBtn" variant="flat" name="prev" collection="material" ></igc-icon-button>
        <igc-icon-button id="nextIconBtn" variant="flat" name="next" collection="material"></igc-icon-button>
    </div>
    
    constructor() {
        const nextIconButton = this.nextIconButton = document.getElementById('nextIconBtn') as IgcIconButtonComponent;
        const prevIconButton = this.prevIconButton = document.getElementById('prevIconBtn') as IgcIconButtonComponent;
        nextIconButton.addEventListener("click", this.nextSearch);
        prevIconButton.addEventListener("click", this.prevSearch);
    }
    
    public prevSearch() {
        this.treeGrid.findPrev(this.searchBox.value, this.caseSensitiveChip.selected, this.exactMatchChip.selected);
    }
    
    public nextSearch() {
        this.treeGrid.findNext(this.searchBox.value, this.caseSensitiveChip.selected, this.exactMatchChip.selected);
    }
    

    Known Limitations

    Limitación Descripción
    Buscando en celdas con una plantilla Las funciones destacadas de búsqueda funcionan solo para las plantillas de celda predeterminadas. Si tiene una columna con una plantilla de celda personalizada, los resaltados no funcionarán, por lo que deberá utilizar enfoques alternativos, como un formateador de columnas, o configurar elsearchable propiedad en la columna a falso.
    Virtualización remota La búsqueda no funcionará correctamente al utilizar la virtualización remota
    Celdas con texto cortado Cuando el texto en la celda es demasiado grande para caber y el texto que estamos buscando está cortado por los puntos suspensivos, aún nos desplazaremos hasta la celda y la incluiremos en el recuento de coincidencias, pero no se resaltará nada.

    API References

    In this article we implemented our own search bar for the IgcTreeGridComponent with some additional functionality when it comes to navigating between the search results. We also used some additional Ignite UI for Web Components components like icons, chips and inputs. The search API is listed below.

    IgcTreeGridComponent methods:

    • FindNext
    • FindPrev
    • ClearSearch
    • RefreshSearch

    IgcColumnComponent properties:

    Componentes adicionales con API relativas que se utilizaron:

    Additional Resources

    Nuestra comunidad es activa y siempre da la bienvenida a nuevas ideas.