javascript - 使用 jQuery 将工具提示添加到 DataTable 中的 TD

标签 javascript jquery datatables

我有一个数据表,我通过 JavaScript 为其绑定(bind)数据:

$("#TableUsage").DataTable({
                data: infolist(),
                ordering: true,
                paginate: false,
                "info": false,
                columns: [
                    { data: "Product", title: "Product" },
                    { data: "Serial", title: "Serial" },
                    { data: "App", title: "App" },
                ]});

信息列表看起来像:

infolist.push({Product: "01", Serial: "05", App: "M07", AppCol: "App info P01"})
infolist.push({Product: "02", Serial: "08", App: "M03", AppCol: "App info P02"})
infolist.push({Product: "03", Serial: "03", App: "M09", AppCol: "App info P03"})

通常,在 HTML 中我只写:

<td data-bind="text: $data.App, attr: {title: $data.AppCol}">App</td>

当鼠标悬停在“App”单元格值上时,AppCol 信息会显示在工具提示内。

有没有办法添加工具提示,但使用 Javascript 代码,因为现在 HTML 中的表格看起来像这样?

<table class="table" style="width: 100%;" id="TableUsage"></table>

更新!

JSFiddle附:)

最佳答案

好的,您可以通过为列定义渲染函数来实现,这样您就可以输出 HTML 进行显示并返回原始数据进行排序/过滤。您需要将以下内容添加到 DataTable 选项对象中:

columnDefs: [{
      targets: 2,
      render: function(data, type, full, meta) {
        if(type === 'display') {
           // Return element with title and value.  This is only returned for display
           return '<div title="' + full.AppCol + '">' + data + '</div>';
        }

        // Return raw data for sorting and filtering
        return data;
      }
    }],
    drawCallback: function(settings) {
      //Wire up tool tip here if the library won't automatically pick up new elements.
      // This fires after the draw event is completed so the DataTable is in the DOM
      // With all of it's rows.
    }

在绘制回调中,如果工具提示库不会自动拾取新的网格行,您可以连接它。

关于javascript - 使用 jQuery 将工具提示添加到 DataTable 中的 TD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40724567/

相关文章:

javascript - datatables-editable 插件在数据表中动态添加行后不起作用

javascript - 如何对数据表中的容量列进行排序

jquery - 同一页面中不同版本的 Jquery Datatable

javascript - 关闭 Bootstrap 模式后重置验证消息

javascript - 当文本字段中有文本时使提交按钮处于事件状态 - jquery

javascript - css3 动画和 jquery(悬停翻转卡)的视觉问题

jquery 动态添加的复选框不适用于 Change() 函数

javascript - 如何在node.js中同步连接mssql服务器

php - 我可以在没有 javascript 的情况下只加载页面的一部分吗?

javascript - 在下拉列表中设置选项值