diff --git a/components/table/interface.js b/components/table/interface.js index 8b61384c43..3bcb640555 100644 --- a/components/table/interface.js +++ b/components/table/interface.js @@ -37,6 +37,7 @@ export const ColumnProps = { filteredValue: PropTypes.array, sortOrder: PropTypes.oneOfType([PropTypes.bool, PropTypes.oneOf(['ascend', 'descend'])]), sortDirections: PropTypes.array, + showOverflowTooltip: PropTypes.bool, // children?: ColumnProps[]; // onCellClick?: (record: T, event: any) => void; // onCell?: (record: T) => any; diff --git a/components/table/style/index.less b/components/table/style/index.less index 3bb44cf259..012f0167d5 100644 --- a/components/table/style/index.less +++ b/components/table/style/index.less @@ -200,6 +200,16 @@ &-tbody > tr > td { border-bottom: @border-width-base @border-style-base @border-color-split; transition: all 0.3s, border 0s; + + &.has-tooltip { + + > div { + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; + } + + } } &-thead > tr, diff --git a/components/vc-table/src/TableCell.jsx b/components/vc-table/src/TableCell.jsx index 3efa16a6be..69e51a40dc 100644 --- a/components/vc-table/src/TableCell.jsx +++ b/components/vc-table/src/TableCell.jsx @@ -1,6 +1,7 @@ import PropTypes from '../../_util/vue-types'; import get from 'lodash/get'; import { isValidElement, mergeProps } from '../../_util/props-util'; +import Tooltip from '../../tooltip'; function isInvalidRenderCellText(text) { return ( @@ -20,6 +21,33 @@ export default { expandIcon: PropTypes.any, component: PropTypes.any, }, + data() { + return { + tooltipContent: '', + notShowTooltip: false, + tooltipVisible: false, + }; + }, + mounted() { + if (this.column.showOverflowTooltip) { + let cell = this.$el; + let cellWidth = cell.getBoundingClientRect().width; + + // get real text width + let textWrapper = document.createElement('span'); + textWrapper.innerText = this.tooltipContent; + document.body.appendChild(textWrapper); + let range = document.createRange(); + range.setStart(textWrapper, 0); + range.setEnd(textWrapper, 1); + const textWidth = range.getBoundingClientRect().width; + document.body.removeChild(textWrapper); + + if (cellWidth > textWidth) { + this.notShowTooltip = true; + } + } + }, methods: { handleClick(e) { const { @@ -30,6 +58,20 @@ export default { onCellClick(record, e); } }, + handleVisibilityChange(visible) { + this.tooltipVisible = this.notShowTooltip ? false : visible; + }, + getRawText(record, dataIndex) { + let text; + if (typeof dataIndex === 'number') { + text = get(record, dataIndex); + } else if (!dataIndex || dataIndex.length === 0) { + text = record; + } else { + text = get(record, dataIndex); + } + return text; + }, }, render() { @@ -47,14 +89,9 @@ export default { const cls = className || column.class; // We should return undefined if no dataIndex is specified, but in order to // be compatible with object-path's behavior, we return the record object instead. - let text; - if (typeof dataIndex === 'number') { - text = get(record, dataIndex); - } else if (!dataIndex || dataIndex.length === 0) { - text = record; - } else { - text = get(record, dataIndex); - } + let text = this.getRawText(record, dataIndex); + this.tooltipContent = text; + let tdProps = { props: {}, attrs: {}, @@ -100,11 +137,24 @@ export default { tdProps.style = { ...tdProps.style, textAlign: column.align }; } - return ( + const cellContent = [ + indentText, + expandIcon, + text, + ]; + + return column.showOverflowTooltip ? ( + + +
{cellContent}
+
+
+ ) : ( - {indentText} - {expandIcon} - {text} + {cellContent} ); },