File

projects/angular/components/ui-grid/src/body/ui-grid-column.directive.ts

Description

The grid column definition directive.

Implements

OnChanges OnDestroy

Metadata

Index

Properties
Inputs
Accessors

Inputs

description
Type : string
Default value : ''
disableToggle
Type : boolean
Default value : false

If the column can have visibility toggled.

method
Type : string

The method metadata used for searches.

minWidth
Type : number
Default value : 30

The minimum width percentage that the column should have when resizing.

primary
Type : boolean

If the column should be styled as primary.

property
Type : | string

The property that should be loaded in the associated row cell.

queryProperty
Type : | string

If defined, this will be used for sorting and filtering

refetch
Type : boolean
Default value : false

If the searchable dropdown associated to the column should trigger a data fetch when opened.

resizeable
Type : boolean
Default value : true

Configure if the column is resizeable or not.

searchable
Type : boolean
Default value : false

Configure if the column should be included in the search.

sort
Type : SortDirection
Default value : ''

The current sort of the column.

sortable
Type : boolean
Default value : false

Configure if the column is sortable.

title
Type : string

The column title.

visible
Type : boolean

If the column should be rendered, used as default state if toggle columns is turned on.

width

Set the column width, in %.

Properties

change$
Default value : new Subject<SimpleChanges>()

Emits when reactive properties change.

identifier
Default value : identifier()

The string identifier for the column.

(used for resize identification)

Accessors

width
getwidth()

Returns the column width, in %.

setwidth(value: number | string)

Set the column width, in %.

Parameters :
Name Type Optional
value number | string No
Returns : void
ariaSort
getariaSort()

Returns the aria-sort associated to the current sort.

primary
getprimary()

If the column should be styled as primary.

setprimary(primary: boolean)
Parameters :
Name Type Optional
primary boolean No
Returns : void
visible
getvisible()

If the column should be rendered, used as default state if toggle columns is turned on.

setvisible(visible: boolean)
Parameters :
Name Type Optional
visible boolean No
Returns : void
import { Subject } from 'rxjs';

import {
    ContentChild,
    Directive,
    Input,
    isDevMode,
    OnChanges,
    OnDestroy,
    SimpleChange,
    SimpleChanges,
    TemplateRef,
} from '@angular/core';
import { SortDirection } from '@angular/material/sort';
import { identifier } from '@uipath/angular/utilities';

import { UiGridDropdownFilterDirective } from '../filters/ui-grid-dropdown-filter.directive';
import { UiGridSearchFilterDirective } from '../filters/ui-grid-search-filter.directive';

/**
 * @ignore
 */
const ARIA_SORT_MAP: Record<SortDirection, string> = {
    // eslint-disable-next-line @typescript-eslint/naming-convention
    '': 'none',
    asc: 'ascending',
    desc: 'descending',
};

/**
 * @ignore
 */
const REACTIVE_INPUT_LIST: (keyof UiGridColumnDirective<Record<string, unknown>>)[]
    = ['sort', 'visible', 'title', 'primary'];

/**
 * The grid column definition directive.
 *
 * @export
 */
@Directive({
    selector: '[uiGridColumn], ui-grid-column',
})
export class UiGridColumnDirective<T> implements OnChanges, OnDestroy {
    /**
     * Set the column width, in `%`.
     *
     */
    @Input()
    set width(value: number | string) {
        if (
            isDevMode() &&
            typeof value === 'string' &&
            !value.endsWith('%')
        ) {
            console.error(`Width should be percentual for '${this.title}' column.`);
        }

        const width = typeof value === 'string' ? Number(parseFloat(value).toFixed(1)) : value;

        if (isNaN(width)) { return; }

        this._width = width * 10;
    }

    /**
     * Returns the column width, in `%`.
     *
     */
    get width() {
        return this._width;
    }

    /**
     * Returns the `aria-sort` associated to the current sort.
     *
     */
    get ariaSort() {
        return ARIA_SORT_MAP[this.sort];
    }

    /**
     * The string identifier for the column.
     *
     * (used for resize identification)
     *
     */
    identifier = identifier();

    /**
     * Configure if the column is sortable.
     *
     */
    @Input()
    sortable = false;

    /**
     * Configure if the column should be included in the search.
     *
     */
    @Input()
    searchable = false;

    /**
     * Configure if the column is resizeable or not.
     *
     */
    @Input()
    resizeable = true;

    /**
     * The column title.
     *
     */
    @Input()
    title?: string;

    /**
     * The property that should be loaded in the associated row cell.
     *
     */
    @Input()
    property?: keyof T | string; // nested property

    /**
     * If defined, this will be used for sorting and filtering
     *
     */
    @Input()
    queryProperty?: keyof T | string; // nested property

    /**
     * The method metadata used for searches.
     *
     */
    @Input()
    method?: string;

    /**
     * The current sort of the column.
     *
     */
    @Input()
    sort: SortDirection = '';

    /**
     * If the column should be styled as primary.
     *
     */
    @Input()
    get primary() {
        return this._primary;
    }
    set primary(primary: boolean) {
        if (primary === this._primary) { return; }
        this._primary = !!primary;

        this.change$.next({
            primary: new SimpleChange(!primary, primary, false),
        });
    }

    /**
     * If the column can have visibility toggled.
     *
     */
    @Input()
    disableToggle = false;

    /**
     * If the column should be rendered, used as default state if toggle columns is turned on.
     *
     */
    @Input()
    get visible() {
        return this._visible;
    }
    set visible(visible: boolean) {
        if (visible === this._visible) { return; }
        this._visible = !!visible;

        this.change$.next({
            visible: new SimpleChange(!visible, visible, false),
        });
    }

    /**
     * The minimum width percentage that the column should have when resizing.
     *
     */
    @Input()
    minWidth = 30;

    /**
     * If the searchable dropdown associated to the column should trigger a data fetch when opened.
     *
     */
    @Input()
    refetch = false;

    @Input()
    description = '';

    /**
     * The searchable dropdown directive reference.
     *
     * @ignore
     */
    @ContentChild(UiGridSearchFilterDirective, {
        static: true,
    })
    searchableDropdown?: UiGridSearchFilterDirective<T>;

    /**
     * The dropdown directive reference.
     *
     * @ignore
     */
    @ContentChild(UiGridDropdownFilterDirective, {
        static: true,
    })
    dropdown?: UiGridDropdownFilterDirective<T>;

    /**
     * The view template associated to the row cell.
     *
     * @ignore
     */
    @ContentChild(TemplateRef, {
        static: true,
    })
    html?: TemplateRef<any>;

    /**
     * Emits when reactive properties change.
     *
     */
    change$ = new Subject<SimpleChanges>();

    private _width = NaN;
    private _visible = true;
    private _primary = false;

    /**
     * @ignore
     */
    ngOnChanges(changes: SimpleChanges) {
        const isAnyPropertyChanged = Object.keys(changes)
            .filter(property => REACTIVE_INPUT_LIST.includes(property as keyof UiGridColumnDirective<T>))
            .map(property => changes[property])
            .some(change => !change.firstChange &&
                change.currentValue !== change.previousValue,
            );

        if (!isAnyPropertyChanged) { return; }

        this.change$.next(changes);
    }

    /**
     * @ignore
     */
    ngOnDestroy() {
        this.change$.complete();
    }
}

results matching ""

    No results matching ""