Skip to content

Commit 59f4771

Browse files
committed
Enh(col): fix #3118 provide for headerTooltip, provide for static string
1 parent 3224d02 commit 59f4771

File tree

6 files changed

+71
-27
lines changed

6 files changed

+71
-27
lines changed

misc/tutorial/103_filtering.ngdoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ In this example we've provided a "toggle filters" button to allow you to turn th
7272
still visually indicate which columns are filtered even when the filters aren't present, we've used the headerCellClass
7373
to make any columns with a filter condition have blue text.
7474

75-
## Single filter box (similar to 2.x)
75+
### Single filter box (similar to 2.x)
7676

7777
Refer the {@link 499_FAQ FAQ}, it is possible to implement this using a rowsProcessor.
7878

misc/tutorial/117_tooltips.ngdoc

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,26 @@
55
You can set a tooltip (actually, a title) to pop up when a user hovers over a cell.
66

77
This tooltip can be simply the cell contents, in which case set the columnDef to have
8-
`cellTooltip: true`. Or it can be a function that returns a value derived from the
8+
`cellTooltip: true`.
9+
10+
Or it can be a function that returns a value derived from the
911
current column and row - for example:
12+
1013
```
1114
cellTooltip: function(row, col) {
1215
return 'Name: ' + row.entity.name + ' Company: ' + row.entity.company;
1316
}
1417
```
1518

19+
Or it can be a string, in which case that string will be displayed:
20+
21+
```
22+
cellTooltip: 'Custom tooltip - maybe some help text'
23+
```
24+
25+
26+
You can also set a headerTooltip in a similar manner, it defaults to showing the displayName.
27+
1628
Note that turning on tooltips will create an extra watcher per cell, so it has an impact on overall grid
1729
performance, it is not recommended to turn them on for every column, rather only for the columns likely to have
1830
data that won't be displayable within the grid row (e.g. long description fields).
@@ -27,13 +39,16 @@ Tooltips respect the cellFilter, so if you define a cellFilter it will also be u
2739
$scope.gridOptions = {
2840
enableSorting: true,
2941
columnDefs: [
30-
{ field: 'name', cellTooltip: true },
42+
{ field: 'name', cellTooltip: 'Custom string', headerTooltip: 'Custom header string' },
3143
{ field: 'company', cellTooltip:
3244
function( row, col ) {
3345
return 'Name: ' + row.entity.name + ' Company: ' + row.entity.company;
34-
}
46+
}, headerTooltip:
47+
function( col ) {
48+
return 'Header: ' + col.displayName;
49+
}
3550
},
36-
{ field: 'gender', cellTooltip: true, cellFilter: 'mapGender' },
51+
{ field: 'gender', cellTooltip: true, headerTooltip: true, cellFilter: 'mapGender' },
3752
],
3853
onRegisterApi: function( gridApi ) {
3954
$scope.gridApi = gridApi;

src/js/core/factories/Grid.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -890,17 +890,6 @@ angular.module('ui.grid')
890890
var html = col.cellTemplate.replace(uiGridConstants.MODEL_COL_FIELD, self.getQualifiedColField(col));
891891
html = html.replace(uiGridConstants.COL_FIELD, 'grid.getCellValue(row, col)');
892892

893-
if (col.cellTooltip === false){
894-
html = html.replace(uiGridConstants.TOOLTIP, '');
895-
} else {
896-
// gridColumn will have made sure that the col either has false or a function for this value
897-
if (col.cellFilter){
898-
html = html.replace(uiGridConstants.TOOLTIP, 'title="{{col.cellTooltip(row, col) | ' + col.cellFilter + '}}"');
899-
} else {
900-
html = html.replace(uiGridConstants.TOOLTIP, 'title="{{col.cellTooltip(row, col)}}"');
901-
}
902-
}
903-
904893
var compiledElementFn = $compile(html);
905894
col.compiledElementFn = compiledElementFn;
906895

src/js/core/factories/GridColumn.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -487,12 +487,13 @@ angular.module('ui.grid')
487487

488488
/**
489489
* @ngdoc property
490-
* @name tooltip
490+
* @name cellTooltip
491491
* @propertyOf ui.grid.class:GridOptions.columnDef
492492
* @description Whether or not to show a tooltip when a user hovers over the cell.
493493
* If set to false, no tooltip. If true, the cell value is shown in the tooltip (useful
494494
* if you have long values in your cells), if a function then that function is called
495-
* passing in the row and the col `cellTooltip( row, col )`, and the return value is shown in the tooltip.
495+
* passing in the row and the col `cellTooltip( row, col )`, and the return value is shown in the tooltip,
496+
* if it is a static string then displays that static string.
496497
*
497498
* Defaults to false
498499
*
@@ -503,10 +504,42 @@ angular.module('ui.grid')
503504
self.cellTooltip = function(row, col) {
504505
return self.grid.getCellValue( row, col );
505506
};
506-
} else {
507+
} else if (typeof(colDef.cellTooltip) === 'function' ){
507508
self.cellTooltip = colDef.cellTooltip;
509+
} else {
510+
self.cellTooltip = function ( row, col ){
511+
return col.colDef.cellTooltip;
512+
};
513+
}
514+
515+
/**
516+
* @ngdoc property
517+
* @name headerTooltip
518+
* @propertyOf ui.grid.class:GridOptions.columnDef
519+
* @description Whether or not to show a tooltip when a user hovers over the header cell.
520+
* If set to false, no tooltip. If true, the displayName is shown in the tooltip (useful
521+
* if you have long values in your headers), if a function then that function is called
522+
* passing in the row and the col `headerTooltip( col )`, and the return value is shown in the tooltip,
523+
* if a static string then shows that static string.
524+
*
525+
* Defaults to false
526+
*
527+
*/
528+
if ( typeof(colDef.headerTooltip) === 'undefined' || colDef.headerTooltip === false ) {
529+
self.headerTooltip = false;
530+
} else if ( colDef.headerTooltip === true ){
531+
self.headerTooltip = function(col) {
532+
return col.displayName;
533+
};
534+
} else if (typeof(colDef.headerTooltip) === 'function' ){
535+
self.headerTooltip = colDef.headerTooltip;
536+
} else {
537+
self.headerTooltip = function ( col ) {
538+
return col.colDef.headerTooltip;
539+
};
508540
}
509541

542+
510543
/**
511544
* @ngdoc property
512545
* @name footerCellClass

src/js/core/services/gridClassFactory.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@
101101

102102
var templateGetPromises = [];
103103

104-
// Abstracts the standard template processing we do for every template type
105-
var processTemplate = function( templateType, providedType, defaultTemplate, filterType ) {
104+
// Abstracts the standard template processing we do for every template type.
105+
var processTemplate = function( templateType, providedType, defaultTemplate, filterType, tooltipType ) {
106106
if ( !colDef[templateType] ){
107107
col[providedType] = defaultTemplate;
108108
} else {
@@ -112,6 +112,13 @@
112112
templateGetPromises.push(gridUtil.getTemplate(col[providedType])
113113
.then(
114114
function (template) {
115+
var tooltipCall = ( tooltipType === 'cellTooltip' ) ? 'col.cellTooltip(row,col)' : 'col.headerTooltip(col)';
116+
if ( tooltipType && col[tooltipType] === false ){
117+
template = template.replace(uiGridConstants.TOOLTIP, '');
118+
} else if ( tooltipType && col[tooltipType] ){
119+
template = template.replace(uiGridConstants.TOOLTIP, 'title="{{' + tooltipCall + ' CUSTOM_FILTERS }}"');
120+
}
121+
115122
if ( filterType ){
116123
col[templateType] = template.replace(uiGridConstants.CUSTOM_FILTERS, col[filterType] ? "|" + col[filterType] : "");
117124
} else {
@@ -124,8 +131,8 @@
124131
);
125132

126133
};
127-
128-
134+
135+
129136
/**
130137
* @ngdoc property
131138
* @name cellTemplate
@@ -135,9 +142,9 @@
135142
* must contain a div that can receive focus.
136143
*
137144
*/
138-
processTemplate( 'cellTemplate', 'providedCellTemplate', 'ui-grid/uiGridCell', 'cellFilter' );
145+
processTemplate( 'cellTemplate', 'providedCellTemplate', 'ui-grid/uiGridCell', 'cellFilter', 'cellTooltip' );
139146
col.cellTemplatePromise = templateGetPromises[0];
140-
147+
141148
/**
142149
* @ngdoc property
143150
* @name headerCellTemplate
@@ -146,7 +153,7 @@
146153
* is ui-grid/uiGridHeaderCell
147154
*
148155
*/
149-
processTemplate( 'headerCellTemplate', 'providedHeaderCellTemplate', 'ui-grid/uiGridHeaderCell', 'headerCellFilter' );
156+
processTemplate( 'headerCellTemplate', 'providedHeaderCellTemplate', 'ui-grid/uiGridHeaderCell', 'headerCellFilter', 'headerTooltip' );
150157

151158
/**
152159
* @ngdoc property

src/templates/ui-grid/uiGridHeaderCell.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<div ng-class="{ 'sortable': sortable }">
22
<!-- <div class="ui-grid-vertical-bar">&nbsp;</div> -->
3-
<div class="ui-grid-cell-contents" col-index="renderIndex">
3+
<div class="ui-grid-cell-contents" col-index="renderIndex" title="TOOLTIP">
44
<span>{{ col.displayName CUSTOM_FILTERS }}</span>
55

66
<span ui-grid-visible="col.sort.direction" ng-class="{ 'ui-grid-icon-up-dir': col.sort.direction == asc, 'ui-grid-icon-down-dir': col.sort.direction == desc, 'ui-grid-icon-blank': !col.sort.direction }">

0 commit comments

Comments
 (0)