【问题标题】:Add tooltip to expand child row button in Datatables在数据表中添加工具提示以展开子行按钮
【发布时间】:2015-09-04 13:47:34
【问题描述】:

我想在加号/减号绿色/红色按钮中添加工具提示,请参阅 this example。我怎样才能做到这一点?

【问题讨论】:

  • 这样<td class="sorting_1" title="Tooltip text for that element">Airi</td>

标签: javascript html datatables tooltip


【解决方案1】:

解决方案

使用下面的代码:

$('body').tooltip({
   selector: 'td.details-control',
   title: 'Click to expand',
   container: 'body',
   placement: 'right'
});  

演示

/* Formatting function for row details - modify as you need */
function format ( d ) {
    // `d` is the original data object for the row
    return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">'+
        '<tr>'+
            '<td>Full name:</td>'+
            '<td>'+d.name+'</td>'+
        '</tr>'+
        '<tr>'+
            '<td>Extension number:</td>'+
            '<td>'+d.extn+'</td>'+
        '</tr>'+
    '</table>';
}
 
$(document).ready(function() {
  
  $('body').tooltip({
    selector: 'td.details-control',
    title: 'Click to expand',
    container: 'body',
    placement: 'right'
  });  
  
    var data = [{"name":"Tiger Nixon","position":"System Architect","salary":"$320,800","start_date":"2011/04/25","office":"Edinburgh","extn":"5421"},{"name":"Garrett Winters","position":"Accountant","salary":"$170,750","start_date":"2011/07/25","office":"Tokyo","extn":"8422"},{"name":"Ashton Cox","position":"Junior Technical Author","salary":"$86,000","start_date":"2009/01/12","office":"San Francisco","extn":"1562"}];
  
    var table = $('#example').DataTable( {
        "data": data,
        "columns": [
            {
                "className":      'details-control',
                "orderable":      false,
                "data":           null,
                "defaultContent": ''
            },
            { "data": "name" },
            { "data": "position" },
            { "data": "office" },
            { "data": "salary" }
        ],
        "order": [[1, 'asc']],
        "createdRow": function(row, data, dataIndex){
           $('[data-toggle="tooltip"]', row).tooltip();
        }
    } );
     
    // Add event listener for opening and closing details
    $('#example tbody').on('click', 'td.details-control', function () {
        var tr = $(this).closest('tr');
        var row = table.row( tr );
 
        if ( row.child.isShown() ) {
            // This row is already open - close it
            row.child.hide();
            tr.removeClass('shown');
        }
        else {
            // Open this row
            row.child( format(row.data()) ).show();
            tr.addClass('shown');
            
            $('[data-toggle="tooltip"]', tr.next('tr')).tooltip();
        }
    } );
 
} );
td.details-control {
    background: url('https://raw.githubusercontent.com/DataTables/DataTables/1.10.7/examples/resources/details_open.png') no-repeat center center;
    cursor: pointer;
}
tr.shown td.details-control {
    background: url('https://raw.githubusercontent.com/DataTables/DataTables/1.10.7/examples/resources/details_close.png') no-repeat center center;
}
<link href="//cdn.datatables.net/1.10.7/css/jquery.dataTables.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
<script src="//cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js"></script>

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> 

<table id="example" class="display">
<thead>
    <tr>
        <th></th>
        <th>Name</th>
        <th>Position</th>
        <th>Office</th>
        <th>Salary</th>
    </tr>
</thead>

<tfoot>
    <tr>
        <th></th>
        <th>Name</th>
        <th>Position</th>
        <th>Office</th>
        <th>Salary</th>
    </tr>
</tfoot>
</table>

【讨论】:

  • 使用您的解决方案,您可以编辑列,但在我的项目中我不能这样做,因为我已经制作了一个呈现数据表的角度 js 指令。我使用响应选项,因此当我隐藏一行或有隐藏行时,会自动生成加号/减号按钮。有没有办法我可以为这个按钮设置一个 id/class 以添加工具提示?
  • @DarkAngeL,诀窍是我正在使用selector 选项,它会自动将此工具提示添加到所有数据表中。只需使用答案中的代码,您不需要任何额外的代码。
猜你喜欢
  • 1970-01-01
  • 2017-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-20
相关资源
最近更新 更多