Tooltip doesn't work on datatables child rows
Asked Answered
N

2

6

Bootstrap Tooltip doesn't work on child rows with jQuery DataTables.

With fnDrawCallback it works on usual rows, but it has no effect on child rows, and I have no idea how to make it works.

"fnDrawCallback": function( oSettings ) {
   $('[rel="tooltip"]').tooltip();        
}

See Child rows and fnDrawCallback for reference.

HTML Example:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <link rel="shortcut icon" type="image/ico" href="http://www.datatables.net/favicon.ico">
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=2.0">

    <title>DataTables example - Child rows (show extra / detailed information)</title>
    <link rel="stylesheet" type="text/css" href="../../media/css/jquery.dataTables.css">
    <link rel="stylesheet" type="text/css" href="../resources/syntax/shCore.css">
    <link rel="stylesheet" type="text/css" href="../resources/demo.css">
    <style type="text/css" class="init">

td.details-control {
    background: url('../resources/details_open.png') no-repeat center center;
    cursor: pointer;
}
tr.shown td.details-control {
    background: url('../resources/details_close.png') no-repeat center center;
}

    </style>



    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> 
    <script type="text/javascript" language="javascript" src="../../media/js/jquery.dataTables.js"></script>
    <script type="text/javascript" language="javascript" src="../resources/syntax/shCore.js"></script>
    <script type="text/javascript" language="javascript" src="../resources/demo.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> 
    <script type="text/javascript" language="javascript" class="init">


/* 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>'+
        '<tr>'+
            '<td>Tooltip:</td>'+
            '<td><span rel="tooltip" data-toggle="tooltip" data-placement="top" title="" data-original-title="display the tooltip" class="souligne">text with a tooltip</span></td>'+
        '</tr>'+
    '</table>';
}

$(document).ready(function() {
    var table = $('#example').DataTable( {
        "ajax": "objects.txt",
        "columns": [
            {
                "className":      'details-control',
                "orderable":      false,
                "data":           null,
                "defaultContent": ''
            },
            { "data": "name" },
            { "data": "position" },
            { "data": "office" },
            { "data": "salary" }
        ],
        "fnDrawCallback": function( oSettings ) {
           $('[rel="tooltip"]').tooltip();         
        },      
        "order": [[1, 'asc']]
    } );

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


    </script>
</head>

<body class="dt-example">
    <div class="container">
        <section>
            <h1>DataTables example <span>Child rows (show extra / detailed information)</span></h1>


            <table id="example" class="display" cellspacing="0" width="100%">
                <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>

        </section>

        <h1>Tooltip works outside:</h1>
        <span rel="tooltip" data-toggle="tooltip" data-placement="top" title="" data-original-title="display the tooltip" class="souligne">text with a tooltip</span>       
    </div>



<script type="text/javascript">
$(function() {
    $('[data-toggle="tooltip"]').tooltip(); 
});
</script>

</body>
</html>

AJAX source:

{
  "data": [
    {
      "name": "<span rel=\"tooltip\" data-toggle=\"tooltip\" data-placement=\"top\" title=\"\" data-original-title=\"this is a tooltip\" class=\"souligne\">Tiger Nixon<\/span>",
      "position": "System Architect",
      "salary": "$320,800",
      "start_date": "2011/04/25",
      "office": "Edinburgh",
      "extn": "5421"
    },
    {
      "name": "<span rel=\"tooltip\" data-toggle=\"tooltip\" data-placement=\"top\" title=\"\" data-original-title=\"this is a tooltip\" class=\"souligne\">Test tooltip<\/span>",
      "position": "Accountant",
      "salary": "$162,700",
      "start_date": "2008/11/28",
      "office": "Tokyo",
      "extn": "5407"
    },
    {
      "name": "Brielle Williamson",
      "position": "Integration Specialist",
      "salary": "$372,000",
      "start_date": "2012/12/02",
      "office": "New York",
      "extn": "4804"
    },
    {
      "name": "Herrod Chandler",
      "position": "Sales Assistant",
      "salary": "$137,500",
      "start_date": "2012/08/06",
      "office": "San Francisco",
      "extn": "9608"
    },
    {
      "name": "Rhona Davidson",
      "position": "Integration Specialist",
      "salary": "$327,900",
      "start_date": "2010/10/14",
      "office": "Tokyo",
      "extn": "6200"
    },
    {
      "name": "Michael Bruce",
      "position": "Javascript Developer",
      "salary": "$183,000",
      "start_date": "2011/06/27",
      "office": "Singapore",
      "extn": "5384"
    },
    {
      "name": "Donna Snider",
      "position": "Customer Support",
      "salary": "$112,000",
      "start_date": "2011/01/25",
      "office": "New York",
      "extn": "4226"
    }
  ]
}

You can also see that if you remove:

"fnDrawCallback": function( oSettings ) {
   $('[rel="tooltip"]').tooltip();         
},

usual rows containing a tooltip won't work.

Sources:

Just change the file row_details.html with my html code to see the test case (and add the file objects.txt to the same folder).

Newsworthy answered 27/8, 2015 at 6:17 Comment(0)
I
5

Try providing tooltip as below and not in fnDrawCallBak but on $(document).ready

$('body').tooltip({
    selector: '[rel=tooltip]'
});
Interplead answered 27/8, 2015 at 6:48 Comment(11)
So I removed fnDrawCallBak and I did: $(document).ready(function(){ $('[rel="tooltip"]').tooltip(); }); but it's still the same problem and usual rows doesn't display the tooltipNewsworthy
You haven't replaced fnCallDrawBack with $(document).ready right? This should be outside from dataTables!Interplead
Yes I did it outside datatables, without fnDrawCallBak (only $(document).ready) and with fnDrawCallBak (both in the file)Newsworthy
Can you show the html structure too? So that I can create a demo and cross verify?Interplead
I edited the post above you got all, just download datatables and replace the file with my code. ThanksNewsworthy
@Alex1 You haven't given it properly in document.ready. Check this DEMO. You pasted it as $(function() { $('[data-toggle="tooltip"]').tooltip(); }); whereas data-toggle should have been relInterplead
Thanks, it works perfectly! But I'm wondering why it doesn't work when I do this: $(function() { $('[rel=tooltip]').tooltip(); });Newsworthy
Anytime.. Happy coding.. :)Interplead
Didn't know about the selector option, great answer +1!Panier
@Alex1 since it is a dynamically added element you need kind of event delegation which is how you can do it with tooltip's selector option.. Your code mentioned in comments would definitely work for already placed element.Interplead
@Guruprasad Rao, awesome!Newsworthy
P
8

Use createdRow to initialize tooltips in regular rows.

"createdRow": function(row, data, dataIndex){
    $('[data-toggle="tooltip"]', row).tooltip();
}

Regarding the child rows, modify your code as follows:

// Open this row
row.child( format(row.data()) ).show();
tr.addClass('shown');

// Initialize tooltips in the child row that follows the parent row      
$('[data-toggle="tooltip"]', tr.next('tr')).tooltip();

See the snippet below for code and demonstration.

/* 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>'+
        '<tr>'+
            '<td>Tooltip:</td>'+
            '<td><span rel="tooltip" data-toggle="tooltip" data-placement="top" title="" data-original-title="display the tooltip" class="souligne">text with a tooltip</span></td>'+
        '</tr>'+      
    '</table>';
}
 
$(document).ready(function() {
  
    var data = [{"name":"<span rel=\"tooltip\" data-toggle=\"tooltip\" data-placement=\"top\" title=\"\" data-original-title=\"this is a tooltip\" class=\"souligne\">Tiger Nixon</span>","position":"System Architect","salary":"$320,800","start_date":"2011/04/25","office":"Edinburgh","extn":"5421"},{"name":"<span rel=\"tooltip\" data-toggle=\"tooltip\" data-placement=\"top\" title=\"\" data-original-title=\"this is a tooltip\" class=\"souligne\">Garrett Winters</span>","position":"Accountant","salary":"$170,750","start_date":"2011/07/25","office":"Tokyo","extn":"8422"},{"name":"<span rel=\"tooltip\" data-toggle=\"tooltip\" data-placement=\"top\" title=\"\" data-original-title=\"this is a tooltip\" class=\"souligne\">Ashton Cox</span>","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>
Panier answered 27/8, 2015 at 8:48 Comment(1)
plus 1 for your efforts too as this also works well.. :)Interplead
I
5

Try providing tooltip as below and not in fnDrawCallBak but on $(document).ready

$('body').tooltip({
    selector: '[rel=tooltip]'
});
Interplead answered 27/8, 2015 at 6:48 Comment(11)
So I removed fnDrawCallBak and I did: $(document).ready(function(){ $('[rel="tooltip"]').tooltip(); }); but it's still the same problem and usual rows doesn't display the tooltipNewsworthy
You haven't replaced fnCallDrawBack with $(document).ready right? This should be outside from dataTables!Interplead
Yes I did it outside datatables, without fnDrawCallBak (only $(document).ready) and with fnDrawCallBak (both in the file)Newsworthy
Can you show the html structure too? So that I can create a demo and cross verify?Interplead
I edited the post above you got all, just download datatables and replace the file with my code. ThanksNewsworthy
@Alex1 You haven't given it properly in document.ready. Check this DEMO. You pasted it as $(function() { $('[data-toggle="tooltip"]').tooltip(); }); whereas data-toggle should have been relInterplead
Thanks, it works perfectly! But I'm wondering why it doesn't work when I do this: $(function() { $('[rel=tooltip]').tooltip(); });Newsworthy
Anytime.. Happy coding.. :)Interplead
Didn't know about the selector option, great answer +1!Panier
@Alex1 since it is a dynamically added element you need kind of event delegation which is how you can do it with tooltip's selector option.. Your code mentioned in comments would definitely work for already placed element.Interplead
@Guruprasad Rao, awesome!Newsworthy

© 2022 - 2024 — McMap. All rights reserved.