2012-07-03 4 views
5

Come posso modificare il codice per visualizzare le informazioni di ciascuna cella in un suggerimento?Come visualizzare la descrizione dell'utensile per ogni cella?

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

risposta

2

È possibile impostare il titolo semplicemente setAttribute per ogni td

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

e chiamare tooltip sul td

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

Sì. E se il contenuto del tooltip non è inteso come il testo, potresti teoricamente recuperare altre informazioni da altrove (ad esempio, un attributo dati) usando essenzialmente la stessa tecnica. –

9

Si può fare

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

nella configurazione della colonna. Puoi usare tutti i dati di riga in questo modo. Di sicuro, questo non deve mancare:

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

Fantastico piccolo consiglio lì! Ho lavorato w/datatables di recente, ed è incredibilmente robusto. La mia unica domanda, come funziona davvero? Perché è sufficiente impostare il titolo per esporre un suggerimento? Sono confuso da questo. – rkd80

+0

Hi rkd80, tooltip è un componente dell'interfaccia utente jQuery (https://jqueryui.com/tooltip/) che leggerà l'attributo title. "oTable. $ ('td'). tooltip (..." dice al componente tooltip di mostrare il tooltip per ogni elemento td nella tabella. –

Problemi correlati