Wijmo5 FlexGrid教程(18)- 实现添加ToolTip提示功能

发布时间:2016/07/14 00:07 发布者:Alice

返回博客中心

Wijmo5提供了tooltip,可以在页面上的元素弹出展示额外信息。

它通常有两种模式:

自动模式:使用setTooltip方法,链接ToolTip和页面上的元素。Tooltip就会监视事件,自动展示。

比如:

var tt = new wijmo.Tooltip();
tt.setTooltip('#menu', 'Select commands.');
tt.setTooltip('#tree', 'Explore the hierarchy.');
tt.setTooltip('#chart', '#idChartTooltip');

 

手动模式:在事件里通过show和hide方法来展示和隐藏Tooltip。

比如:

var tt = new wijmo.Tooltip();
element.addEventListener('click', function () {
  if (tt.isVisible) {
    tt.hide();
  } else {
    tt.show(element, 'This is an important element!');
  }
});

 

自动模式非常好理解,本文就来介绍如何通过手动模式给flexgrid设置Tooltip。

这里需要调用相关的鼠标事件,mouseover和mouseout事件。在mouseover的时候显示tooltip,在mouseout的时候隐藏tooltip。

 

代码参考:

  // monitor the mouse over the grid            
flex.hostElement.addEventListener('mousemove', function (evt) {                
var ht = flex.hitTest(evt);              
 if (!ht.cellRange.equals(rng)) {                   
 // new cell selected, show tooltip                  
  if (ht.cellType == wijmo.grid.CellType.Cell) {                       
 rng = ht.cellRange;                       
 var cellElement = document.elementFromPoint(evt.clientX, evt.clientY),                            
cellBounds = wijmo.Rect.fromBoundingRect(cellElement.getBoundingClientRect()),                           
 data = wijmo.escapeHtml(flex.getCellData(rng.row, rng.col, true)),                           
 tipContent = 'cell (' + rng.row + ' ' + rng.col + ') contains "<b>' + data + '</b>"';                       
 if (cellElement.className.indexOf('wj-cell') > -1) {                            
tip.show(flex.hostElement, tipContent, cellBounds);                        
} 
else {                            
tip.hide(); 
// cell must be behind scroll bar...                        
}                    
}               
 }           
 });            

flex.hostElement.addEventListener('mouseout', function () {               
 tip.hide();               
 rng = null;           
 });

 

当鼠标hover到单元格上,效果如图:

image

本文的源代码请下载:

更多资源:

Wijmo中文官网:/developer/wijmojs

Wijmo5在线示例:/developer/wijmojscore

Wijmo5产品文档:/developer/wijmojscore

如果依然有问题,可以到我们的官方产品论坛发帖咨询:http://gcdn.grapecity.com.cn/showforum-140.html

请参考Wijmo5技术文章汇总

《wijmo5 flexgrid基础教程》系列文章


关于葡萄城

赋能开发者!葡萄城是专业的集开发工具、商业智能解决方案、低代码开发平台于一身的软件和服务提供商,为超过 75% 的全球财富 500 强企业提供服务。葡萄城专注控件软件领域30年,希望通过模块化的开发控件、灵活的低代码应用开发平台等一系列开发工具、解决方案和服务,帮助开发者快速响应复杂多变的业务需求,最大程度地发挥开发者的才智和潜能,让开发者的 IT 人生更从容更美好。

了解详情,请访问葡萄城官网