JA-pythonista
JA-pythonista

Reputation: 1323

How to add tooltip to a particular column that contains missing cells in datatables

I am using DataTables, and I am interested in adding tooltip to a column that contains missing cells. I wanted to add a simple note as to why those cells in that column are missing. The column I am talking about is the 7th column.

A simple inspection to the missing cells in the column for one table cell looks like this:

enter image description here

Here is my script:

<script> 

$(document).ready(function() {
    $(".se-pre-con").fadeOut("slow");


           $('table thead tr').clone(true).appendTo( 'table thead' );
           $('thead tr:eq(1) th').each( function (i) {
               if(i > 4){
                   $(this).hide()
               }
           })

            $('table').DataTable( {

                fixedHeader: true,

                language: {
                    processing:     "Bitte warten ..",
                    search:         "Suchen",
                    lengthMenu:    "_MENU_ Einträge anzeigen",              
                    info:           "_START_ bis _END_ von _TOTAL_ Einträgen",
                    infoEmpty:      "Keine Daten vorhanden",
                    infoFiltered:   "(gefiltert von _MAX_ Einträgen)",
                    infoPostFix:    "",
                    loadingRecords: "Wird geladen ..",
                    zeroRecords:    "Keine Einträge vorhanden",
                    paginate: {
                        first:      "Erste",
                        previous:   "Zurück",
                        next:       "Nächste",
                        last:       "Letzte"}
                    },

                   initComplete: function () {

                       this.api().columns().every( function () {

                           var column = this;

                           console.log(column.index())
                           var select = $('<select><option value=""></option></select>')
                               .appendTo( $(column.header()).empty() )
                               .on( 'change', function () {
                                   var val = $.fn.dataTable.util.escapeRegex(
                                       $(this).val()
                                   );

                                   column
                                       .search( val ? '^'+val+'$' : '', true, false )
                                       .draw();
                               } );

                           column.data().unique().sort().each( function ( d, j ) {
                               select.append( '<option value="'+d+'">'+d+'</option>' )
                           } );
                       } );
                   }
               } );

       } );

</script> 

Upvotes: 1

Views: 604

Answers (2)

davidkonrad
davidkonrad

Reputation: 85538

Dont know what kind of tooltip you are using, so I just address the way you would attach a Bootstrap tooltip to the <td>. Look at createdCell -> https://datatables.net/reference/option/columns.createdCell

Add a createdCell callback to your columns / columnDefs section. Since it seems like you are using a static <table> without any column definitions you can do this :

columnDefs: [{ 
  targets: 6, //columns are zero based
  createdCell: function(td, cellData) {
    if (cellData === '') {
      $(td).tooltip({ trigger: 'hover', title: 'The cell is empty because ...' })
    }
  }
}],

Upvotes: 2

user13581509
user13581509

Reputation:

I don't know for sure what kind of tooltip is meant here, if its a reference to an official jQuery tooltip or otherwise, but in order to modify that which has no cells, simply get a reference to the table, then loop through table.rows, and by each row element, check the number of cells, and if it has none, then add a tooltip to that row. Example psuedo code:

table = document.querySelector("table");
Array.apply(0, table.rows).forEach(x => {
    x.cells.length == 0 && (functionToMakeTooltipToRow(x))
});

Upvotes: 0

Related Questions