Ag Grid shared functions possible?

Discussão em 'Angular' iniciado por Chip, Março 21, 2025.

  1. Chip

    Chip Guest

    I have several grids that utilize functions to format data (mostly ValueFormatter functions). I would like to put them into one shared TS file to be imported and used by multiple components. However, when I do this, I get an error about Promise

    (Error Build:'Promise' only refers to a type, but is being used as a value here. Do you need to
    change your target library? Try changing the 'lib' compiler option to es2015 or later.)


    simply by importing formatDate from angular/common. My lib compiler option is es2018... so that's not it.

    "lib": [
    "es2018",
    "dom"
    ],


    Has anyone done something similar? For example, a function I would like to use on multiple grids/components:

    dateFormatter(params: ValueFormatterParams) {
    var myDate: string = '';
    if (params.value) {
    myDate = formatDate(new Date(params.value), 'MM/dd/yyyy', 'en-US');
    }
    return myDate;
    }


    Would think I could just create a grid-shared.ts file importing the things needed and then export the functions but I get build errors shown above.

    Here is the grid-shared.ts file I am trying to build for re-use of code.

    import { formatDate } from "@angular/common";
    import { IDateFilterParams, ValueFormatterParams } from "ag-grid-community";

    export function caseFormatter(params: ValueFormatterParams) {
    if (params.value) {
    // pad number to be 7 characters in length
    var formattedValue: string = params.value.toString().padStart(7, "0");
    return formattedValue.slice(0, 6) + '-' + formattedValue.slice(6);
    }
    else { return ''; }
    }
    export function dateFormatter(params: ValueFormatterParams) {
    var myDate: string = '';
    if (params.value) {
    myDate = formatDate(new Date(params.value), 'MM/dd/yyyy', 'en-US');
    }
    return myDate;
    }

    export const dateTimeFilterParams: IDateFilterParams = {
    comparator: (filterLocalDateAtMidnight: Date, cellValue: string) => {
    const dateAsString = cellValue;
    if (dateAsString == null) return -1;
    const dateTimeStrings = dateAsString.split("T");
    const dateParts = dateTimeStrings[0].split("-");
    const cellDate = new Date(
    Number(dateParts[0]),
    Number(dateParts[1]) - 1,
    Number(dateParts[2]),
    );
    if (filterLocalDateAtMidnight.getTime() === cellDate.getTime()) {
    return 0;
    }
    if (cellDate < filterLocalDateAtMidnight) {
    return -1;
    }
    if (cellDate > filterLocalDateAtMidnight) {
    return 1;
    }
    return 0;
    },
    };

    Continue reading...

Compartilhe esta Página