ui-grid icon indicating copy to clipboard operation
ui-grid copied to clipboard

Show tooltip only when cell data has been truncated

Open mscharp opened this issue 11 years ago • 6 comments

It would be great if there was a property that could be specified that would only show the tooltip if the data in the cell has been truncated, ie resulting in an ellipsis (...). Whether that's a new directive, a property such as tooltip-append-to-body="true", or a property in the columDefinitions, I'm not sure. I tried looking through some google searches, but didn't find anything. I think this is a fairly common usage of tooltips.

Hopefully this isn't a duplicate request or me missing something obvious. Thanks!

mscharp avatar Dec 23 '14 20:12 mscharp

I don't think there's a way to know if the browser will truncate a cell's content. They each seem to go about it differently.

You could take each cell, render its contents in an invisible container and measure to see if it would be wider than the cell it's in, but that would completely thrash your browser.

c0bra avatar Dec 23 '14 21:12 c0bra

I used ui.bootstrap to add a tooltip for a cell that has content longer than a certain number of characters. The problem is that the tooltip gets cut off by the overflow property of "ui-grid-viewport" for the first row.

genu avatar Mar 05 '15 19:03 genu

Hello guys, I have just investigated this a bit and found a possible solution for determining if a cell is "ellipsed" or not.

function isEllipsisActive(e) {
     return (e.offsetWidth < e.scrollWidth);
}

where "e" is the HTMLElement that represents the cell:

<div class="ui-grid-cell-contents ng-binding ng-scope">Test text</div>

This is a much simpler and less expensive way of determining if the text has been cut of than to create a hidden element without text-overflow on it and measuring its width to compare.

Let me know what you think and when do you estimate that this feature will be introduced in the code.

Thanks!

iorzetic avatar Apr 15 '15 08:04 iorzetic

We've introduced a tooltip feature for both cells and headers. I don't think we'll introduce a dynamic feature where they only show the tooltip if cutoff, but I think always showing the tooltip is sufficient for the stated purpose. Closing, happy to accept a PR if someone wants to do the additional work to make this dependent on size.

PaulL1 avatar May 27 '15 07:05 PaulL1

@iorzetic is there a way to get the HTMLElement that represents the cell?

i was trying to implement dynamic tooltips if the cell is truncated using something like this:

`cellTemplate: '{{grid.appScope.setCellContents(row, cell)}}'

function setCellContents (row, cell) { var content = '<div'; if (cell.offsetWidth < cell.scrollWidth) { content += 'uib-tooltip="row.entity.name">'; } else { content += '>'; } content += row.entity.name + '

'; return content; }`

alib192 avatar Jul 15 '16 14:07 alib192

It can be done by using directive in cell template

In Cell template <div class="ui-grid-cell-contents" text-truncate-tooltip>{{COL_FIELD || "NA"}} </div>

Filter: app.filter('count', [function() { return function(value) { let count = 0; if(value){ if(angular.isArray(value) || angular.isString(value)) count = value.length; else if(angular.isObject(value)) count = Object.keys(value).length; } return count; }; }]); In directive: app.directive('textTruncateTooltip', function($filters){ return { restrict: 'AE', link: function(scope, element, attrs) { function setOverflowFlag() { let visibleWidth = element[0].offsetWidth, contentWidth = element[0].scrollWidth; if(contentWidth > visibleWidth){ if($filter('count')(element[0].dataset) == 0 ) { element.attr("title", element[0].textContent) }else if(element[0].hasOwnProperty('tooltip')){ element.attr("title", "") } }else{ element[0].tooltip = false; element.removeAttr("title"); } } element.bind('mouseover', setOverflowFlag); } }; });

Manik92 avatar Jun 03 '20 07:06 Manik92