Skip to content

Commit

Permalink
fix(cellFilter): calculate width for colDefs with : in cellFilter
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Den-dp committed May 9, 2020
1 parent a4d1d95 commit 661c7d2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 34 deletions.
4 changes: 3 additions & 1 deletion demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ function MainController($http){
{ field: 'age', displayName: 'third age in a row' }, //works ok with aliases
{ name: 'guid' },
{ name: 'registered', cellFilter: 'date:"yyyy-MM-dd"' }, //can handle filters
{ name: 'registered', displayName: 'registered2', cellFilter: 'date:"medium"' },
{ name: 'registered2', field: 'registered', cellFilter: 'date:"medium"' },
{ name: 'registered3', field: 'registered', cellFilter: 'date:"medium":"UTC"' },
{ name: 'registered4', field: 'registered', cellFilter: "date:'yyyy-MM-dd HH:mm:ss':'UTC'" },
{ name: 'picture' },
{ name: 'company' },
{ name: 'email' },
Expand Down
36 changes: 3 additions & 33 deletions src/UiGridAutoFitColumnsService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IFilterService, IQService } from 'angular';
import { IQService } from 'angular';
import { IColumnDef, IGridColumn, IGridInstance, IGridOptions, IGridRow } from 'ui-grid';
import { Measurer } from './Measurer';
import { UiGridMetrics } from './UiGridMetrics';
Expand Down Expand Up @@ -27,7 +27,7 @@ export class UiGridAutoFitColumnsService {
static $inject = ['$q', '$filter', '$parse'];
private gridMetrics: UiGridMetrics;

constructor (private $q: IQService, private $filter: IFilterService) {
constructor (private $q: IQService) {
this.gridMetrics = new UiGridMetrics();
}

Expand All @@ -43,31 +43,6 @@ export class UiGridAutoFitColumnsService {
gridOptions.enableColumnAutoFit = gridOptions.enableColumnAutoFit !== false;
}

private getFilterIfExists<T>(filterName): any {
try {
return this.$filter<IAnyFilterPredicateFunc>(filterName);
} catch (e) {
return null;
}
}

private getFilteredValue(value: string, cellFilter: string) {
if (cellFilter && cellFilter !== '') {
const filter = this.getFilterIfExists(cellFilter);
if (filter) {
value = filter(value);
} else {
// https://regex101.com/r/rC5eR5/2
const re = /([^:]*):([^:]*):?([\s\S]+)?/;
let matches;
if ((matches = re.exec(cellFilter)) !== null) {
value = this.$filter<IAnyFilterPredicateFunc>(matches[1])(value, matches[2], matches[3]);
}
}
}
return value;
}

colAutoFitColumnBuilder(colDef: IExtendedColumnDef, col: IExtendedGridColumn, gridOptions: IExtendedGridOptions) {
const promises = [];

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

rows.forEach((row) => {
let cellText = row.grid.getCellValue(row, column);

if (!!column.colDef.cellFilter) {
cellText = this.getFilteredValue(cellText, column.colDef.cellFilter);
}

const cellText = row.grid.getCellDisplayValue(row, column);
const currentCellWidth = Measurer.measureRoundedTextWidth(cellText, this.gridMetrics.getCellFont());
const optimalCellWidth = currentCellWidth > 300 ? 300 : currentCellWidth;

Expand Down

0 comments on commit 661c7d2

Please sign in to comment.