Skip to content

Commit

Permalink
fix: type cell context
Browse files Browse the repository at this point in the history
  • Loading branch information
spike-rabbit committed Sep 26, 2024
1 parent c3e30c7 commit e5984b1
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Directive, TemplateRef } from '@angular/core';
import { CellContext } from "../../types/cell-context.type";

@Directive({ selector: '[ngx-datatable-cell-template]' })
export class DataTableColumnCellDirective {
constructor(public template: TemplateRef<any>) {}

static ngTemplateContextGuard<TRow = any>(dir: DataTableColumnCellDirective, ctx: any): ctx is CellContext<TRow> {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { DataTableColumnCellTreeToggle } from './tree.directive';
import { ColumnChangesService } from '../../services/column-changes.service';
import { TableColumnProp } from '../../types/table-column.type';
import { DataTableColumnGhostCellDirective } from './column-ghost-cell.directive';
import { HeaderCellContext } from '../../types/cell-context.type';
import { CellContext, HeaderCellContext } from '../../types/cell-context.type';

@Directive({ selector: 'ngx-datatable-column' })
export class DataTableColumnDirective implements OnChanges {
export class DataTableColumnDirective<TRow> implements OnChanges {
@Input() name: string;
@Input() prop: TableColumnProp;
@Input() frozenLeft: any;
Expand All @@ -33,12 +33,12 @@ export class DataTableColumnDirective implements OnChanges {
@Input() summaryTemplate: TemplateRef<any>;

@Input('cellTemplate')
_cellTemplateInput: TemplateRef<any>;
_cellTemplateInput: TemplateRef<CellContext<TRow>>;

@ContentChild(DataTableColumnCellDirective, { read: TemplateRef, static: true })
_cellTemplateQuery: TemplateRef<any>;
_cellTemplateQuery: TemplateRef<CellContext<TRow>>;

get cellTemplate(): TemplateRef<any> {
get cellTemplate(): TemplateRef<CellContext<TRow>> {
return this._cellTemplateInput || this._cellTemplateQuery;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -628,15 +628,15 @@ export class DatatableComponent<TRow = any> implements OnInit, DoCheck, AfterVie
* if described in your markup.
*/
@ContentChildren(DataTableColumnDirective)
set columnTemplates(val: QueryList<DataTableColumnDirective>) {
set columnTemplates(val: QueryList<DataTableColumnDirective<TRow>>) {
this._columnTemplates = val;
this.translateColumns(val);
}

/**
* Returns the column templates.
*/
get columnTemplates(): QueryList<DataTableColumnDirective> {
get columnTemplates(): QueryList<DataTableColumnDirective<TRow>> {
return this._columnTemplates;
}

Expand Down Expand Up @@ -711,7 +711,7 @@ export class DatatableComponent<TRow = any> implements OnInit, DoCheck, AfterVie
_internalRows: TRow[];
_internalColumns: TableColumn[];
_columns: TableColumn[];
_columnTemplates: QueryList<DataTableColumnDirective>;
_columnTemplates: QueryList<DataTableColumnDirective<TRow>>;
_subscriptions: Subscription[] = [];
_ghostLoadingIndicator = false;
protected verticalScrollVisible = false;
Expand Down Expand Up @@ -812,7 +812,7 @@ export class DatatableComponent<TRow = any> implements OnInit, DoCheck, AfterVie
/**
* Translates the templates to the column objects
*/
translateColumns(val: QueryList<DataTableColumnDirective>) {
translateColumns(val: QueryList<DataTableColumnDirective<TRow>>) {
if (val) {
const arr = val.toArray();
if (arr.length) {
Expand Down
4 changes: 2 additions & 2 deletions projects/ngx-datatable/src/lib/types/cell-context.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TableColumn } from './table-column.type';
import { SortDirection } from './sort-direction.type';
import { ActivateEvent } from './activate-event.type';
import { TreeStatus } from '../components/body/body-cell.component';
import { Observable } from 'rxjs';
import { BehaviorSubject, Observable } from 'rxjs';
import { RowOrGroup } from './group.type';

export interface HeaderCellContext {
Expand Down Expand Up @@ -30,7 +30,7 @@ export interface CellContext<TRow> {
isSelected: boolean;
rowIndex: number;
treeStatus: TreeStatus,
disable$: Observable<boolean>;
disable$: BehaviorSubject<boolean>;
onTreeAction: () => void;
expanded?: boolean;
}
6 changes: 3 additions & 3 deletions projects/ngx-datatable/src/lib/types/table-column.type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PipeTransform, TemplateRef } from '@angular/core';
import { ValueGetter } from '../utils/column-prop-getters';
import { HeaderCellContext } from './cell-context.type';
import { CellContext, HeaderCellContext } from './cell-context.type';

/**
* Column property that indicates how to retrieve this column's
Expand All @@ -12,7 +12,7 @@ export type TableColumnProp = string | number;
/**
* Column Type
*/
export interface TableColumn {
export interface TableColumn<TRow = any> {
/**
* Internal unique id
*
Expand Down Expand Up @@ -160,7 +160,7 @@ export interface TableColumn {
*
* @memberOf TableColumn
*/
cellTemplate?: any;
cellTemplate?: TemplateRef<CellContext<TRow>>;

/**
* Ghost Cell template ref
Expand Down
2 changes: 1 addition & 1 deletion projects/ngx-datatable/src/lib/utils/column-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export function isNullOrUndefined<T>(value: T | null | undefined): value is null
/**
* Translates templates definitions to objects
*/
export function translateTemplates(templates: DataTableColumnDirective[]): TableColumn[] {
export function translateTemplates<TRow>(templates: DataTableColumnDirective<TRow>[]): TableColumn[] {
const result: TableColumn[] = [];
for (const temp of templates) {
const col: TableColumn = {};
Expand Down
2 changes: 1 addition & 1 deletion src/app/basic/row-detail.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ import { FullEmployee } from "../data.model";
</ngx-datatable-column>
<ngx-datatable-column name="Expanded" [width]="80">
<ng-template let-row="row" let-expanded="expanded" ngx-datatable-cell-template>
<strong>{{ expanded === 1 }}</strong>
<strong>{{ expanded }}</strong>
</ng-template>
</ngx-datatable-column>
<ngx-datatable-column name="Name" [width]="200">
Expand Down

0 comments on commit e5984b1

Please sign in to comment.