Ho questo script che utilizza DataTable (http://www.datatables.net/index)
Codice PHP:
<script type="text/javascript">
$(document).ready(function()
{
/*
* Table sorting
*/
// Apply to table
$('.tabella').each(function(i)
{
// DataTable config
var table = $(this),
oTable = table.dataTable({
/*
* We set specific options for each columns here. Some columns contain raw data to enable correct sorting, so we convert it for display
* @url [url]http://www.datatables.net/usage/columns[/url]
*/
aoColumns: [
{ bSortable: false }, // No sorting for this columns, as it only contains checkboxes
{ sType: 'string' },
{ bSortable: false },
{ sType: 'numeric', bUseRendered: false, fnRender: function(obj) // Append unit and add icon
{
return '[size="1"][img]images/icons/fugue/image.png[/img] '+obj.aData[obj.iDataColumn]+' Ko[/size]';
}
},
{ sType: 'date' },
{ sType: 'numeric', bUseRendered: false, fnRender: function(obj) // Size is given as float for sorting, convert to format 000 x 000
{
return obj.aData[obj.iDataColumn].split('.').join(' x ');
}
},
{ bSortable: false } // No sorting for actions column
],
/*
* Set DOM structure for table controls
* @url [url]http://www.datatables.net/examples/basic_init/dom.html[/url]
*/
sDom: '<"block-controls"<"controls-buttons"p>>rti<"block-footer clearfix"lf>',
/*
* Callback to apply template setup
*/
fnDrawCallback: function()
{
this.parent().applyTemplateSetup();
},
fnInitComplete: function()
{
this.parent().applyTemplateSetup();
}
});
// Sorting arrows behaviour
table.find('thead .sort-up').click(function(event)
{
// Stop link behaviour
event.preventDefault();
// Find column index
var column = $(this).closest('th'),
columnIndex = column.parent().children().index(column.get(0));
// Send command
oTable.fnSort([[columnIndex, 'asc']]);
// Prevent bubbling
return false;
});
table.find('thead .sort-down').click(function(event)
{
// Stop link behaviour
event.preventDefault();
// Find column index
var column = $(this).closest('th'),
columnIndex = column.parent().children().index(column.get(0));
// Send command
oTable.fnSort([[columnIndex, 'desc']]);
// Prevent bubbling
return false;
});
});
</script>
Quello che voglio fare è utilizzare questo nell'head dei documenti per inizializzare tutte le tabelle con classe 'tabella' ma la parte 'aoColumns' la voglio eliminare da qui ed inserire solo nelle pagine in cui utilizzerò delle tabelle delle quali fare il sorting in modo di specificare per ogni tabella il tipo di dato che viene utilizzato.
Non so come fare però perchè il datatable risulta già inizializzato per quella tabella.
In pratica ho bisogno di aggiungere solo il comportamento delle colonne:
Codice PHP:
<script type="text/javascript">
$(document).ready(function() {
$('.tabella').dataTable( {
"aoColumns": [
null,
null,
null,
null,
{ "sType": "numeric-comma" }
]
} );
});
</script>
Questo codice mi restituisce questo messaggio:
DataTables warning: Cannot reinitialise DataTable.
To retrieve the DataTables object for this table, please pass either no arguments to the dataTable() function, or set bRetrieve to true. Alternatively, to destory the old table and create a new one, set bDestroy to true (note that a lot of changes to the configuration can be made through the API which is usually much faster).
Il motivo è ovvio, che non posso re-inizializzare data table. Oppure dice di utilizzare bRetrieve a true.
Settandolo a true non ricevo alcun errore ma non aggiunge il comportamento che voglio.