zoukankan      html  css  js  c++  java
  • 基于1.3.3版本tooltip的datagrid单元格tip实现

    基于1.3.3版本tooltip的datagrid单元格tip实现
    2013年05月25日 ⁄ datagrid ⁄ 共 6122字 ⁄ 评论数 26 ⁄ 被围观 7,033 views+

    在Easyui的1.3.3版本中,作者新增了tooltip组件,尽管样式看起来也不咋的,但是终归也是官方出品,同时其功能也算是比较丰富。之前我写过一篇《扩展:datagrid鼠标经过提示单元格内容》那就是用纯编码生成的tip,更为丑陋,有了Easyui 1.3.3的tooltip,我们实现起来就很容易了,直接上代码:

    实现代码

     
    1. $.extend($.fn.datagrid.methods, {      
    2.     /**
    3.      * 开打提示功能    
    4.      * @param {} jq    
    5.      * @param {} params 提示消息框的样式    
    6.      * @return {}    
    7.      */     
    8.     doCellTip:function (jq, params) {      
    9.         function showTip(showParams, td, e, dg) {      
    10.             //无文本,不提示。      
    11.             if ($(td).text() == ""return;      
    12.                
    13.             params = params || {};   
    14.             var options = dg.data('datagrid');      
    15.             showParams.content = '<div class="tipcontent">' + showParams.content + '</div>';      
    16.             $(td).tooltip({      
    17.                 content:showParams.content,      
    18.                 trackMouse:true,      
    19.                 position:params.position,      
    20.                 onHide:function () {      
    21.                     $(this).tooltip('destroy');      
    22.                 },      
    23.                 onShow:function () {      
    24.                     var tip = $(this).tooltip('tip');      
    25.                     if(showParams.tipStyler){      
    26.                         tip.css(showParams.tipStyler);      
    27.                     }      
    28.                     if(showParams.contentStyler){      
    29.                         tip.find('div.tipcontent').css(showParams.contentStyler);      
    30.                     }   
    31.                 }      
    32.             }).tooltip('show');      
    33.      
    34.         };      
    35.         return jq.each(function () {      
    36.             var grid = $(this);      
    37.             var options = $(this).data('datagrid');      
    38.             if (!options.tooltip) {      
    39.                 var panel = grid.datagrid('getPanel').panel('panel');      
    40.                 panel.find('.datagrid-body').each(function () {      
    41.                     var delegateEle = $(this).find('> div.datagrid-body-inner').length ? $(this).find('> div.datagrid-body-inner')[0] : this;      
    42.                     $(delegateEle).undelegate('td', 'mouseover').undelegate('td', 'mouseout').undelegate('td', 'mousemove').delegate('td[field]', {      
    43.                         'mouseover':function (e) {   
    44.                             //if($(this).attr('field')===undefined) return;      
    45.                             var that = this;   
    46.                             var setField = null;   
    47.                             if(params.specialShowFields && params.specialShowFields.sort){   
    48.                                 for(var i=0; i<params.specialShowFields.length; i++){   
    49.                                     if(params.specialShowFields[i].field == $(this).attr('field')){   
    50.                                         setField = params.specialShowFields[i];   
    51.                                     }   
    52.                                 }   
    53.                             }   
    54.                             if(setField==null){   
    55.                                 options.factContent = $(this).find('>div').clone().css({'margin-left':'-5000px', 'width':'auto', 'display':'inline', 'position':'absolute'}).appendTo('body');      
    56.                                 var factContentWidth = options.factContent.width();      
    57.                                 params.content = $(this).text();      
    58.                                 if (params.onlyShowInterrupt) {      
    59.                                     if (factContentWidth > $(this).width()) {      
    60.                                         showTip(params, this, e, grid);      
    61.                                     }      
    62.                                 } else {      
    63.                                     showTip(params, this, e, grid);      
    64.                                 }    
    65.                             }else{   
    66.                                 panel.find('.datagrid-body').each(function(){   
    67.                                     var trs = $(this).find('tr[datagrid-row-index="' + $(that).parent().attr('datagrid-row-index') + '"]');   
    68.                                     trs.each(function(){   
    69.                                         var td = $(this).find('> td[field="' + setField.showField + '"]');   
    70.                                         if(td.length){   
    71.                                             params.content = td.text();   
    72.                                         }   
    73.                                     });   
    74.                                 });   
    75.                                 showTip(params, this, e, grid);   
    76.                             }   
    77.                         },      
    78.                         'mouseout':function (e) {      
    79.                             if (options.factContent) {      
    80.                                 options.factContent.remove();      
    81.                                 options.factContent = null;      
    82.                             }      
    83.                         }      
    84.                     });      
    85.                 });      
    86.             }      
    87.         });      
    88.     },      
    89.     /**
    90.      * 关闭消息提示功能    
    91.      * @param {} jq    
    92.      * @return {}    
    93.      */     
    94.     cancelCellTip:function (jq) {      
    95.         return jq.each(function () {      
    96.             var data = $(this).data('datagrid');      
    97.             if (data.factContent) {      
    98.                 data.factContent.remove();      
    99.                 data.factContent = null;      
    100.             }      
    101.             var panel = $(this).datagrid('getPanel').panel('panel');      
    102.             panel.find('.datagrid-body').undelegate('td', 'mouseover').undelegate('td', 'mouseout').undelegate('td', 'mousemove')      
    103.         });      
    104.     }      
    105. });  

    入参列表

    doCellTip方法的参数包含以下属性:

    名称参数类型描述以及默认值
    onlyShowInterrupt string 是否只有在文字被截断时才显示tip,默认值为false,即所有单元格都显示tip。
    specialShowFields Array 需要特殊定义显示的列,比如要求鼠标经过name列时提示standName列(可以是隐藏列)的内容,specialShowFields参数可以传入:[{field:'name',showField:'standName'}]。
    position string tip的位置,可以为top,botom,right,left。
    tipStyler object tip内容的样式,注意要符合jquery css函数的要求。
    contentStyler object 整个tip的样式,注意要符合jquery css函数的要求。

    使用示例

     
    1. $('#dg').datagrid('doCellTip',   
    2.     {   
    3.         onlyShowInterrupt:false,   
    4.         position:'bottom',   
    5.         tipStyler:{'backgroundColor':'#fff000', borderColor:'#ff0000', maxWidth:'50px', boxShadow:'1px 1px 3px #292929'},   
    6.         contentStyler:{backgroundColor:'#333', paddingLeft:'5px'}   
    7.     });  

    效果演示

    http://www.easyui.info/version/jquery-easyui-1.3.3/demo/datagrid/celltips.html

  • 相关阅读:
    JSON 操作
    生成下面的模块时,启用了优化或没有调试信息
    Emacs
    Integration rules
    Testing tools
    软件架构(读书笔记1)
    依赖于自己做计算
    POJO
    软件架构(读书笔记2)
    设计得不好
  • 原文地址:https://www.cnblogs.com/8090sns/p/3644251.html
Copyright © 2011-2022 走看看