Skip to content

Commit 661c7d2

Browse files
committed
fix(cellFilter): calculate width for colDefs with : in cellFilter
Previously we tried to parse `cellFilter` with a custom code, that has very fragile RegularExpression. Ui-grid has a better way to do that called `getCellDisplayValue()` fix #33
1 parent a4d1d95 commit 661c7d2

File tree

2 files changed

+6
-34
lines changed

2 files changed

+6
-34
lines changed

demo/demo.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ function MainController($http){
2626
{ field: 'age', displayName: 'third age in a row' }, //works ok with aliases
2727
{ name: 'guid' },
2828
{ name: 'registered', cellFilter: 'date:"yyyy-MM-dd"' }, //can handle filters
29-
{ name: 'registered', displayName: 'registered2', cellFilter: 'date:"medium"' },
29+
{ name: 'registered2', field: 'registered', cellFilter: 'date:"medium"' },
30+
{ name: 'registered3', field: 'registered', cellFilter: 'date:"medium":"UTC"' },
31+
{ name: 'registered4', field: 'registered', cellFilter: "date:'yyyy-MM-dd HH:mm:ss':'UTC'" },
3032
{ name: 'picture' },
3133
{ name: 'company' },
3234
{ name: 'email' },

src/UiGridAutoFitColumnsService.ts

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IFilterService, IQService } from 'angular';
1+
import { IQService } from 'angular';
22
import { IColumnDef, IGridColumn, IGridInstance, IGridOptions, IGridRow } from 'ui-grid';
33
import { Measurer } from './Measurer';
44
import { UiGridMetrics } from './UiGridMetrics';
@@ -27,7 +27,7 @@ export class UiGridAutoFitColumnsService {
2727
static $inject = ['$q', '$filter', '$parse'];
2828
private gridMetrics: UiGridMetrics;
2929

30-
constructor (private $q: IQService, private $filter: IFilterService) {
30+
constructor (private $q: IQService) {
3131
this.gridMetrics = new UiGridMetrics();
3232
}
3333

@@ -43,31 +43,6 @@ export class UiGridAutoFitColumnsService {
4343
gridOptions.enableColumnAutoFit = gridOptions.enableColumnAutoFit !== false;
4444
}
4545

46-
private getFilterIfExists<T>(filterName): any {
47-
try {
48-
return this.$filter<IAnyFilterPredicateFunc>(filterName);
49-
} catch (e) {
50-
return null;
51-
}
52-
}
53-
54-
private getFilteredValue(value: string, cellFilter: string) {
55-
if (cellFilter && cellFilter !== '') {
56-
const filter = this.getFilterIfExists(cellFilter);
57-
if (filter) {
58-
value = filter(value);
59-
} else {
60-
// https://regex101.com/r/rC5eR5/2
61-
const re = /([^:]*):([^:]*):?([\s\S]+)?/;
62-
let matches;
63-
if ((matches = re.exec(cellFilter)) !== null) {
64-
value = this.$filter<IAnyFilterPredicateFunc>(matches[1])(value, matches[2], matches[3]);
65-
}
66-
}
67-
}
68-
return value;
69-
}
70-
7146
colAutoFitColumnBuilder(colDef: IExtendedColumnDef, col: IExtendedGridColumn, gridOptions: IExtendedGridOptions) {
7247
const promises = [];
7348

@@ -106,12 +81,7 @@ export class UiGridAutoFitColumnsService {
10681
optimalWidths[columnKey] = Measurer.measureRoundedTextWidth(column.displayName, this.gridMetrics.getHeaderFont()) + this.gridMetrics.getHeaderButtonsWidth();
10782

10883
rows.forEach((row) => {
109-
let cellText = row.grid.getCellValue(row, column);
110-
111-
if (!!column.colDef.cellFilter) {
112-
cellText = this.getFilteredValue(cellText, column.colDef.cellFilter);
113-
}
114-
84+
const cellText = row.grid.getCellDisplayValue(row, column);
11585
const currentCellWidth = Measurer.measureRoundedTextWidth(cellText, this.gridMetrics.getCellFont());
11686
const optimalCellWidth = currentCellWidth > 300 ? 300 : currentCellWidth;
11787

0 commit comments

Comments
 (0)