Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datatables change language dynamically

I'm trying to change with jQuery language of a table with datatables. I'm trying to push a button to change the language of the table.

$('#prueba').live('click', function () {
var espanol = {
     "sProcessing": "Procesando...",
     "sLengthMenu": "Mostrar _MENU_ registros",
     "sZeroRecords": "No se encontraron resultados",
     "sInfo": "Mostrando desde _START_ hasta _END_ de _TOTAL_ registros",
     "sInfoEmpty": "No existen registros",
     "sInfoFiltered": "(filtrado de un total de _MAX_ líneas)",
     "sInfoPostFix": "",
     "sSearch": "Buscar:",
     "sUrl": "",
"oPaginate": {
"sFirst":    "Primero",
"sPrevious": "Anterior",
"sNext":     "Siguiente",
"sLast":     "Último"
}
};
tablacliente.fnSettings().oLanguage= espanol;
tablacliente.fnDraw();
})
like image 245
faisbu Avatar asked May 09 '26 21:05

faisbu


1 Answers

AFAIK, there is no built-in method or plug-in (currently) to switch the language dynamically. But what you can do is destroy the datatable and re-initialize it with the new language setting.

So, change your button's click handler to something like this:

$('#prueba').click(function(){
    if (typeof tablacliente != 'undefined' && tablacliente != null)
    {
        tablacliente.fnDestroy(); //important! you have to destroy first or you'll get an alert-error.
        tablacliente = null;
        tablacliente = $('#table_id').dataTable( {"oLanguage": espanol} ); //don't forget to include any other settings, if you have.
    }
});

Here is a demo on jsFiddle.

like image 139
Onur Yıldırım Avatar answered May 12 '26 13:05

Onur Yıldırım