Datatables change language dynamically

前端 未结 4 692
一整个雨季
一整个雨季 2021-01-06 13:04

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(         


        
相关标签:
4条回答
  • 2021-01-06 13:41

    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.

    0 讨论(0)
  • 2021-01-06 13:42

    As the original poster mentioned, this does not work:

    tablacliente.fnSettings().oLanguage = espanol;    // does not work!
    

    but something like this should work, without having to destroy the table:

    var oLanguage = tablacliente.fnSettings().oLanguage;
    for (var field in espanol) {
        oLanguage[field] = espanol[field];
    }
    
    0 讨论(0)
  • 2021-01-06 13:50

    Try this:

    tablacliente.fnSettings().oLanguage= espanol; tablacliente.fnUpdate();

    Works for me.

    0 讨论(0)
  • 2021-01-06 13:52
    <?php
    $countries = array (
        "tr-TR"=>"//cdn.datatables.net/plug-ins/1.10.16/i18n/Turkish.json",
        "de-DE" =>"//cdn.datatables.net/plug-ins/1.10.16/i18n/German.json",
        "es-ES"=>"//cdn.datatables.net/plug-ins/1.10.16/i18n/Spanish.json"
    );
    ?>
    <script>
        var locale='<?php echo $countries[locale_get_default()];?>';
        $(document).ready(function () {
            $('#page-params').dataTable({
                responsive: true,
                "oLanguage": {
                    "sUrl": locale
                }
            });
        });
    </script>
    
    0 讨论(0)
提交回复
热议问题