2012-07-03 7 views
5

Jak zmodyfikować kod, aby wyświetlić informacje o każdej komórce w podpowiedzi?Jak wyświetlić końcówkę narzędzia dla każdej komórki?

http://datatables.net/release-datatables/examples/advanced_init/events_post_init.html

$(document).ready(function() { 
/* 
* First step is to create title attributes for the rows in the table 
* This isn't needed if the required 'title' attribute is already set in the HTML in the 
* DOM 
*/ 
$('#example tbody tr').each(function() { 
var sTitle; 
var nTds = $('td', this); 
var sBrowser = $(nTds[1]).text(); 
var sGrade = $(nTds[4]).text(); 

if (sGrade == "A") 
sTitle = sBrowser+' will provide a first class (A) level of CSS support.'; 
else if (sGrade == "C") 
sTitle = sBrowser+' will provide a core (C) level of CSS support.'; 
else if (sGrade == "X") 
sTitle = sBrowser+' does not provide CSS support or has a broken implementation. Block CSS.'; 
else 
sTitle = sBrowser+' will provide an undefined level of CSS support.'; 

this.setAttribute('title', sTitle); 
}); 

/* Init DataTables */ 
var oTable = $('#example').dataTable(); 

/* Apply the tooltips */ 
oTable.$('tr').tooltip({ 
"delay": 0, 
"track": true, 
"fade": 250 
}); 
}); 

Odpowiedz

2

Można ustawić tytuł przez prostu setAttribute dla każdego td

$('#example tbody tr td').each(function() { 
    this.setAttribute('title', $(this).text()); 
}); 

i wywołać podpowiedź na td

oTable.$('td').tooltip({ 
"delay": 0, 
"track": true, 
"fade": 250 
}); 
+0

Yup. Jeśli treść podpowiedzi nie ma być tekstem, teoretycznie można pobrać inne informacje z innych źródeł (na przykład atrybut danych), używając zasadniczo tej samej techniki. –

9

Można zrobić

{ "sTitle": "...", ... 
    'fnCreatedCell': function(nTd, sData, oData, iRow, iCol) { 
     nTd.title = 'Some more information'; 
    } 
} 

w konfiguracji kolumn. Możesz łatwo wykorzystać wszystkie dane wiersza. Przyczyna tego nie może być:

oTable.$('td').tooltip({ 
    "delay": 0, 
    "track": true, 
    "fade": 100 
}); 
+1

Fantastyczna mała wskazówka! Ostatnio pracowałem w/datatables i jest niesamowicie solidny. Moje jedyne pytanie, jak to działa? Dlaczego ustawia się tytuł wystarczający do ujawnienia etykiety narzędzi? Jestem tym zdezorientowany. – rkd80

+0

Hi rkd80, tooltip to komponent jQuery UI (https://jqueryui.com/tooltip/), który odczyta atrybut title. "oTable. $ ('td'). tooltip (..." mówi komponentowi podpowiedzi, aby pokazać etykietę narzędzi dla każdego elementu td w tabeli. –

Powiązane problemy