【问题标题】:Tooltip does not work with pagination on datatables工具提示不适用于数据表上的分页
【发布时间】:2016-11-16 16:00:35
【问题描述】:

在数据表插件中使用分页时如何显示我的工具提示? 当列内的文本太长时,我正在使用与数据表相关的插件 protip 来显​​示工具提示。 tooltips 插件已经可以与以下 sn-p 一起使用:

//Datatable Setup
jQuery(document).ready(function($) {
var table = $('#irp-table.raab').DataTable({
    "columnDefs": [
        { visible: false, targets: 2 },
        { className: 'mdl-data-table__cell--non-numeric', targets: [0, 1]}
    ],
    "order": [[ 0, 'asc' ]],
    "displayLength": 25,
    "drawCallback": function ( settings ) {
        var api = this.api();
        var rows = api.rows( {page:'current'} ).nodes();
        var last=null;

        api.column(2, {page:'current'} ).data().each( function ( group, i ) {
            if ( last !== group ) {
                $(rows).eq( i ).before(
                    '<tr class="group"><td colspan="3">'+group+'</td></tr>'
                );

                last = group;
            }
        } );
    }
} );

//Initialize ToolTip
jQuery(document).ready(function($) {
$.protip();
});

//ToolTip hover behaviour
jQuery(document).ready(function($) {
$('td').bind('mouseenter', function () {
  var $this = $(this);
    if (this.offsetWidth < this.scrollWidth) {

      var text = $this.text();
      $this.attr("data-pt-title", text);
      $this.protipShow()
    }
}).mouseenter();
});

但是,对于我在数据表上使用分页并导航到另一个站点的情况,它仅适用于第一个站点。

【问题讨论】:

    标签: javascript jquery pagination datatables


    【解决方案1】:

    解决方案

    DataTables 每次重绘表格时都需要使用drawCallback 来初始化工具提示。这是必需的,因为在显示第一页时,DOM 中不存在除第一页以外的页面的 TRTD 元素。

    也不需要调用mouseenter()

    例如:

    "drawCallback": function ( settings ) {
       var api = this.api();
    
       // ... skipped ...
    
       $.protip();
    
       $('td', api.table().container()).on('mouseenter', function () {
          var $this = $(this);
          if (this.offsetWidth < this.scrollWidth) {
             var text = $this.text();
             $this.attr("data-pt-title", text);
             $this.protipShow();
          }
       });
    }
    

    链接

    有关更多示例和详细信息,请参阅jQuery DataTables: Custom control does not work on second page and after

    【讨论】:

    • 嗨@gyrocode.com,你似乎是对的。我还在stackoverflow上找到了另一个答案,有人遇到了同样的问题,而您的建议显示了方法。但是,我正在努力让它最终发挥作用。你可以检查我的小提琴吗?会显示工具提示,但在第二页上会显示每个提示,而不会将项目悬停。 jsfiddle.net/w6zL3kqt
    • @DoUtDes,需要去掉mouseenter()调用,见jsfiddle.net/w6zL3kqt/1
    • 非常感谢!我正在搜索 protip 插件的整个文档以找到错误。感谢您再次回复以澄清解决方案,尽管这是 javascript 而不是原始主题的一般问题!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-21
    • 1970-01-01
    • 1970-01-01
    • 2013-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多