简体   繁体   English

工具提示在表格单元格中的位置

[英]Tooltip positioning in table cell

I'm using React and have a table with some actions (delete, edit, etc.) in the cell. 我正在使用React,并且在单元格中有一个表,其中包含一些操作(删除,编辑等)。 And I need to put a tooltip on each action. 我需要在每个操作上添加工具提示。 I'm not using the jquery and don't plan to, and not title props (I will need to upgrade this tooltip to some specific data or even another component). 我没有使用jquery,也没有计划,也没有标题道具(我需要将此工具提示升级到某些特定数据甚至其他组件)。

So the problem is I can't position the tooltip correctly (for example in the middle of the top or bottom). 所以问题是我无法正确放置工具提示(例如,在顶部或底部的中间)。 Witch params should my component receive and how to do it with css? 我的组件应该接收女巫参数,以及如何使用CSS做到这一点?

 const Tooltip = ({position = 'top', display, style, children}) => { let displayClass = display ? `fade ${position} in` : `tooltip-${position}` return ( <div className={`tooltip ${displayClass} `} role='tooltip'> <div className='tooltip-arrow' /> <div className='tooltip-inner'> {children} </div> </div> ) } const ActionLinkItem = ({page, action, data, onMouseEnter, onMouseLeave, display, tooltipText, id}) => { const {buttonClass, icon} = actionsStyles[action] return ( <Link to={`/${page}/${action.toLowerCase()}/${data.id}`}> <a className={`btn btn-xs ${buttonClass}`} id={id} onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave} ><i className={`fa fa-${icon}`} /> <Tooltip display={display} action={action}>{tooltipText}</Tooltip> </a> </Link> ) } export default class Actions extends Component { constructor (props) { super(props) this.state = { tooltipActive: '' } } handleHover (event) { this.setState({ tooltipActive: event.target.id }) } handleBlur (event) { this.setState({ tooltipActive: '' }) } getActionsTemplate () { const {actions, data, page} = this.props return actions.map(action => { let display = this.state.tooltipActive === `${action.action}-${data.id}` let id = `${action.action}-${data.id}` let tooltip = tooltipText[action.action].replace(/{type}/g, page).replace(/{item}/g, data.name return <ActionLinkItem key={`${data.id}-${action.action}`} page={page} action={action.action} data={data} id={id} tooltipText={tooltip} display={display} onMouseEnter={(e) => this.handleHover(e)} onMouseLeave={(e) => this.handleBlur(e)} /> }) } render () { return ( <div className='row'> {this.getActionsTemplate()} </div> ) } } 

At its simplest, tooltip should be absolutely positioned within a positioned element. 简单地说,工具提示应绝对定位在已定位的元素内。

So, add a style of position: relative to the in ActionLinkItem and add a style of `position: absolute to the outer in Tooltip. 因此,在ActionLinkItem中添加position: relative对于position: relative样式,并在工具提示中添加position: relative对于外部的`position:absolute样式。

For added credit, you will want to set a width on your tooltip, and position or center it within the of ActionLinkItem using styles like bottom: 100% . 为了增加功劳,您将需要在工具提示上设置宽度,并使用bottom: 100%类的样式将其定位或居中在ActionLinkItem中。

You can also do some calculations to ensure that your tooltip does not run off the page by moving it left and right if the containing is on the right or left respectively. 您还可以进行一些计算以确保您的工具提示不会在页面上运行,如果包含内容分别位于右侧或左侧,请向左和向右移动工具提示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM