• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

IgniteUI / igniteui-angular / 16805526752

07 Aug 2025 01:13PM UTC coverage: 91.387% (-0.05%) from 91.44%
16805526752

Pull #16024

github

web-flow
Merge e235e83c8 into 6d3657091
Pull Request #16024: Grid Cell merging

13580 of 15959 branches covered (85.09%)

199 of 219 new or added lines in 10 files covered. (90.87%)

14 existing lines in 1 file now uncovered.

27354 of 29932 relevant lines covered (91.39%)

35690.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.76
/projects/igniteui-angular/src/lib/grids/cell.component.ts
1
import { useAnimation } from '@angular/animations';
2
import {
3
    ChangeDetectionStrategy,
4
    ChangeDetectorRef,
5
    Component,
6
    ElementRef,
7
    HostBinding,
8
    HostListener,
9
    Input,
10
    TemplateRef,
11
    ViewChild,
12
    NgZone,
13
    OnInit,
14
    OnDestroy,
15
    OnChanges,
16
    SimpleChanges,
17
    Inject,
18
    ViewChildren,
19
    QueryList,
20
    AfterViewInit,
21
    booleanAttribute
22
} from '@angular/core';
23
import { formatPercent, NgClass, NgTemplateOutlet, DecimalPipe, PercentPipe, CurrencyPipe, DatePipe, getLocaleCurrencyCode, getCurrencySymbol } from '@angular/common';
24
import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms';
25

26
import { first, takeUntil, takeWhile } from 'rxjs/operators';
27
import { Subject } from 'rxjs';
28

29
import { IgxTextHighlightDirective } from '../directives/text-highlight/text-highlight.directive';
30
import { formatCurrency, formatDate, PlatformUtil } from '../core/utils';
31
import { IgxGridSelectionService } from './selection/selection.service';
32
import { HammerGesturesManager } from '../core/touch';
33
import { GridSelectionMode } from './common/enums';
34
import { CellType, ColumnType, GridType, IgxCellTemplateContext, IGX_GRID_BASE, RowType } from './common/grid.interface';
35
import { GridColumnDataType } from '../data-operations/data-util';
36
import { IgxRowDirective } from './row.directive';
37
import { ISearchInfo } from './common/events';
38
import { IgxGridCell } from './grid-public-cell';
39
import { ISelectionNode } from './common/types';
40
import { AutoPositionStrategy, HorizontalAlignment, IgxOverlayService } from '../services/public_api';
41
import { IgxIconComponent } from '../icon/icon.component';
42
import { IgxGridCellImageAltPipe, IgxStringReplacePipe, IgxColumnFormatterPipe } from './common/pipes';
43
import { IgxTooltipDirective } from '../directives/tooltip/tooltip.directive';
44
import { IgxTooltipTargetDirective } from '../directives/tooltip/tooltip-target.directive';
45
import { IgxSuffixDirective } from '../directives/suffix/suffix.directive';
46
import { IgxPrefixDirective } from '../directives/prefix/prefix.directive';
47
import { IgxDateTimeEditorDirective } from '../directives/date-time-editor/date-time-editor.directive';
48
import { IgxTimePickerComponent } from '../time-picker/time-picker.component';
49
import { IgxDatePickerComponent } from '../date-picker/date-picker.component';
50
import { IgxCheckboxComponent } from '../checkbox/checkbox.component';
51
import { IgxTextSelectionDirective } from '../directives/text-selection/text-selection.directive';
52
import { IgxFocusDirective } from '../directives/focus/focus.directive';
53
import { IgxInputDirective } from '../directives/input/input.directive';
54
import { IgxInputGroupComponent } from '../input-group/input-group.component';
55
import { IgxChipComponent } from '../chips/chip.component';
56
import { fadeOut, scaleInCenter } from 'igniteui-angular/animations';
57

58
/**
59
 * Providing reference to `IgxGridCellComponent`:
60
 * ```typescript
61
 * @ViewChild('grid', { read: IgxGridComponent })
62
 *  public grid: IgxGridComponent;
63
 * ```
64
 * ```typescript
65
 *  let column = this.grid.columnList.first;
66
 * ```
67
 * ```typescript
68
 *  let cell = column.cells[0];
69
 * ```
70
 */
71
@Component({
72
    changeDetection: ChangeDetectionStrategy.OnPush,
73
    selector: 'igx-grid-cell',
74
    templateUrl: './cell.component.html',
75
    providers: [HammerGesturesManager],
76
    imports: [
77
        NgClass,
78
        NgTemplateOutlet,
79
        DecimalPipe,
80
        PercentPipe,
81
        CurrencyPipe,
82
        DatePipe,
83
        ReactiveFormsModule,
84
        IgxChipComponent,
85
        IgxTextHighlightDirective,
86
        IgxIconComponent,
87
        IgxInputGroupComponent,
88
        IgxInputDirective,
89
        IgxFocusDirective,
90
        IgxTextSelectionDirective,
91
        IgxCheckboxComponent,
92
        IgxDatePickerComponent,
93
        IgxTimePickerComponent,
94
        IgxDateTimeEditorDirective,
95
        IgxPrefixDirective,
96
        IgxSuffixDirective,
97
        IgxTooltipTargetDirective,
98
        IgxTooltipDirective,
99
        IgxGridCellImageAltPipe,
100
        IgxStringReplacePipe,
101
        IgxColumnFormatterPipe
102
    ]
103
})
104
export class IgxGridCellComponent implements OnInit, OnChanges, OnDestroy, CellType, AfterViewInit {
3✔
105
    private _destroy$ = new Subject<void>();
154,850✔
106
    /**
107
     * @hidden
108
     * @internal
109
     */
110
    @HostBinding('class.igx-grid__td--new')
111
    public get isEmptyAddRowCell() {
112
        return this.intRow.addRowUI && (this.value === undefined || this.value === null);
1,224,178✔
113
    }
114

115
    /**
116
     * @hidden
117
     * @internal
118
     */
119
    @ViewChildren('error', { read: IgxTooltipDirective })
120
    public errorTooltip: QueryList<IgxTooltipDirective>;
121

122
    /**
123
     * @hidden
124
     * @internal
125
     */
126
    @ViewChild('errorIcon', { read: IgxIconComponent, static: false })
127
    public errorIcon: IgxIconComponent;
128

129
    /**
130
     * Gets the default error template.
131
     * @hidden @internal
132
     */
133
    @ViewChild('defaultError', { read: TemplateRef, static: true })
134
    public defaultErrorTemplate: TemplateRef<any>;
135

136
    /**
137
     * Gets the column of the cell.
138
     * ```typescript
139
     *  let cellColumn = this.cell.column;
140
     * ```
141
     *
142
     * @memberof IgxGridCellComponent
143
     */
144
    @Input()
145
    public column: ColumnType;
146

147
    /**
148
     * @hidden
149
     * @internal
150
     */
151
    @Input()
152
    public isPlaceholder: boolean;
153

154
    /**
155
        Gets whether this cell is a merged cell.
156
     */
157
    @Input()
158
    public isMerged: boolean;
159

160
    /**
161
     * @hidden
162
     * @internal
163
     */
164
    protected get formGroup(): FormGroup {
165
        return this.grid.validation.getFormGroup(this.intRow.key);
5,950,876✔
166
    }
167

168
    /**
169
     * @hidden
170
     * @internal
171
     */
172
    @Input()
173
    public intRow: IgxRowDirective;
174

175
    /**
176
     * Gets the row of the cell.
177
     * ```typescript
178
     * let cellRow = this.cell.row;
179
     * ```
180
     *
181
     * @memberof IgxGridCellComponent
182
     */
183
    @Input()
184
    public get row(): RowType {
185
        return this.grid.createRow(this.intRow.index);
8,252✔
186
    }
187

188
    /**
189
     * Gets the data of the row of the cell.
190
     * ```typescript
191
     * let rowData = this.cell.rowData;
192
     * ```
193
     *
194
     * @memberof IgxGridCellComponent
195
     */
196
    @Input()
197
    public rowData: any;
198

199
    /**
200
     * @hidden
201
     * @internal
202
     */
203
    @Input()
204
    public columnData: any;
205

206
    /**
207
     * Sets/gets the template of the cell.
208
     * ```html
209
     * <ng-template #cellTemplate igxCell let-value>
210
     *   <div style="font-style: oblique; color:blueviolet; background:red">
211
     *       <span>{{value}}</span>
212
     *   </div>
213
     * </ng-template>
214
     * ```
215
     * ```typescript
216
     * @ViewChild('cellTemplate',{read: TemplateRef})
217
     * cellTemplate: TemplateRef<any>;
218
     * ```
219
     * ```typescript
220
     * this.cell.cellTemplate = this.cellTemplate;
221
     * ```
222
     * ```typescript
223
     * let template =  this.cell.cellTemplate;
224
     * ```
225
     *
226
     * @memberof IgxGridCellComponent
227
     */
228
    @Input()
229
    public cellTemplate: TemplateRef<any>;
230

231
    @Input()
232
    public cellValidationErrorTemplate: TemplateRef<any>;
233

234
    @Input()
235
    public pinnedIndicator: TemplateRef<any>;
236

237
    /**
238
     * Sets/gets the cell value.
239
     * ```typescript
240
     * this.cell.value = "Cell Value";
241
     * ```
242
     * ```typescript
243
     * let cellValue = this.cell.value;
244
     * ```
245
     *
246
     * @memberof IgxGridCellComponent
247
     */
248
    @Input()
249
    public value: any;
250

251
    /**
252
     * Gets the cell formatter.
253
     * ```typescript
254
     * let cellForamatter = this.cell.formatter;
255
     * ```
256
     *
257
     * @memberof IgxGridCellComponent
258
     */
259
    @Input()
260
    public formatter: (value: any, rowData?: any, columnData?: any) => any;
261

262
    /**
263
     * Gets the cell template context object.
264
     * ```typescript
265
     *  let context = this.cell.context();
266
     * ```
267
     *
268
     * @memberof IgxGridCellComponent
269
     */
270
    public get context(): IgxCellTemplateContext {
271
        const getCellType = () => this.getCellType(true);
655,773✔
272
        const ctx: IgxCellTemplateContext = {
655,773✔
273
            $implicit: this.value,
274
            additionalTemplateContext: this.column.additionalTemplateContext,
275
            get cell() {
276
                /* Turns the `cell` property from the template context object into lazy-evaluated one.
277
                 * Otherwise on each detection cycle the cell template is recreating N cell instances where
278
                 * N = number of visible cells in the grid, leading to massive performance degradation in large grids.
279
                 */
280
                return getCellType();
2,025✔
281
            }
282
        };
283
        if (this.editMode) {
655,773✔
284
            ctx.formControl = this.formControl;
2,487✔
285
        }
286
        if (this.isInvalid) {
655,773✔
287
            ctx.defaultErrorTemplate = this.defaultErrorTemplate;
429✔
288
        }
289
        return ctx;
655,773✔
290
    }
291

292
    /**
293
     * Gets the cell template.
294
     * ```typescript
295
     * let template = this.cell.template;
296
     * ```
297
     *
298
     * @memberof IgxGridCellComponent
299
     */
300
    public get template(): TemplateRef<any> {
301
        if (this.isPlaceholder) {
327,839✔
302
            return this.emptyCellTemplate;
198✔
303
        }
304
        if (this.editMode && this.formGroup) {
327,641✔
305
            const inlineEditorTemplate = this.column.inlineEditorTemplate;
1,242✔
306
            return inlineEditorTemplate ? inlineEditorTemplate : this.inlineEditorTemplate;
1,242✔
307
        }
308
        if (this.cellTemplate) {
326,399✔
309
            return this.cellTemplate;
765✔
310
        }
311
        if (this.grid.rowEditable && this.intRow.addRowUI) {
325,634✔
312
            return this.addRowCellTemplate;
1,918✔
313
        }
314
        return this.defaultCellTemplate;
323,716✔
315
    }
316

317
    /**
318
     * Gets the pinned indicator template.
319
     * ```typescript
320
     * let template = this.cell.pinnedIndicatorTemplate;
321
     * ```
322
     *
323
     * @memberof IgxGridCellComponent
324
     */
325
    public get pinnedIndicatorTemplate() {
326
        if (this.pinnedIndicator) {
327,791!
327
            return this.pinnedIndicator;
×
328
        }
329
        return this.defaultPinnedIndicator;
327,791✔
330
    }
331

332
    /**
333
     * Gets the `id` of the grid in which the cell is stored.
334
     * ```typescript
335
     * let gridId = this.cell.gridID;
336
     * ```
337
     *
338
     * @memberof IgxGridCellComponent
339
     */
340
    public get gridID(): any {
341
        return this.intRow.gridID;
305,992✔
342
    }
343

344

345
    /**
346
     * Gets the `index` of the row where the cell is stored.
347
     * ```typescript
348
     * let rowIndex = this.cell.rowIndex;
349
     * ```
350
     *
351
     * @memberof IgxGridCellComponent
352
     */
353
    @HostBinding('attr.data-rowIndex')
354
    public get rowIndex(): number {
355
        return this.intRow.index;
4,894,672✔
356
    }
357

358
    /**
359
     * Gets the `index` of the cell column.
360
     * ```typescript
361
     * let columnIndex = this.cell.columnIndex;
362
     * ```
363
     *
364
     * @memberof IgxGridCellComponent
365
     */
366
    public get columnIndex(): number {
367
        return this.column.index;
17,843✔
368
    }
369

370
    /**
371
     * Returns the column visible index.
372
     * ```typescript
373
     * let visibleColumnIndex = this.cell.visibleColumnIndex;
374
     * ```
375
     *
376
     * @memberof IgxGridCellComponent
377
     */
378
    @HostBinding('attr.data-visibleIndex')
379
    @Input()
380
    public get visibleColumnIndex() {
381
        return this.column.columnLayoutChild ? this.column.visibleIndex : this._vIndex;
4,897,349✔
382
    }
383

384
    public set visibleColumnIndex(val) {
385
        this._vIndex = val;
161,384✔
386
    }
387

388
    /**
389
     * Gets the ID of the cell.
390
     * ```typescript
391
     * let cellID = this.cell.cellID;
392
     * ```
393
     *
394
     * @memberof IgxGridCellComponent
395
     */
396
    public get cellID() {
397
        const primaryKey = this.grid.primaryKey;
462✔
398
        const rowID = primaryKey ? this.rowData[primaryKey] : this.rowData;
462✔
399
        return { rowID, columnID: this.columnIndex, rowIndex: this.rowIndex };
462✔
400
    }
401

402
    @HostBinding('attr.id')
403
    public get attrCellID() {
404
        return `${this.intRow.gridID}_${this.rowIndex}_${this.visibleColumnIndex}`;
1,222,307✔
405
    }
406

407
    @HostBinding('attr.title')
408
    public get title() {
409
        if (this.editMode || this.cellTemplate || this.errorShowing) {
1,222,320✔
410
            return '';
5,422✔
411
        }
412

413
        if (this.formatter) {
1,216,898✔
414
            return this.formatter(this.value, this.rowData, this.columnData);
22,032✔
415
        }
416

417
        const args = this.column.pipeArgs;
1,194,866✔
418
        const locale = this.grid.locale;
1,194,866✔
419

420
        switch (this.column.dataType) {
1,194,866✔
421
            case GridColumnDataType.Percent:
422
                return formatPercent(this.value, locale, args.digitsInfo);
427✔
423
            case GridColumnDataType.Currency:
424
                return formatCurrency(this.value, this.currencyCode, args.display, args.digitsInfo, locale);
20,316✔
425
            case GridColumnDataType.Date:
426
            case GridColumnDataType.DateTime:
427
            case GridColumnDataType.Time:
428
                return formatDate(this.value, args.format, locale, args.timezone);
172,012✔
429
        }
430
        return this.value;
1,002,111✔
431
    }
432

433
    @HostBinding('class.igx-grid__td--bool-true')
434
    public get booleanClass() {
435
        return this.column.dataType === 'boolean' && this.value;
1,222,307✔
436
    }
437

438
    /**
439
     * Returns a reference to the nativeElement of the cell.
440
     * ```typescript
441
     * let cellNativeElement = this.cell.nativeElement;
442
     * ```
443
     *
444
     * @memberof IgxGridCellComponent
445
     */
446
    public get nativeElement(): HTMLElement {
447
        return this.element.nativeElement;
1,346,166✔
448
    }
449

450
    /**
451
     * @hidden
452
     * @internal
453
     */
454
    @Input()
455
    public get cellSelectionMode() {
456
        return this._cellSelection;
310,744✔
457
    }
458

459
    public set cellSelectionMode(value) {
460
        if (this._cellSelection === value) {
155,218✔
461
            return;
154,246✔
462
        }
463
        this.zone.runOutsideAngular(() => {
972✔
464
            if (value === GridSelectionMode.multiple) {
972✔
465
                this.addPointerListeners(value);
60✔
466
            } else {
467
                this.removePointerListeners(this._cellSelection);
912✔
468
            }
469
        });
470
        this._cellSelection = value;
972✔
471
    }
472

473
    /**
474
     * @hidden
475
     * @internal
476
     */
477
    @Input()
478
    public set lastSearchInfo(value: ISearchInfo) {
479
        this._lastSearchInfo = value;
160,375✔
480
        this.highlightText(this._lastSearchInfo.searchText, this._lastSearchInfo.caseSensitive, this._lastSearchInfo.exactMatch);
160,375✔
481
    }
482

483
    /**
484
     * @hidden
485
     * @internal
486
     */
487
    @Input()
488
    @HostBinding('class.igx-grid__td--pinned-last')
489
    public lastPinned = false;
154,850✔
490

491
    /**
492
     * @hidden
493
     * @internal
494
     */
495
    @Input()
496
    @HostBinding('class.igx-grid__td--pinned-first')
497
    public firstPinned = false;
154,850✔
498

499
    /**
500
     * Returns whether the cell is in edit mode.
501
     */
502
    @Input({ transform: booleanAttribute })
503
    @HostBinding('class.igx-grid__td--editing')
504
    public editMode = false;
154,850✔
505

506
    /**
507
     * Sets/get the `role` property of the cell.
508
     * Default value is `"gridcell"`.
509
     * ```typescript
510
     * this.cell.role = 'grid-cell';
511
     * ```
512
     * ```typescript
513
     * let cellRole = this.cell.role;
514
     * ```
515
     *
516
     * @memberof IgxGridCellComponent
517
     */
518
    @HostBinding('attr.role')
519
    public role = 'gridcell';
154,850✔
520

521
    /**
522
     * Gets whether the cell is editable.
523
     * ```typescript
524
     * let isCellReadonly = this.cell.readonly;
525
     * ```
526
     *
527
     * @memberof IgxGridCellComponent
528
     */
529
    @HostBinding('attr.aria-readonly')
530
    public get readonly(): boolean {
531
        return !this.editable;
1,222,308✔
532
    }
533

534
    /** @hidden @internal */
535
    @HostBinding('attr.aria-describedby')
536
    public get ariaDescribeBy() {
537
        return this.isInvalid ? this.ariaErrorMessage : null;
1,223,403✔
538
    }
539

540
    /** @hidden @internal */
541
    public get ariaErrorMessage() {
542
        return this.grid.id + '_' + this.column.field + '_' + this.intRow.index + '_error';
253✔
543
    }
544

545
    /**
546
     * @hidden
547
     * @internal
548
     */
549
    @HostBinding('class.igx-grid__td--invalid')
550
    @HostBinding('attr.aria-invalid')
551
    public get isInvalid() {
552
        if (this.formGroup) {
4,653,751✔
553
            const isInvalid = this.grid.validation?.isFieldInvalid(this.formGroup, this.column?.field);
61,243✔
554
            return !this.intRow.deleted && isInvalid;
61,243✔
555
        }
556
        return false;
4,592,508✔
557
    }
558

559
    /**
560
     * @hidden
561
     * @internal
562
     */
563
    @HostBinding('class.igx-grid__td--valid')
564
    public get isValidAfterEdit() {
565
        if (this.formGroup) {
1,222,307✔
566
            const isValidAfterEdit = this.grid.validation?.isFieldValidAfterEdit(this.formGroup, this.column?.field);
11,111✔
567
            return this.editMode && isValidAfterEdit;
11,111✔
568
        }
569
        return false;
1,211,196✔
570
    }
571

572
    /**
573
     * Gets the formControl responsible for value changes and validation for this cell.
574
     */
575
    protected get formControl(): FormControl {
576
        return this.grid.validation.getFormControl(this.intRow.key, this.column.field) as FormControl;
54,536✔
577
    }
578

579
    public get gridRowSpan(): number {
580
        return this.column.gridRowSpan;
198✔
581
    }
582

583
    public get gridColumnSpan(): number {
584
        return this.column.gridColumnSpan;
198✔
585
    }
586

587
    public get rowEnd(): number {
588
        return this.column.rowEnd;
×
589
    }
590

591
    public get colEnd(): number {
592
        return this.column.colEnd;
×
593
    }
594

595
    public get rowStart(): number {
596
        return this.column.rowStart;
×
597
    }
598

599
    public get colStart(): number {
600
        return this.column.colStart;
×
601
    }
602

603
    /**
604
     * Gets the width of the cell.
605
     * ```typescript
606
     * let cellWidth = this.cell.width;
607
     * ```
608
     *
609
     * @memberof IgxGridCellComponent
610
     */
611
    @Input()
612
    public width = '';
154,850✔
613

614
    /**
615
     * @hidden
616
     */
617
    @Input()
618
    @HostBinding('class.igx-grid__td--active')
619
    public active = false;
154,850✔
620

621
    @HostBinding('attr.aria-selected')
622
    public get ariaSelected() {
623
        return this.selected || this.column.selected || this.intRow.selected;
1,222,307✔
624
    }
625

626
    /**
627
     * Gets whether the cell is selected.
628
     * ```typescript
629
     * let isSelected = this.cell.selected;
630
     * ```
631
     *
632
     * @memberof IgxGridCellComponent
633
     */
634
    @HostBinding('class.igx-grid__td--selected')
635
    public get selected() {
636
        return this.selectionService.selected(this.selectionNode);
2,446,699✔
637
    }
638

639
    /**
640
     * Selects/deselects the cell.
641
     * ```typescript
642
     * this.cell.selected = true.
643
     * ```
644
     *
645
     * @memberof IgxGridCellComponent
646
     */
647
    public set selected(val: boolean) {
648
        const node = this.selectionNode;
1✔
649
        if (val) {
1!
650
            this.selectionService.add(node);
1✔
651
        } else {
652
            this.selectionService.remove(node);
×
653
        }
654
        this.grid.notifyChanges();
1✔
655
    }
656

657
    /**
658
     * Gets whether the cell column is selected.
659
     * ```typescript
660
     * let isCellColumnSelected = this.cell.columnSelected;
661
     * ```
662
     *
663
     * @memberof IgxGridCellComponent
664
     */
665
    @HostBinding('class.igx-grid__td--column-selected')
666
    public get columnSelected() {
667
        return this.selectionService.isColumnSelected(this.column.field);
1,222,307✔
668
    }
669

670
    /**
671
     * Sets the current edit value while a cell is in edit mode.
672
     * Only for cell editing mode.
673
     * ```typescript
674
     * this.cell.editValue = value;
675
     * ```
676
     *
677
     * @memberof IgxGridCellComponent
678
     */
679
    public set editValue(value) {
680
        if (this.grid.crudService.cellInEditMode) {
44✔
681
            this.grid.crudService.cell.editValue = value;
44✔
682
        }
683
    }
684

685
    /**
686
     * Gets the current edit value while a cell is in edit mode.
687
     * Only for cell editing mode.
688
     * ```typescript
689
     * let editValue = this.cell.editValue;
690
     * ```
691
     *
692
     * @memberof IgxGridCellComponent
693
     */
694
    public get editValue() {
695
        if (this.grid.crudService.cellInEditMode) {
163✔
696
            return this.grid.crudService.cell.editValue;
163✔
697
        }
698
    }
699

700
    /**
701
     * Returns whether the cell is editable.
702
     */
703
    public get editable(): boolean {
704
        return this.column.editable && !this.intRow.disabled;
1,223,586✔
705
    }
706

707
    /**
708
     * @hidden
709
     */
710
    @Input()
711
    @HostBinding('class.igx-grid__td--row-pinned-first')
712
    public displayPinnedChip = false;
154,850✔
713

714
    @HostBinding('style.min-height.px')
715
    protected get minHeight() {
716
        if ((this.grid as any).isCustomSetRowHeight) {
1,222,307✔
717
            return this.grid.renderedRowHeight;
464✔
718
        }
719
    }
720

721
    @ViewChild('defaultCell', { read: TemplateRef, static: true })
722
    protected defaultCellTemplate: TemplateRef<any>;
723

724
    @ViewChild('emptyCell', { read: TemplateRef, static: true })
725
    protected emptyCellTemplate: TemplateRef<any>;
726

727
    @ViewChild('defaultPinnedIndicator', { read: TemplateRef, static: true })
728
    protected defaultPinnedIndicator: TemplateRef<any>;
729

730
    @ViewChild('inlineEditor', { read: TemplateRef, static: true })
731
    protected inlineEditorTemplate: TemplateRef<any>;
732

733
    @ViewChild('addRowCell', { read: TemplateRef, static: true })
734
    protected addRowCellTemplate: TemplateRef<any>;
735

736
    @ViewChild(IgxTextHighlightDirective, { read: IgxTextHighlightDirective })
737
    protected set highlight(value: IgxTextHighlightDirective) {
738
        this._highlight = value;
157,003✔
739

740
        if (this._highlight && this.grid.lastSearchInfo.searchText) {
157,003✔
741
            this._highlight.highlight(this.grid.lastSearchInfo.searchText,
400✔
742
                this.grid.lastSearchInfo.caseSensitive,
743
                this.grid.lastSearchInfo.exactMatch);
744
            this._highlight.activateIfNecessary();
400✔
745
        }
746
    }
747

748
    protected get highlight() {
749
        return this._highlight;
356,462✔
750
    }
751

752
    protected get selectionNode(): ISelectionNode {
753
        return {
2,448,604✔
754
            row: this.rowIndex,
755
            column: this.column.columnLayoutChild ? this.column.parent.visibleIndex : this.visibleColumnIndex,
2,448,604✔
756
            layout: this.column.columnLayoutChild ? {
2,448,604✔
757
                rowStart: this.column.rowStart,
758
                colStart: this.column.colStart,
759
                rowEnd: this.column.rowEnd,
760
                colEnd: this.column.colEnd,
761
                columnVisibleIndex: this.visibleColumnIndex
762
            } : null
763
        };
764
    }
765

766
    /**
767
     * Sets/gets the highlight class of the cell.
768
     * Default value is `"igx-highlight"`.
769
     * ```typescript
770
     * let highlightClass = this.cell.highlightClass;
771
     * ```
772
     * ```typescript
773
     * this.cell.highlightClass = 'igx-cell-highlight';
774
     * ```
775
     *
776
     * @memberof IgxGridCellComponent
777
     */
778
    public highlightClass = 'igx-highlight';
154,850✔
779

780
    /**
781
     * Sets/gets the active highlight class class of the cell.
782
     * Default value is `"igx-highlight__active"`.
783
     * ```typescript
784
     * let activeHighlightClass = this.cell.activeHighlightClass;
785
     * ```
786
     * ```typescript
787
     * this.cell.activeHighlightClass = 'igx-cell-highlight_active';
788
     * ```
789
     *
790
     * @memberof IgxGridCellComponent
791
     */
792
    public activeHighlightClass = 'igx-highlight__active';
154,850✔
793

794
    /** @hidden @internal */
795
    public get step(): number {
796
        const digitsInfo = this.column.pipeArgs.digitsInfo;
218✔
797
        if (!digitsInfo) {
218!
798
            return 1;
×
799
        }
800
        const step = +digitsInfo.substr(digitsInfo.indexOf('.') + 1, 1);
218✔
801
        return 1 / (Math.pow(10, step));
218✔
802
    }
803

804
    /** @hidden @internal */
805
    public get currencyCode(): string {
806
        return this.column.pipeArgs.currencyCode ?
57,180✔
807
            this.column.pipeArgs.currencyCode : getLocaleCurrencyCode(this.grid.locale);
808
    }
809

810
    /** @hidden @internal */
811
    public get currencyCodeSymbol(): string {
812
        return getCurrencySymbol(this.currencyCode, 'wide', this.grid.locale);
4✔
813
    }
814

815
    protected _lastSearchInfo: ISearchInfo;
816
    private _highlight: IgxTextHighlightDirective;
817
    private _cellSelection: GridSelectionMode = GridSelectionMode.multiple;
154,850✔
818
    private _vIndex = -1;
154,850✔
819

820
    constructor(
821
        protected selectionService: IgxGridSelectionService,
154,850✔
822
        @Inject(IGX_GRID_BASE) public grid: GridType,
154,850✔
823
        @Inject(IgxOverlayService) protected overlayService: IgxOverlayService,
154,850✔
824
        public cdr: ChangeDetectorRef,
154,850✔
825
        private element: ElementRef<HTMLElement>,
154,850✔
826
        protected zone: NgZone,
154,850✔
827
        private touchManager: HammerGesturesManager,
154,850✔
828
        protected platformUtil: PlatformUtil
154,850✔
829
    ) { }
830

831
    /**
832
     * @hidden
833
     * @internal
834
     */
835
    @HostListener('dblclick', ['$event'])
836
    public onDoubleClick = (event: MouseEvent) => {
154,850✔
837
        if (event.type === 'doubletap') {
153✔
838
            // prevent double-tap to zoom on iOS
839
            event.preventDefault();
1✔
840
        }
841
        if (this.editable && !this.editMode && !this.intRow.deleted && !this.grid.crudService.rowEditingBlocked) {
153✔
842
            this.grid.crudService.enterEditMode(this, event as Event);
147✔
843
        }
844

845
        this.grid.doubleClick.emit({
153✔
846
            cell: this.getCellType(),
847
            event
848
        });
849
    };
850

851
    /**
852
     * @hidden
853
     * @internal
854
     */
855
    @HostListener('click', ['$event'])
856
    public onClick(event: MouseEvent) {
857
        this.grid.cellClick.emit({
265✔
858
            cell: this.getCellType(),
859
            event
860
        });
861
    }
862

863
    /**
864
     * @hidden
865
     * @internal
866
     */
867
    public ngOnInit() {
868
        this.zone.runOutsideAngular(() => {
154,850✔
869
            this.nativeElement.addEventListener('pointerdown', this.pointerdown);
154,850✔
870
            this.addPointerListeners(this.cellSelectionMode);
154,850✔
871
        });
872
        if (this.platformUtil.isIOS) {
154,850✔
873
            this.touchManager.addEventListener(this.nativeElement, 'doubletap', this.onDoubleClick, {
40✔
874
                cssProps: {} /* don't disable user-select, etc */
875
            });
876
        }
877

878
    }
879

880
    public ngAfterViewInit() {
881
        this.errorTooltip.changes.pipe(takeUntil(this._destroy$)).subscribe(() => {
154,850✔
882
            if (this.errorTooltip.length > 0 && this.active) {
44✔
883
                // error ocurred
884
                this.cdr.detectChanges();
25✔
885
                this.openErrorTooltip();
25✔
886
            }
887
        });
888
    }
889

890
    /**
891
     * @hidden
892
     * @internal
893
     */
894
    public errorShowing = false;
154,850✔
895

896
    private openErrorTooltip() {
897
        const tooltip = this.errorTooltip.first;
26✔
898
        tooltip.open(
26✔
899
            {
900
                target: this.errorIcon.el.nativeElement,
901
                closeOnOutsideClick: true,
902
                excludeFromOutsideClick: [this.nativeElement],
903
                closeOnEscape: false,
904
                outlet: this.grid.outlet,
905
                modal: false,
906
                positionStrategy: new AutoPositionStrategy({
907
                    horizontalStartPoint: HorizontalAlignment.Center,
908
                    horizontalDirection: HorizontalAlignment.Center,
909
                    openAnimation: useAnimation(scaleInCenter, { params: { duration: '150ms' } }),
910
                    closeAnimation: useAnimation(fadeOut, { params: { duration: '75ms' } })
911
                })
912
            }
913
        );
914
    }
915

916
    /**
917
     * @hidden
918
     * @internal
919
     */
920
    public ngOnDestroy() {
921
        this.zone.runOutsideAngular(() => {
153,505✔
922
            this.nativeElement.removeEventListener('pointerdown', this.pointerdown);
153,505✔
923
            this.removePointerListeners(this.cellSelectionMode);
153,505✔
924
        });
925
        this.touchManager.destroy();
153,505✔
926
        this._destroy$.next();
153,505✔
927
        this._destroy$.complete();
153,505✔
928
    }
929

930
    /**
931
     * @hidden
932
     * @internal
933
     */
934
    public ngOnChanges(changes: SimpleChanges): void {
935
        if (changes.editMode && changes.editMode.currentValue && this.formControl) {
293,972✔
936
            // ensure when values change, form control is forced to be marked as touche.
937
            this.formControl.valueChanges.pipe(takeWhile(() => this.editMode)).subscribe(() => this.formControl.markAsTouched());
402✔
938
            // while in edit mode subscribe to value changes on the current form control and set to editValue
939
            this.formControl.statusChanges.pipe(takeWhile(() => this.editMode)).subscribe(status => {
402✔
940
                if (status === 'INVALID' && this.errorTooltip.length > 0) {
192!
941
                    this.cdr.detectChanges();
×
942
                    const tooltip = this.errorTooltip.first;
×
943
                    this.resizeAndRepositionOverlayById(tooltip.overlayId, this.errorTooltip.first.element.offsetWidth);
×
944
                }
945
            });
946
        }
947
        if (changes.value && !changes.value.firstChange) {
293,972✔
948
            if (this.highlight) {
47,880✔
949
                this.highlight.lastSearchInfo.searchText = this.grid.lastSearchInfo.searchText;
45,461✔
950
                this.highlight.lastSearchInfo.caseSensitive = this.grid.lastSearchInfo.caseSensitive;
45,461✔
951
                this.highlight.lastSearchInfo.exactMatch = this.grid.lastSearchInfo.exactMatch;
45,461✔
952
            }
953
            const isInEdit = this.grid.rowEditable ? this.row.inEditMode : this.editMode;
47,880✔
954
            if (this.formControl && this.formControl.value !== changes.value.currentValue && !isInEdit) {
47,880✔
955
                this.formControl.setValue(changes.value.currentValue);
131✔
956
            }
957
        }
958
    }
959

960

961

962
    /**
963
     * @hidden @internal
964
     */
965
    private resizeAndRepositionOverlayById(overlayId: string, newSize: number) {
966
        const overlay = this.overlayService.getOverlayById(overlayId);
×
967
        if (!overlay) return;
×
968
        overlay.initialSize.width = newSize;
×
969
        overlay.elementRef.nativeElement.parentElement.style.width = newSize + 'px';
×
970
        this.overlayService.reposition(overlayId);
×
971
    }
972

973
    /**
974
     * Starts/ends edit mode for the cell.
975
     *
976
     * ```typescript
977
     * cell.setEditMode(true);
978
     * ```
979
     */
980
    public setEditMode(value: boolean): void {
981
        if (this.intRow.deleted) {
30!
982
            return;
×
983
        }
984
        if (this.editable && value) {
30✔
985
            if (this.grid.crudService.cellInEditMode) {
26✔
986
                this.grid.gridAPI.update_cell(this.grid.crudService.cell);
5✔
987
                this.grid.crudService.endCellEdit();
5✔
988
            }
989
            this.grid.crudService.enterEditMode(this);
26✔
990
        } else {
991
            this.grid.crudService.endCellEdit();
4✔
992
        }
993
        this.grid.notifyChanges();
30✔
994
    }
995

996
    /**
997
     * Sets new value to the cell.
998
     * ```typescript
999
     * this.cell.update('New Value');
1000
     * ```
1001
     *
1002
     * @memberof IgxGridCellComponent
1003
     */
1004
    // TODO: Refactor
1005
    public update(val: any) {
1006
        if (this.intRow.deleted) {
34!
1007
            return;
×
1008
        }
1009

1010
        let cell = this.grid.crudService.cell;
34✔
1011
        if (!cell) {
34✔
1012
            cell = this.grid.crudService.createCell(this);
14✔
1013
        }
1014
        cell.editValue = val;
34✔
1015
        this.grid.gridAPI.update_cell(cell);
34✔
1016
        this.grid.crudService.endCellEdit();
34✔
1017
        this.cdr.markForCheck();
34✔
1018
    }
1019

1020
    /**
1021
     *
1022
     * @hidden
1023
     * @internal
1024
     */
1025
    public pointerdown = (event: PointerEvent) => {
154,850✔
1026

1027
        if (this.isMerged) {
433✔
1028
            // need an approximation of where in the cell the user clicked to get actual index to be activated.
1029
            const scrollOffset = this.grid.verticalScrollContainer.scrollPosition + (event.y - this.grid.tbody.nativeElement.getBoundingClientRect().y);
5✔
1030
            const targetRowIndex = this.grid.verticalScrollContainer.getIndexAtScroll(scrollOffset);
5✔
1031
            if (targetRowIndex != this.rowIndex) {
5!
NEW
1032
                const row = this.grid.rowList.toArray().find(x => x.index === targetRowIndex);
×
NEW
1033
                const actualTarget = row.cells.find(x => x.column === this.column);
×
NEW
1034
                actualTarget.pointerdown(event);
×
NEW
1035
                return;
×
1036
            }
1037
        }
1038

1039
        if (this.cellSelectionMode !== GridSelectionMode.multiple) {
433✔
1040
            this.activate(event);
23✔
1041
            return;
23✔
1042
        }
1043
        if (!this.platformUtil.isLeftClick(event)) {
410✔
1044
            event.preventDefault();
4✔
1045
            this.grid.navigation.setActiveNode({ rowIndex: this.rowIndex, colIndex: this.visibleColumnIndex });
4✔
1046
            this.selectionService.addKeyboardRange();
4✔
1047
            this.selectionService.initKeyboardState();
4✔
1048
            this.selectionService.primaryButton = false;
4✔
1049
            // Ensure RMB Click on edited cell does not end cell editing
1050
            if (!this.selected) {
4✔
1051
                this.grid.crudService.updateCell(true, event);
2✔
1052
            }
1053
            return;
4✔
1054
        } else {
1055
            this.selectionService.primaryButton = true;
406✔
1056
        }
1057
        this.selectionService.pointerDown(this.selectionNode, event.shiftKey, event.ctrlKey);
406✔
1058
        this.activate(event);
406✔
1059
    };
1060

1061
    /**
1062
     *
1063
     * @hidden
1064
     * @internal
1065
     */
1066
    public pointerenter = (event: PointerEvent) => {
154,850✔
1067
        const isHierarchicalGrid = this.grid.type === 'hierarchical';
119✔
1068
        if (isHierarchicalGrid && (!this.grid.navigation?.activeNode?.gridID || this.grid.navigation.activeNode.gridID !== this.gridID)) {
119✔
1069
            return;
3✔
1070
        }
1071
        const dragMode = this.selectionService.pointerEnter(this.selectionNode, event);
116✔
1072
        if (dragMode) {
116✔
1073
            this.grid.cdr.detectChanges();
115✔
1074
        }
1075
    };
1076

1077
    /**
1078
     * @hidden
1079
     * @internal
1080
     */
1081
    public focusout = () => {
154,850✔
1082
        this.closeErrorTooltip();
49✔
1083
    }
1084

1085
    private closeErrorTooltip() {
1086
        const tooltip = this.errorTooltip.first;
49✔
1087
        if (tooltip) {
49!
1088
            tooltip.close();
×
1089
        }
1090
    }
1091

1092
    /**
1093
     * @hidden
1094
     * @internal
1095
     */
1096
    public pointerup = (event: PointerEvent) => {
154,850✔
1097
        const isHierarchicalGrid = this.grid.type === 'hierarchical';
407✔
1098
        if (!this.platformUtil.isLeftClick(event) || (isHierarchicalGrid && (!this.grid.navigation?.activeNode?.gridID ||
407✔
1099
            this.grid.navigation.activeNode.gridID !== this.gridID))) {
1100
            return;
4✔
1101
        }
1102
        if (this.selectionService.pointerUp(this.selectionNode, this.grid.rangeSelected)) {
403✔
1103
            this.grid.cdr.detectChanges();
50✔
1104
        }
1105
    };
1106

1107
    /**
1108
     * @hidden
1109
     * @internal
1110
     */
1111
    public activate(event: FocusEvent | KeyboardEvent) {
1112
        const node = this.selectionNode;
979✔
1113
        let shouldEmitSelection = !this.selectionService.isActiveNode(node);
979✔
1114

1115
        if (this.selectionService.primaryButton) {
979!
1116
            const currentActive = this.selectionService.activeElement;
979✔
1117
            if (this.cellSelectionMode === GridSelectionMode.single && (event as any)?.ctrlKey && this.selected) {
979✔
1118
                this.selectionService.activeElement = null;
1✔
1119
                shouldEmitSelection = true;
1✔
1120
            } else {
1121
                this.selectionService.activeElement = node;
978✔
1122
            }
1123
            const cancel = this._updateCRUDStatus(event);
979✔
1124
            if (cancel) {
979✔
1125
                this.selectionService.activeElement = currentActive;
2✔
1126
                return;
2✔
1127
            }
1128

1129
            const activeElement = this.selectionService.activeElement;
977✔
1130
            const row = activeElement ? this.grid.gridAPI.get_row_by_index(activeElement.row) : null;
977✔
1131
            if (this.grid.crudService.rowEditingBlocked && row && this.intRow.key !== row.key) {
977!
1132
                return;
×
1133
            }
1134

1135
        } else {
1136
            this.selectionService.activeElement = null;
×
1137
            if (this.grid.crudService.cellInEditMode && !this.editMode) {
×
1138
                this.grid.crudService.updateCell(true, event);
×
1139
            }
1140
        }
1141

1142
        this.grid.navigation.setActiveNode({ row: this.rowIndex, column: this.visibleColumnIndex });
977✔
1143

1144
        const isTargetErrorIcon = event && event.target && event.target === this.errorIcon?.el.nativeElement
977✔
1145
        if (this.isInvalid && !isTargetErrorIcon) {
977✔
1146
            this.cdr.detectChanges();
1✔
1147
            this.openErrorTooltip();
1✔
1148
            this.grid.activeNodeChange.pipe(first()).subscribe(() => {
1✔
1149
                this.closeErrorTooltip();
×
1150
            });
1151
        }
1152
        this.selectionService.primaryButton = true;
977✔
1153
        if (this.cellSelectionMode === GridSelectionMode.multiple && this.selectionService.activeElement) {
977✔
1154
            if (this.selectionService.isInMap(this.selectionService.activeElement) && (event as any)?.ctrlKey && !(event as any)?.shiftKey) {
945✔
1155
                this.selectionService.remove(this.selectionService.activeElement);
3✔
1156
                shouldEmitSelection = true;
3✔
1157
            } else {
1158
                this.selectionService.add(this.selectionService.activeElement, false); // pointer events handle range generation
942✔
1159
                this.selectionService.keyboardStateOnFocus(node, this.grid.rangeSelected, this.nativeElement);
942✔
1160
            }
1161
        }
1162
        if (this.grid.isCellSelectable && shouldEmitSelection) {
977✔
1163
            this.zone.run(() => this.grid.selected.emit({ cell: this.getCellType(), event }));
950✔
1164
        }
1165
    }
1166

1167
    /**
1168
     * If the provided string matches the text in the cell, the text gets highlighted.
1169
     * ```typescript
1170
     * this.cell.highlightText('Cell Value', true);
1171
     * ```
1172
     *
1173
     * @memberof IgxGridCellComponent
1174
     */
1175
    public highlightText(text: string, caseSensitive?: boolean, exactMatch?: boolean): number {
1176
        return this.highlight && this.column.searchable ? this.highlight.highlight(text, caseSensitive, exactMatch) : 0;
163,562✔
1177
    }
1178

1179
    /**
1180
     * Clears the highlight of the text in the cell.
1181
     * ```typescript
1182
     * this.cell.clearHighLight();
1183
     * ```
1184
     *
1185
     * @memberof IgxGridCellComponent
1186
     */
1187
    public clearHighlight() {
1188
        if (this.highlight && this.column.searchable) {
40✔
1189
            this.highlight.clearHighlight();
40✔
1190
        }
1191
    }
1192

1193
    /**
1194
     * @hidden
1195
     * @internal
1196
     */
1197
    public calculateSizeToFit(range: any): number {
1198
        return this.platformUtil.getNodeSizeViaRange(range, this.nativeElement);
214✔
1199
    }
1200

1201
    /**
1202
     * @hidden
1203
     * @internal
1204
     */
1205
    public get searchMetadata() {
1206
        const meta = new Map<string, any>();
305,849✔
1207
        meta.set('pinned', this.grid.isRecordPinnedByViewIndex(this.intRow.index));
305,849✔
1208
        return meta;
305,849✔
1209
    }
1210

1211
    /**
1212
     * @hidden
1213
     * @internal
1214
     */
1215
    private _updateCRUDStatus(event?: Event) {
1216
        if (this.editMode) {
979✔
1217
            return;
48✔
1218
        }
1219

1220
        let editableArgs;
1221
        const crud = this.grid.crudService;
931✔
1222
        const editableCell = this.grid.crudService.cell;
931✔
1223
        const editMode = !!(crud.row || crud.cell);
931✔
1224

1225
        if (this.editable && editMode && !this.intRow.deleted) {
931✔
1226
            if (editableCell) {
88✔
1227
                editableArgs = this.grid.crudService.updateCell(false, event);
77✔
1228

1229
                /* This check is related with the following issue #6517:
1230
                 * when edit cell that belongs to a column which is sorted and press tab,
1231
                 * the next cell in edit mode is with wrong value /its context is not updated/;
1232
                 * So we reapply sorting before the next cell enters edit mode.
1233
                 * Also we need to keep the notifyChanges below, because of the current
1234
                 * change detection cycle when we have editing with enabled transactions
1235
                 */
1236
                if (this.grid.sortingExpressions.length && this.grid.sortingExpressions.indexOf(editableCell.column.field)) {
77!
1237
                    this.grid.cdr.detectChanges();
×
1238
                }
1239

1240
                if (editableArgs && editableArgs.cancel) {
77✔
1241
                    return true;
2✔
1242
                }
1243

1244
                crud.exitCellEdit(event);
75✔
1245
            }
1246
            this.grid.tbody.nativeElement.focus({ preventScroll: true });
86✔
1247
            this.grid.notifyChanges();
86✔
1248
            crud.enterEditMode(this, event);
86✔
1249
            return false;
86✔
1250
        }
1251

1252
        if (editableCell && crud.sameRow(this.cellID.rowID)) {
843✔
1253
            this.grid.crudService.updateCell(true, event);
1✔
1254
        } else if (editMode && !crud.sameRow(this.cellID.rowID)) {
842✔
1255
            this.grid.crudService.endEdit(true, event);
3✔
1256
        }
1257
    }
1258

1259
    private addPointerListeners(selection) {
1260
        if (selection !== GridSelectionMode.multiple) {
154,910✔
1261
            return;
604✔
1262
        }
1263
        this.nativeElement.addEventListener('pointerenter', this.pointerenter);
154,306✔
1264
        this.nativeElement.addEventListener('pointerup', this.pointerup);
154,306✔
1265
        this.nativeElement.addEventListener('focusout', this.focusout);
154,306✔
1266
    }
1267

1268
    private removePointerListeners(selection) {
1269
        if (selection !== GridSelectionMode.multiple) {
154,417✔
1270
            return;
852✔
1271
        }
1272
        this.nativeElement.removeEventListener('pointerenter', this.pointerenter);
153,565✔
1273
        this.nativeElement.removeEventListener('pointerup', this.pointerup);
153,565✔
1274
        this.nativeElement.removeEventListener('focusout', this.focusout);
153,565✔
1275
    }
1276

1277
    private getCellType(useRow?: boolean): CellType {
1278
        const rowID = useRow ? this.grid.createRow(this.intRow.index, this.intRow.data) : this.intRow.index;
3,393✔
1279
        return new IgxGridCell(this.grid, rowID, this.column);
3,393✔
1280
    }
1281
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc