javascript - 工具提示不适用于数据表上的分页

标签 javascript jquery pagination datatables

在数据表插件中使用分页时如何显示工具提示? 当列内的文本太长时,我将插件 protip 与数据表结合使用来显示工具提示。工具提示插件已经可以使用以下代码片段:

//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();
});

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

最佳答案

解决方案

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

也不需要调用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了解更多示例和详细信息。

关于javascript - 工具提示不适用于数据表上的分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40636645/

相关文章:

javascript - 如果内容大于屏幕高度,弹出窗口始终显示为固定在顶部边缘但会随着整个页面滚动?

c# - 如何在警报 javascript 中正确显示此字符串

css - 动态加载的文本被切成两半

html - 理解css中的类

jquery - 如何使用jquery查找当前幻灯片的数据

javascript - 如何显示获取[object object]的Json数据?

javascript - jQuery 未在 Electron 应用程序的 web View 中加载

javascript - HTML5 之前和之后比较 slider

java - 需要关于是否保留此代码的建议

javascript - Windows 8 JS 应用程序中的 jQuery