Show tooltip only when cell data has been truncated
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!
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.
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.
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!
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.
@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 + '
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); } }; });