pivottable icon indicating copy to clipboard operation
pivottable copied to clipboard

Typescript definition file

Open trystanclarke opened this issue 10 years ago • 8 comments
trafficstars

Hi Nicolas, I am using the pivot table within a typescript app, and I put together a typescript definition file so I thought I would share it with you in case anyone else could find it useful. I haven't gone as far as branching/creating a pull request, just copy the below into a file called pivottable.d.ts:

// PivotTable Typescript definition file

interface PivotOptions
{
    /**
     * array of attribute names to use as rows, defaults to []
     */
    rows?: string[];

    /**
     * array of attribute names for use as columns, defaults to []
     */
    cols?: string[];

    /**
     * constructor for an object which will aggregate results per cell, defaults to count()
     */
    aggregator?: any;

    /**
     * function to generate output from pivot data structure (defaults to simple table)
     */
    renderer?: any;

    /**
     * object to define derived attributes, defaults to {}
     */
    derivedAttributes?: any;

    /**
     * function called on each record, returns false if the record is to be excluded from the input before rendering or true otherwise, (defaults to returning true for all records)
     */
    filter?: any;

    /**
     * object passed through to renderer as options
     */
    rendererOptions?: any;

    /**
     * locale-specific strings for error messages
     */
    localeStrings?: any;
}

interface PivotUiOptions
{
    /**
     * dictionary of rendering functions, defaulting with various table renderers 
     */
    renderers?: any;

    /**
     * dictionary of generators for aggregation functions in dropdown, defaulting to common aggregators
     */
    aggregators?: any;

    /**
     * array of strings, attribute names to prepopulate in row area, default is []
     */
    rows?: string[];

    /**
     * array of strings, attribute names to prepopulate in cols area, default is []
     */
    cols?: string[];

    /**
     * array of strings, attribute names to prepopulate in vals area, default is []
     */
    vals?: string[];

    /**
     * string, aggregator to prepopulate in dropdown (key to aggregators object), default is first key in aggregators
     */
    aggregatorName?: string;

    /**
     * string, renderer to prepopulate in radio button (key to renderers object), default is first key in renderers
     */
    rendererName?: string;

    /**
     * object, defines derived attributes, default is {}
     */
    derivedAttributes?: any;

    /**
     * function called on each record, returns false if the record is to be excluded from the input before rendering or true otherwise, (defaults to returning true for all records)
     */
    filter?: any;

    /**
     * object, defaults to {}, keys are attribute names and values are arrays of attribute values which denote records to exclude from rendering (used to prepopulate the filter menus that appear on double-click)
     */
    exclusions?: any;

    /**
    * array of strings, defaults to [], contains attribute names to omit from the UI
    */
    hiddenAttributes?: string[];

    /**
     * integer, defaults to 50, maximum number of values to list in the double-click menu
     */
    menuLimit?: number;

    /**
     * object, defaults to null, passed through to renderer as options
     */
    rendererOptions?: any;

    /**
     * function, called upon renderer refresh with an object representing the current UI settings
     */
    onRefresh?: (options: PivotUiOptions) => void;

    /**
     * object, defaults to English strings - locale-specific strings for UI display
     */
    localeStrings?: any;

    /**
     * boolean, defaults to false, controls whether or not unused attributes are kept sorted in the UI
     */
    autoSortUnusedAttrs?: boolean;

    /**
     * boolean, defaults to false, controls whether or not unused attributes are shown vertically instead of the default which is horizontally
     */
    unusedAttrsVertical?: boolean;

}

interface JQuery
{
    /**
     * Renders pivot table
     * @param data Data to use for pivot
     * @param options Pivot options
     */
    pivot(data: any[], options?: PivotOptions): JQuery

    /**
     * Renders interactive pivot table
     * @param data Data to use for pivot
     * @param options PivotUI options
     * @param overwrite Whether to overwrite state with supplied options when calling pivotUI repeatedly
     * @param locale String defaulting to 'en' which controls the default locale
     */
    pivotUI(data: any[], options?: PivotUiOptions, overwrite?:boolean, locale?:string): JQuery
}

// require js module support

declare var PivotTable: any;
declare module "pivottable" {
    export = PivotTable;
} 

trystanclarke avatar Jan 19 '15 08:01 trystanclarke

Very cool, thank you!

nicolaskruchten avatar Jan 19 '15 12:01 nicolaskruchten

Any reason why this has not been added yet @nicolaskruchten ?

Does it need additional work?

SamMousa avatar Nov 14 '16 13:11 SamMousa

@SamMousa I'm not able to commit the time to test and maintain this file, so I'm leery of merging it to master.

nicolaskruchten avatar Nov 14 '16 20:11 nicolaskruchten

Can someone help me to implement in this my project?

karthikvt avatar Feb 23 '18 12:02 karthikvt

I will be happy to pilot this if you can provide me a repo pivot table is a hot item be nice if it were remodeled in pure typescript

nhhockeyplayer avatar May 30 '18 17:05 nhhockeyplayer

@nhhockeyplayer I think the common practice here is to contribute a third-party type definition to https://github.com/DefinitelyTyped/DefinitelyTyped

nicolaskruchten avatar May 30 '18 18:05 nicolaskruchten

thanks for saving me the time to type this up!

ctrl-d avatar Feb 06 '22 08:02 ctrl-d

Hi, can anyone provide a code snippet as to how to generate a pivottable using the custom definition file? I am not sure why my compiler is not detecting my newly created type :/

tracknemo avatar Mar 20 '22 16:03 tracknemo