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

IgniteUI / igniteui-angular / 16463715188

23 Jul 2025 06:55AM UTC coverage: 91.425% (+0.007%) from 91.418%
16463715188

Pull #16054

github

web-flow
Merge 8b1a427a6 into ea24646f7
Pull Request #16054: Handle validation for fields with '.' - 20.0.x

13440 of 15782 branches covered (85.16%)

18 of 18 new or added lines in 2 files covered. (100.0%)

18 existing lines in 1 file now uncovered.

27157 of 29704 relevant lines covered (91.43%)

37276.24 hits per line

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

91.86
/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>();
152,378✔
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,214,584✔
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
    /**
149
     * @hidden
150
     * @internal
151
     */
152
    protected get formGroup(): FormGroup {
153
        return this.grid.validation.getFormGroup(this.intRow.key);
5,832,388✔
154
    }
155

156
    /**
157
     * @hidden
158
     * @internal
159
     */
160
    @Input()
161
    public intRow: IgxRowDirective;
162

163
    /**
164
     * Gets the row of the cell.
165
     * ```typescript
166
     * let cellRow = this.cell.row;
167
     * ```
168
     *
169
     * @memberof IgxGridCellComponent
170
     */
171
    @Input()
172
    public get row(): RowType {
173
        return this.grid.createRow(this.intRow.index);
8,224✔
174
    }
175

176
    /**
177
     * Gets the data of the row of the cell.
178
     * ```typescript
179
     * let rowData = this.cell.rowData;
180
     * ```
181
     *
182
     * @memberof IgxGridCellComponent
183
     */
184
    @Input()
185
    public rowData: any;
186

187
    /**
188
     * @hidden
189
     * @internal
190
     */
191
    @Input()
192
    public columnData: any;
193

194
    /**
195
     * Sets/gets the template of the cell.
196
     * ```html
197
     * <ng-template #cellTemplate igxCell let-value>
198
     *   <div style="font-style: oblique; color:blueviolet; background:red">
199
     *       <span>{{value}}</span>
200
     *   </div>
201
     * </ng-template>
202
     * ```
203
     * ```typescript
204
     * @ViewChild('cellTemplate',{read: TemplateRef})
205
     * cellTemplate: TemplateRef<any>;
206
     * ```
207
     * ```typescript
208
     * this.cell.cellTemplate = this.cellTemplate;
209
     * ```
210
     * ```typescript
211
     * let template =  this.cell.cellTemplate;
212
     * ```
213
     *
214
     * @memberof IgxGridCellComponent
215
     */
216
    @Input()
217
    public cellTemplate: TemplateRef<any>;
218

219
    @Input()
220
    public cellValidationErrorTemplate: TemplateRef<any>;
221

222
    @Input()
223
    public pinnedIndicator: TemplateRef<any>;
224

225
    /**
226
     * Sets/gets the cell value.
227
     * ```typescript
228
     * this.cell.value = "Cell Value";
229
     * ```
230
     * ```typescript
231
     * let cellValue = this.cell.value;
232
     * ```
233
     *
234
     * @memberof IgxGridCellComponent
235
     */
236
    @Input()
237
    public value: any;
238

239
    /**
240
     * Gets the cell formatter.
241
     * ```typescript
242
     * let cellForamatter = this.cell.formatter;
243
     * ```
244
     *
245
     * @memberof IgxGridCellComponent
246
     */
247
    @Input()
248
    public formatter: (value: any, rowData?: any, columnData?: any) => any;
249

250
    /**
251
     * Gets the cell template context object.
252
     * ```typescript
253
     *  let context = this.cell.context();
254
     * ```
255
     *
256
     * @memberof IgxGridCellComponent
257
     */
258
    public get context(): IgxCellTemplateContext {
259
        const getCellType = () => this.getCellType(true);
648,577✔
260
        const ctx: IgxCellTemplateContext = {
648,577✔
261
            $implicit: this.value,
262
            additionalTemplateContext: this.column.additionalTemplateContext,
263
            get cell() {
264
                /* Turns the `cell` property from the template context object into lazy-evaluated one.
265
                 * Otherwise on each detection cycle the cell template is recreating N cell instances where
266
                 * N = number of visible cells in the grid, leading to massive performance degradation in large grids.
267
                 */
268
                return getCellType();
1,998✔
269
            }
270
        };
271
        if (this.editMode) {
648,577✔
272
            ctx.formControl = this.formControl;
2,473✔
273
        }
274
        if (this.isInvalid) {
648,577✔
275
            ctx.defaultErrorTemplate = this.defaultErrorTemplate;
429✔
276
        }
277
        return ctx;
648,577✔
278
    }
279

280
    /**
281
     * Gets the cell template.
282
     * ```typescript
283
     * let template = this.cell.template;
284
     * ```
285
     *
286
     * @memberof IgxGridCellComponent
287
     */
288
    public get template(): TemplateRef<any> {
289
        if (this.editMode && this.formGroup) {
324,241✔
290
            const inlineEditorTemplate = this.column.inlineEditorTemplate;
1,235✔
291
            return inlineEditorTemplate ? inlineEditorTemplate : this.inlineEditorTemplate;
1,235✔
292
        }
293
        if (this.cellTemplate) {
323,006✔
294
            return this.cellTemplate;
688✔
295
        }
296
        if (this.grid.rowEditable && this.intRow.addRowUI) {
322,318✔
297
            return this.addRowCellTemplate;
1,918✔
298
        }
299
        return this.defaultCellTemplate;
320,400✔
300
    }
301

302
    /**
303
     * Gets the pinned indicator template.
304
     * ```typescript
305
     * let template = this.cell.pinnedIndicatorTemplate;
306
     * ```
307
     *
308
     * @memberof IgxGridCellComponent
309
     */
310
    public get pinnedIndicatorTemplate() {
311
        if (this.pinnedIndicator) {
324,193!
312
            return this.pinnedIndicator;
×
313
        }
314
        return this.defaultPinnedIndicator;
324,193✔
315
    }
316

317
    /**
318
     * Gets the `id` of the grid in which the cell is stored.
319
     * ```typescript
320
     * let gridId = this.cell.gridID;
321
     * ```
322
     *
323
     * @memberof IgxGridCellComponent
324
     */
325
    public get gridID(): any {
326
        return this.intRow.gridID;
1,517,055✔
327
    }
328

329

330
    /**
331
     * Gets the `index` of the row where the cell is stored.
332
     * ```typescript
333
     * let rowIndex = this.cell.rowIndex;
334
     * ```
335
     *
336
     * @memberof IgxGridCellComponent
337
     */
338
    @HostBinding('attr.data-rowIndex')
339
    public get rowIndex(): number {
340
        return this.intRow.index;
4,856,256✔
341
    }
342

343
    /**
344
     * Gets the `index` of the cell column.
345
     * ```typescript
346
     * let columnIndex = this.cell.columnIndex;
347
     * ```
348
     *
349
     * @memberof IgxGridCellComponent
350
     */
351
    public get columnIndex(): number {
352
        return this.column.index;
17,841✔
353
    }
354

355
    /**
356
     * Returns the column visible index.
357
     * ```typescript
358
     * let visibleColumnIndex = this.cell.visibleColumnIndex;
359
     * ```
360
     *
361
     * @memberof IgxGridCellComponent
362
     */
363
    @HostBinding('attr.data-visibleIndex')
364
    @Input()
365
    public get visibleColumnIndex() {
366
        return this.column.columnLayoutChild ? this.column.visibleIndex : this._vIndex;
4,858,936✔
367
    }
368

369
    public set visibleColumnIndex(val) {
370
        this._vIndex = val;
158,852✔
371
    }
372

373
    /**
374
     * Gets the ID of the cell.
375
     * ```typescript
376
     * let cellID = this.cell.cellID;
377
     * ```
378
     *
379
     * @memberof IgxGridCellComponent
380
     */
381
    public get cellID() {
382
        const primaryKey = this.grid.primaryKey;
460✔
383
        const rowID = primaryKey ? this.rowData[primaryKey] : this.rowData;
460✔
384
        return { rowID, columnID: this.columnIndex, rowIndex: this.rowIndex };
460✔
385
    }
386

387
    @HostBinding('attr.id')
388
    public get attrCellID() {
389
        return `${this.intRow.gridID}_${this.rowIndex}_${this.visibleColumnIndex}`;
1,212,713✔
390
    }
391

392
    @HostBinding('attr.title')
393
    public get title() {
394
        if (this.editMode || this.cellTemplate || this.errorShowing) {
1,212,726✔
395
            return '';
5,175✔
396
        }
397

398
        if (this.formatter) {
1,207,551✔
399
            return this.formatter(this.value, this.rowData, this.columnData);
22,032✔
400
        }
401

402
        const args = this.column.pipeArgs;
1,185,519✔
403
        const locale = this.grid.locale;
1,185,519✔
404

405
        switch (this.column.dataType) {
1,185,519✔
406
            case GridColumnDataType.Percent:
407
                return formatPercent(this.value, locale, args.digitsInfo);
427✔
408
            case GridColumnDataType.Currency:
409
                return formatCurrency(this.value, this.currencyCode, args.display, args.digitsInfo, locale);
20,316✔
410
            case GridColumnDataType.Date:
411
            case GridColumnDataType.DateTime:
412
            case GridColumnDataType.Time:
413
                return formatDate(this.value, args.format, locale, args.timezone);
170,436✔
414
        }
415
        return this.value;
994,340✔
416
    }
417

418
    @HostBinding('class.igx-grid__td--bool-true')
419
    public get booleanClass() {
420
        return this.column.dataType === 'boolean' && this.value;
1,212,713✔
421
    }
422

423
    /**
424
     * Returns a reference to the nativeElement of the cell.
425
     * ```typescript
426
     * let cellNativeElement = this.cell.nativeElement;
427
     * ```
428
     *
429
     * @memberof IgxGridCellComponent
430
     */
431
    public get nativeElement(): HTMLElement {
432
        return this.element.nativeElement;
1,273,149✔
433
    }
434

435
    /**
436
     * @hidden
437
     * @internal
438
     */
439
    @Input()
440
    public get cellSelectionMode() {
441
        return this._cellSelection;
305,776✔
442
    }
443

444
    public set cellSelectionMode(value) {
445
        if (this._cellSelection === value) {
152,746✔
446
            return;
151,774✔
447
        }
448
        this.zone.runOutsideAngular(() => {
972✔
449
            if (value === GridSelectionMode.multiple) {
972✔
450
                this.addPointerListeners(value);
60✔
451
            } else {
452
                this.removePointerListeners(this._cellSelection);
912✔
453
            }
454
        });
455
        this._cellSelection = value;
972✔
456
    }
457

458
    /**
459
     * @hidden
460
     * @internal
461
     */
462
    @Input()
463
    public set lastSearchInfo(value: ISearchInfo) {
464
        this._lastSearchInfo = value;
157,678✔
465
        this.highlightText(this._lastSearchInfo.searchText, this._lastSearchInfo.caseSensitive, this._lastSearchInfo.exactMatch);
157,678✔
466
    }
467

468
    /**
469
     * @hidden
470
     * @internal
471
     */
472
    @Input()
473
    @HostBinding('class.igx-grid__td--pinned-last')
474
    public lastPinned = false;
152,378✔
475

476
    /**
477
     * @hidden
478
     * @internal
479
     */
480
    @Input()
481
    @HostBinding('class.igx-grid__td--pinned-first')
482
    public firstPinned = false;
152,378✔
483

484
    /**
485
     * Returns whether the cell is in edit mode.
486
     */
487
    @Input({ transform: booleanAttribute })
488
    @HostBinding('class.igx-grid__td--editing')
489
    public editMode = false;
152,378✔
490

491
    /**
492
     * Sets/get the `role` property of the cell.
493
     * Default value is `"gridcell"`.
494
     * ```typescript
495
     * this.cell.role = 'grid-cell';
496
     * ```
497
     * ```typescript
498
     * let cellRole = this.cell.role;
499
     * ```
500
     *
501
     * @memberof IgxGridCellComponent
502
     */
503
    @HostBinding('attr.role')
504
    public role = 'gridcell';
152,378✔
505

506
    /**
507
     * Gets whether the cell is editable.
508
     * ```typescript
509
     * let isCellReadonly = this.cell.readonly;
510
     * ```
511
     *
512
     * @memberof IgxGridCellComponent
513
     */
514
    @HostBinding('attr.aria-readonly')
515
    public get readonly(): boolean {
516
        return !this.editable;
1,212,714✔
517
    }
518

519
    /** @hidden @internal */
520
    @HostBinding('attr.aria-describedby')
521
    public get ariaDescribeBy() {
522
        let describeBy = (this.gridID + '_' + this.column.field).replace('.', '_');
1,213,802✔
523
        if (this.isInvalid) {
1,213,802✔
524
            describeBy += ' ' + this.ariaErrorMessage;
110✔
525
        }
526
        return describeBy;
1,213,802✔
527
    }
528

529
    /** @hidden @internal */
530
    public get ariaErrorMessage() {
531
        return this.grid.id + '_' + this.column.field + '_' + this.intRow.index + '_error';
253✔
532
    }
533

534
    /**
535
     * @hidden
536
     * @internal
537
     */
538
    @HostBinding('class.igx-grid__td--invalid')
539
    @HostBinding('attr.aria-invalid')
540
    public get isInvalid() {
541
        const fieldName = this.getTransformedFieldName(this.column?.field);
4,614,153✔
542
        const isInvalid = this.formGroup?.get(fieldName)?.invalid && this.formGroup?.get(fieldName)?.touched;
4,614,153✔
543
        return !this.intRow.deleted && isInvalid;
4,614,153✔
544
    }
545

546
    /**
547
     * @hidden
548
     * @internal
549
     */
550
    @HostBinding('class.igx-grid__td--valid')
551
    public get isValidAfterEdit() {
552
        const fieldName = this.getTransformedFieldName(this.column?.field);
1,212,713✔
553
        const formControl = this.formGroup?.get(fieldName);
1,212,713✔
554
        return this.editMode && formControl && !formControl.invalid && formControl.dirty;
1,212,713✔
555
    }
556

557
    /**
558
     * Gets the formControl responsible for value changes and validation for this cell.
559
     */
560
    protected get formControl(): FormControl {
561
        return this.grid.validation.getFormControl(this.intRow.key, this.column.field) as FormControl;
54,161✔
562
    }
563

564
    public get gridRowSpan(): number {
565
        return this.column.gridRowSpan;
198✔
566
    }
567

568
    public get gridColumnSpan(): number {
569
        return this.column.gridColumnSpan;
198✔
570
    }
571

572
    public get rowEnd(): number {
UNCOV
573
        return this.column.rowEnd;
×
574
    }
575

576
    public get colEnd(): number {
577
        return this.column.colEnd;
×
578
    }
579

580
    public get rowStart(): number {
581
        return this.column.rowStart;
×
582
    }
583

584
    public get colStart(): number {
585
        return this.column.colStart;
×
586
    }
587

588
    /**
589
     * Gets the width of the cell.
590
     * ```typescript
591
     * let cellWidth = this.cell.width;
592
     * ```
593
     *
594
     * @memberof IgxGridCellComponent
595
     */
596
    @Input()
597
    public width = '';
152,378✔
598

599
    /**
600
     * @hidden
601
     */
602
    @Input()
603
    @HostBinding('class.igx-grid__td--active')
604
    public active = false;
152,378✔
605

606
    @HostBinding('attr.aria-selected')
607
    public get ariaSelected() {
608
        return this.selected || this.column.selected || this.intRow.selected;
1,212,713✔
609
    }
610

611
    /**
612
     * Gets whether the cell is selected.
613
     * ```typescript
614
     * let isSelected = this.cell.selected;
615
     * ```
616
     *
617
     * @memberof IgxGridCellComponent
618
     */
619
    @HostBinding('class.igx-grid__td--selected')
620
    public get selected() {
621
        return this.selectionService.selected(this.selectionNode);
2,427,511✔
622
    }
623

624
    /**
625
     * Selects/deselects the cell.
626
     * ```typescript
627
     * this.cell.selected = true.
628
     * ```
629
     *
630
     * @memberof IgxGridCellComponent
631
     */
632
    public set selected(val: boolean) {
633
        const node = this.selectionNode;
1✔
634
        if (val) {
1!
635
            this.selectionService.add(node);
1✔
636
        } else {
UNCOV
637
            this.selectionService.remove(node);
×
638
        }
639
        this.grid.notifyChanges();
1✔
640
    }
641

642
    /**
643
     * Gets whether the cell column is selected.
644
     * ```typescript
645
     * let isCellColumnSelected = this.cell.columnSelected;
646
     * ```
647
     *
648
     * @memberof IgxGridCellComponent
649
     */
650
    @HostBinding('class.igx-grid__td--column-selected')
651
    public get columnSelected() {
652
        return this.selectionService.isColumnSelected(this.column.field);
1,212,713✔
653
    }
654

655
    /**
656
     * Sets the current edit value while a cell is in edit mode.
657
     * Only for cell editing mode.
658
     * ```typescript
659
     * this.cell.editValue = value;
660
     * ```
661
     *
662
     * @memberof IgxGridCellComponent
663
     */
664
    public set editValue(value) {
665
        if (this.grid.crudService.cellInEditMode) {
44✔
666
            this.grid.crudService.cell.editValue = value;
44✔
667
        }
668
    }
669

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

685
    /**
686
     * Returns whether the cell is editable.
687
     */
688
    public get editable(): boolean {
689
        return this.column.editable && !this.intRow.disabled;
1,213,981✔
690
    }
691

692
    /**
693
     * @hidden
694
     */
695
    @Input()
696
    @HostBinding('class.igx-grid__td--row-pinned-first')
697
    public displayPinnedChip = false;
152,378✔
698

699
    @HostBinding('style.min-height.px')
700
    protected get minHeight() {
701
        if ((this.grid as any).isCustomSetRowHeight) {
1,212,713✔
702
            return this.grid.renderedRowHeight;
464✔
703
        }
704
    }
705

706
    @ViewChild('defaultCell', { read: TemplateRef, static: true })
707
    protected defaultCellTemplate: TemplateRef<any>;
708

709
    @ViewChild('defaultPinnedIndicator', { read: TemplateRef, static: true })
710
    protected defaultPinnedIndicator: TemplateRef<any>;
711

712
    @ViewChild('inlineEditor', { read: TemplateRef, static: true })
713
    protected inlineEditorTemplate: TemplateRef<any>;
714

715
    @ViewChild('addRowCell', { read: TemplateRef, static: true })
716
    protected addRowCellTemplate: TemplateRef<any>;
717

718
    @ViewChild(IgxTextHighlightDirective, { read: IgxTextHighlightDirective })
719
    protected set highlight(value: IgxTextHighlightDirective) {
720
        this._highlight = value;
154,403✔
721

722
        if (this._highlight && this.grid.lastSearchInfo.searchText) {
154,403✔
723
            this._highlight.highlight(this.grid.lastSearchInfo.searchText,
399✔
724
                this.grid.lastSearchInfo.caseSensitive,
725
                this.grid.lastSearchInfo.exactMatch);
726
            this._highlight.activateIfNecessary();
399✔
727
        }
728
    }
729

730
    protected get highlight() {
731
        return this._highlight;
352,741✔
732
    }
733

734
    protected get selectionNode(): ISelectionNode {
735
        return {
2,429,391✔
736
            row: this.rowIndex,
737
            column: this.column.columnLayoutChild ? this.column.parent.visibleIndex : this.visibleColumnIndex,
2,429,391✔
738
            layout: this.column.columnLayoutChild ? {
2,429,391✔
739
                rowStart: this.column.rowStart,
740
                colStart: this.column.colStart,
741
                rowEnd: this.column.rowEnd,
742
                colEnd: this.column.colEnd,
743
                columnVisibleIndex: this.visibleColumnIndex
744
            } : null
745
        };
746
    }
747

748
    /**
749
     * Sets/gets the highlight class of the cell.
750
     * Default value is `"igx-highlight"`.
751
     * ```typescript
752
     * let highlightClass = this.cell.highlightClass;
753
     * ```
754
     * ```typescript
755
     * this.cell.highlightClass = 'igx-cell-highlight';
756
     * ```
757
     *
758
     * @memberof IgxGridCellComponent
759
     */
760
    public highlightClass = 'igx-highlight';
152,378✔
761

762
    /**
763
     * Sets/gets the active highlight class class of the cell.
764
     * Default value is `"igx-highlight__active"`.
765
     * ```typescript
766
     * let activeHighlightClass = this.cell.activeHighlightClass;
767
     * ```
768
     * ```typescript
769
     * this.cell.activeHighlightClass = 'igx-cell-highlight_active';
770
     * ```
771
     *
772
     * @memberof IgxGridCellComponent
773
     */
774
    public activeHighlightClass = 'igx-highlight__active';
152,378✔
775

776
    /** @hidden @internal */
777
    public get step(): number {
778
        const digitsInfo = this.column.pipeArgs.digitsInfo;
218✔
779
        if (!digitsInfo) {
218!
UNCOV
780
            return 1;
×
781
        }
782
        const step = +digitsInfo.substr(digitsInfo.indexOf('.') + 1, 1);
218✔
783
        return 1 / (Math.pow(10, step));
218✔
784
    }
785

786
    /** @hidden @internal */
787
    public get currencyCode(): string {
788
        return this.column.pipeArgs.currencyCode ?
57,180✔
789
            this.column.pipeArgs.currencyCode : getLocaleCurrencyCode(this.grid.locale);
790
    }
791

792
    /** @hidden @internal */
793
    public get currencyCodeSymbol(): string {
794
        return getCurrencySymbol(this.currencyCode, 'wide', this.grid.locale);
4✔
795
    }
796

797
    protected _lastSearchInfo: ISearchInfo;
798
    private _highlight: IgxTextHighlightDirective;
799
    private _cellSelection: GridSelectionMode = GridSelectionMode.multiple;
152,378✔
800
    private _vIndex = -1;
152,378✔
801

802
    constructor(
803
        protected selectionService: IgxGridSelectionService,
152,378✔
804
        @Inject(IGX_GRID_BASE) public grid: GridType,
152,378✔
805
        @Inject(IgxOverlayService) protected overlayService: IgxOverlayService,
152,378✔
806
        public cdr: ChangeDetectorRef,
152,378✔
807
        private element: ElementRef<HTMLElement>,
152,378✔
808
        protected zone: NgZone,
152,378✔
809
        private touchManager: HammerGesturesManager,
152,378✔
810
        protected platformUtil: PlatformUtil
152,378✔
811
    ) { }
812

813
    /**
814
     * @hidden
815
     * @internal
816
     */
817
    @HostListener('dblclick', ['$event'])
818
    public onDoubleClick = (event: MouseEvent) => {
152,378✔
819
        if (event.type === 'doubletap') {
151✔
820
            // prevent double-tap to zoom on iOS
821
            event.preventDefault();
1✔
822
        }
823
        if (this.editable && !this.editMode && !this.intRow.deleted && !this.grid.crudService.rowEditingBlocked) {
151✔
824
            this.grid.crudService.enterEditMode(this, event as Event);
145✔
825
        }
826

827
        this.grid.doubleClick.emit({
151✔
828
            cell: this.getCellType(),
829
            event
830
        });
831
    };
832

833
    /**
834
     * @hidden
835
     * @internal
836
     */
837
    @HostListener('click', ['$event'])
838
    public onClick(event: MouseEvent) {
839
        this.grid.cellClick.emit({
260✔
840
            cell: this.getCellType(),
841
            event
842
        });
843
    }
844

845
    /**
846
     * @hidden
847
     * @internal
848
     */
849
    public ngOnInit() {
850
        this.zone.runOutsideAngular(() => {
152,378✔
851
            this.nativeElement.addEventListener('pointerdown', this.pointerdown);
152,378✔
852
            this.addPointerListeners(this.cellSelectionMode);
152,378✔
853
        });
854
        if (this.platformUtil.isIOS) {
152,378✔
855
            this.touchManager.addEventListener(this.nativeElement, 'doubletap', this.onDoubleClick, {
40✔
856
                cssProps: {} /* don't disable user-select, etc */
857
            });
858
        }
859

860
    }
861

862
    public ngAfterViewInit() {
863
        this.errorTooltip.changes.pipe(takeUntil(this._destroy$)).subscribe(() => {
152,378✔
864
            if (this.errorTooltip.length > 0 && this.active) {
44✔
865
                // error ocurred
866
                this.cdr.detectChanges();
25✔
867
                this.openErrorTooltip();
25✔
868
            }
869
        });
870
    }
871

872
    /**
873
     * @hidden
874
     * @internal
875
     */
876
    public errorShowing = false;
152,378✔
877

878
    private openErrorTooltip() {
879
        const tooltip = this.errorTooltip.first;
26✔
880
        tooltip.open(
26✔
881
            {
882
                target: this.errorIcon.el.nativeElement,
883
                closeOnOutsideClick: true,
884
                excludeFromOutsideClick: [this.nativeElement],
885
                closeOnEscape: false,
886
                outlet: this.grid.outlet,
887
                modal: false,
888
                positionStrategy: new AutoPositionStrategy({
889
                    horizontalStartPoint: HorizontalAlignment.Center,
890
                    horizontalDirection: HorizontalAlignment.Center,
891
                    openAnimation: useAnimation(scaleInCenter, { params: { duration: '150ms' } }),
892
                    closeAnimation: useAnimation(fadeOut, { params: { duration: '75ms' } })
893
                })
894
            }
895
        );
896
    }
897

898
    /**
899
     * @hidden
900
     * @internal
901
     */
902
    public ngOnDestroy() {
903
        this.zone.runOutsideAngular(() => {
151,033✔
904
            this.nativeElement.removeEventListener('pointerdown', this.pointerdown);
151,033✔
905
            this.removePointerListeners(this.cellSelectionMode);
151,033✔
906
        });
907
        this.touchManager.destroy();
151,033✔
908
        this._destroy$.next();
151,033✔
909
        this._destroy$.complete();
151,033✔
910
    }
911

912
    /**
913
     * @hidden
914
     * @internal
915
     */
916
    public ngOnChanges(changes: SimpleChanges): void {
917
        if (changes.editMode && changes.editMode.currentValue && this.formControl) {
290,847✔
918
            // ensure when values change, form control is forced to be marked as touche.
919
            this.formControl.valueChanges.pipe(takeWhile(() => this.editMode)).subscribe(() => this.formControl.markAsTouched());
400✔
920
            // while in edit mode subscribe to value changes on the current form control and set to editValue
921
            this.formControl.statusChanges.pipe(takeWhile(() => this.editMode)).subscribe(status => {
400✔
922
                if (status === 'INVALID' && this.errorTooltip.length > 0) {
190!
UNCOV
923
                    this.cdr.detectChanges();
×
UNCOV
924
                    const tooltip = this.errorTooltip.first;
×
UNCOV
925
                    this.resizeAndRepositionOverlayById(tooltip.overlayId, this.errorTooltip.first.element.offsetWidth);
×
926
                }
927
            });
928
        }
929
        if (changes.value && !changes.value.firstChange) {
290,847✔
930
            if (this.highlight) {
47,682✔
931
                this.highlight.lastSearchInfo.searchText = this.grid.lastSearchInfo.searchText;
45,293✔
932
                this.highlight.lastSearchInfo.caseSensitive = this.grid.lastSearchInfo.caseSensitive;
45,293✔
933
                this.highlight.lastSearchInfo.exactMatch = this.grid.lastSearchInfo.exactMatch;
45,293✔
934
            }
935
            const isInEdit = this.grid.rowEditable ? this.row.inEditMode : this.editMode;
47,682✔
936
            if (this.formControl && this.formControl.value !== changes.value.currentValue && !isInEdit) {
47,682✔
937
                this.formControl.setValue(changes.value.currentValue);
131✔
938
            }
939
        }
940
    }
941

942

943

944
    /**
945
     * @hidden @internal
946
     */
947
    private resizeAndRepositionOverlayById(overlayId: string, newSize: number) {
UNCOV
948
        const overlay = this.overlayService.getOverlayById(overlayId);
×
UNCOV
949
        if (!overlay) return;
×
UNCOV
950
        overlay.initialSize.width = newSize;
×
UNCOV
951
        overlay.elementRef.nativeElement.parentElement.style.width = newSize + 'px';
×
952
        this.overlayService.reposition(overlayId);
×
953
    }
954

955
    /**
956
     * Starts/ends edit mode for the cell.
957
     *
958
     * ```typescript
959
     * cell.setEditMode(true);
960
     * ```
961
     */
962
    public setEditMode(value: boolean): void {
963
        if (this.intRow.deleted) {
30!
UNCOV
964
            return;
×
965
        }
966
        if (this.editable && value) {
30✔
967
            if (this.grid.crudService.cellInEditMode) {
26✔
968
                this.grid.gridAPI.update_cell(this.grid.crudService.cell);
5✔
969
                this.grid.crudService.endCellEdit();
5✔
970
            }
971
            this.grid.crudService.enterEditMode(this);
26✔
972
        } else {
973
            this.grid.crudService.endCellEdit();
4✔
974
        }
975
        this.grid.notifyChanges();
30✔
976
    }
977

978
    /**
979
     * Sets new value to the cell.
980
     * ```typescript
981
     * this.cell.update('New Value');
982
     * ```
983
     *
984
     * @memberof IgxGridCellComponent
985
     */
986
    // TODO: Refactor
987
    public update(val: any) {
988
        if (this.intRow.deleted) {
34!
UNCOV
989
            return;
×
990
        }
991

992
        let cell = this.grid.crudService.cell;
34✔
993
        if (!cell) {
34✔
994
            cell = this.grid.crudService.createCell(this);
14✔
995
        }
996
        cell.editValue = val;
34✔
997
        this.grid.gridAPI.update_cell(cell);
34✔
998
        this.grid.crudService.endCellEdit();
34✔
999
        this.cdr.markForCheck();
34✔
1000
    }
1001

1002
    /**
1003
     *
1004
     * @hidden
1005
     * @internal
1006
     */
1007
    public pointerdown = (event: PointerEvent) => {
152,378✔
1008
        if (this.cellSelectionMode !== GridSelectionMode.multiple) {
425✔
1009
            this.activate(event);
23✔
1010
            return;
23✔
1011
        }
1012
        if (!this.platformUtil.isLeftClick(event)) {
402✔
1013
            event.preventDefault();
4✔
1014
            this.grid.navigation.setActiveNode({ rowIndex: this.rowIndex, colIndex: this.visibleColumnIndex });
4✔
1015
            this.selectionService.addKeyboardRange();
4✔
1016
            this.selectionService.initKeyboardState();
4✔
1017
            this.selectionService.primaryButton = false;
4✔
1018
            // Ensure RMB Click on edited cell does not end cell editing
1019
            if (!this.selected) {
4✔
1020
                this.grid.crudService.updateCell(true, event);
2✔
1021
            }
1022
            return;
4✔
1023
        } else {
1024
            this.selectionService.primaryButton = true;
398✔
1025
        }
1026
        this.selectionService.pointerDown(this.selectionNode, event.shiftKey, event.ctrlKey);
398✔
1027
        this.activate(event);
398✔
1028
    };
1029

1030
    /**
1031
     *
1032
     * @hidden
1033
     * @internal
1034
     */
1035
    public pointerenter = (event: PointerEvent) => {
152,378✔
1036
        const isHierarchicalGrid = this.grid.type === 'hierarchical';
118✔
1037
        if (isHierarchicalGrid && (!this.grid.navigation?.activeNode?.gridID || this.grid.navigation.activeNode.gridID !== this.gridID)) {
118✔
1038
            return;
3✔
1039
        }
1040
        const dragMode = this.selectionService.pointerEnter(this.selectionNode, event);
115✔
1041
        if (dragMode) {
115✔
1042
            this.grid.cdr.detectChanges();
114✔
1043
        }
1044
    };
1045

1046
    /**
1047
     * @hidden
1048
     * @internal
1049
     */
1050
    public focusout = () => {
152,378✔
1051
        this.closeErrorTooltip();
48✔
1052
    }
1053

1054
    private closeErrorTooltip() {
1055
        const tooltip = this.errorTooltip.first;
48✔
1056
        if (tooltip) {
48!
UNCOV
1057
            tooltip.close();
×
1058
        }
1059
    }
1060

1061
    /**
1062
     * @hidden
1063
     * @internal
1064
     */
1065
    public pointerup = (event: PointerEvent) => {
152,378✔
1066
        const isHierarchicalGrid = this.grid.type === 'hierarchical';
399✔
1067
        if (!this.platformUtil.isLeftClick(event) || (isHierarchicalGrid && (!this.grid.navigation?.activeNode?.gridID ||
399✔
1068
            this.grid.navigation.activeNode.gridID !== this.gridID))) {
1069
            return;
4✔
1070
        }
1071
        if (this.selectionService.pointerUp(this.selectionNode, this.grid.rangeSelected)) {
395✔
1072
            this.grid.cdr.detectChanges();
49✔
1073
        }
1074
    };
1075

1076
    /**
1077
     * @hidden
1078
     * @internal
1079
     */
1080
    public activate(event: FocusEvent | KeyboardEvent) {
1081
        const node = this.selectionNode;
971✔
1082
        let shouldEmitSelection = !this.selectionService.isActiveNode(node);
971✔
1083

1084
        if (this.selectionService.primaryButton) {
971!
1085
            const currentActive = this.selectionService.activeElement;
971✔
1086
            if (this.cellSelectionMode === GridSelectionMode.single && (event as any)?.ctrlKey && this.selected) {
971✔
1087
                this.selectionService.activeElement = null;
1✔
1088
                shouldEmitSelection = true;
1✔
1089
            } else {
1090
                this.selectionService.activeElement = node;
970✔
1091
            }
1092
            const cancel = this._updateCRUDStatus(event);
971✔
1093
            if (cancel) {
971✔
1094
                this.selectionService.activeElement = currentActive;
2✔
1095
                return;
2✔
1096
            }
1097

1098
            const activeElement = this.selectionService.activeElement;
969✔
1099
            const row = activeElement ? this.grid.gridAPI.get_row_by_index(activeElement.row) : null;
969✔
1100
            if (this.grid.crudService.rowEditingBlocked && row && this.intRow.key !== row.key) {
969!
UNCOV
1101
                return;
×
1102
            }
1103

1104
        } else {
1105
            this.selectionService.activeElement = null;
×
UNCOV
1106
            if (this.grid.crudService.cellInEditMode && !this.editMode) {
×
UNCOV
1107
                this.grid.crudService.updateCell(true, event);
×
1108
            }
1109
        }
1110

1111
        this.grid.navigation.setActiveNode({ row: this.rowIndex, column: this.visibleColumnIndex });
969✔
1112

1113
        const isTargetErrorIcon = event && event.target && event.target === this.errorIcon?.el.nativeElement
969✔
1114
        if (this.isInvalid && !isTargetErrorIcon) {
969✔
1115
            this.cdr.detectChanges();
1✔
1116
            this.openErrorTooltip();
1✔
1117
            this.grid.activeNodeChange.pipe(first()).subscribe(() => {
1✔
UNCOV
1118
                this.closeErrorTooltip();
×
1119
            });
1120
        }
1121
        this.selectionService.primaryButton = true;
969✔
1122
        if (this.cellSelectionMode === GridSelectionMode.multiple && this.selectionService.activeElement) {
969✔
1123
            if (this.selectionService.isInMap(this.selectionService.activeElement) && (event as any)?.ctrlKey && !(event as any)?.shiftKey) {
937✔
1124
                this.selectionService.remove(this.selectionService.activeElement);
3✔
1125
                shouldEmitSelection = true;
3✔
1126
            } else {
1127
                this.selectionService.add(this.selectionService.activeElement, false); // pointer events handle range generation
934✔
1128
                this.selectionService.keyboardStateOnFocus(node, this.grid.rangeSelected, this.nativeElement);
934✔
1129
            }
1130
        }
1131
        if (this.grid.isCellSelectable && shouldEmitSelection) {
969✔
1132
            this.zone.run(() => this.grid.selected.emit({ cell: this.getCellType(), event }));
942✔
1133
        }
1134
    }
1135

1136
    /**
1137
     * If the provided string matches the text in the cell, the text gets highlighted.
1138
     * ```typescript
1139
     * this.cell.highlightText('Cell Value', true);
1140
     * ```
1141
     *
1142
     * @memberof IgxGridCellComponent
1143
     */
1144
    public highlightText(text: string, caseSensitive?: boolean, exactMatch?: boolean): number {
1145
        return this.highlight && this.column.searchable ? this.highlight.highlight(text, caseSensitive, exactMatch) : 0;
160,775✔
1146
    }
1147

1148
    /**
1149
     * Clears the highlight of the text in the cell.
1150
     * ```typescript
1151
     * this.cell.clearHighLight();
1152
     * ```
1153
     *
1154
     * @memberof IgxGridCellComponent
1155
     */
1156
    public clearHighlight() {
1157
        if (this.highlight && this.column.searchable) {
40✔
1158
            this.highlight.clearHighlight();
40✔
1159
        }
1160
    }
1161

1162
    /**
1163
     * @hidden
1164
     * @internal
1165
     */
1166
    public calculateSizeToFit(range: any): number {
1167
        return this.platformUtil.getNodeSizeViaRange(range, this.nativeElement);
214✔
1168
    }
1169

1170
    /**
1171
     * @hidden
1172
     * @internal
1173
     */
1174
    public get searchMetadata() {
1175
        const meta = new Map<string, any>();
303,112✔
1176
        meta.set('pinned', this.grid.isRecordPinnedByViewIndex(this.intRow.index));
303,112✔
1177
        return meta;
303,112✔
1178
    }
1179

1180
    /**
1181
     * @hidden
1182
     * @internal
1183
     */
1184
    public getTransformedFieldName(field: string): string {
1185
        return field?.replace(/\./g, '_');
5,827,982✔
1186
    }
1187

1188
    /**
1189
     * @hidden
1190
     * @internal
1191
     */
1192
    private _updateCRUDStatus(event?: Event) {
1193
        if (this.editMode) {
971✔
1194
            return;
48✔
1195
        }
1196

1197
        let editableArgs;
1198
        const crud = this.grid.crudService;
923✔
1199
        const editableCell = this.grid.crudService.cell;
923✔
1200
        const editMode = !!(crud.row || crud.cell);
923✔
1201

1202
        if (this.editable && editMode && !this.intRow.deleted) {
923✔
1203
            if (editableCell) {
88✔
1204
                editableArgs = this.grid.crudService.updateCell(false, event);
77✔
1205

1206
                /* This check is related with the following issue #6517:
1207
                 * when edit cell that belongs to a column which is sorted and press tab,
1208
                 * the next cell in edit mode is with wrong value /its context is not updated/;
1209
                 * So we reapply sorting before the next cell enters edit mode.
1210
                 * Also we need to keep the notifyChanges below, because of the current
1211
                 * change detection cycle when we have editing with enabled transactions
1212
                 */
1213
                if (this.grid.sortingExpressions.length && this.grid.sortingExpressions.indexOf(editableCell.column.field)) {
77!
UNCOV
1214
                    this.grid.cdr.detectChanges();
×
1215
                }
1216

1217
                if (editableArgs && editableArgs.cancel) {
77✔
1218
                    return true;
2✔
1219
                }
1220

1221
                crud.exitCellEdit(event);
75✔
1222
            }
1223
            this.grid.tbody.nativeElement.focus({ preventScroll: true });
86✔
1224
            this.grid.notifyChanges();
86✔
1225
            crud.enterEditMode(this, event);
86✔
1226
            return false;
86✔
1227
        }
1228

1229
        if (editableCell && crud.sameRow(this.cellID.rowID)) {
835✔
1230
            this.grid.crudService.updateCell(true, event);
1✔
1231
        } else if (editMode && !crud.sameRow(this.cellID.rowID)) {
834✔
1232
            this.grid.crudService.endEdit(true, event);
3✔
1233
        }
1234
    }
1235

1236
    private addPointerListeners(selection) {
1237
        if (selection !== GridSelectionMode.multiple) {
152,438✔
1238
            return;
604✔
1239
        }
1240
        this.nativeElement.addEventListener('pointerenter', this.pointerenter);
151,834✔
1241
        this.nativeElement.addEventListener('pointerup', this.pointerup);
151,834✔
1242
        this.nativeElement.addEventListener('focusout', this.focusout);
151,834✔
1243
    }
1244

1245
    private removePointerListeners(selection) {
1246
        if (selection !== GridSelectionMode.multiple) {
151,945✔
1247
            return;
852✔
1248
        }
1249
        this.nativeElement.removeEventListener('pointerenter', this.pointerenter);
151,093✔
1250
        this.nativeElement.removeEventListener('pointerup', this.pointerup);
151,093✔
1251
        this.nativeElement.removeEventListener('focusout', this.focusout);
151,093✔
1252
    }
1253

1254
    private getCellType(useRow?: boolean): CellType {
1255
        const rowID = useRow ? this.grid.createRow(this.intRow.index, this.intRow.data) : this.intRow.index;
3,351✔
1256
        return new IgxGridCell(this.grid, rowID, this.column);
3,351✔
1257
    }
1258
}
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

© 2025 Coveralls, Inc