How can I change language option after initialization of Datatable?

How can I change language option after initialization of Datatable?

yadiralizamayadiralizama Posts: 2Questions: 1Answers: 0

Hi,
Allan, first of all, thanks for your amazing datatable.
I am trying to change the language of datatable after initialization. If user changes the language then datatables reload with the new language.

How can I do this without reinitializing table, or how can I save initialization data and reload again with all features and new language?

Thanks for all

Answers

  • allanallan Posts: 63,818Questions: 1Answers: 10,517 Site admin

    There is no option to change the language strings after initialisation - sorry.

    Allan

  • lunatecklunateck Posts: 1Questions: 0Answers: 0
    edited October 2016

    There is a workaround by using jquery to change the text after each draw. Not pretty but works.

    // Declare a message variable
    var message = 'Just replace this message to change the empty table message';
    // Initialize the DataTable with language.emptyTable with a html span (give it a specific classname, e.g. datatables-empty-message
    var yourDataTable = $('#yourDateTable').DataTable({
                    language: {
                        emptyTable: '<span class="datatables-empty-message"></span>'
                    },
                    drawCallback: function (settings) {
                        // Add a drawCallback method that targets the given html span from step 1 and change the text based on the message variable
                        $('.datatables-empty-message').text(message);
                    }
                });
    
    // change the message and redraw the table
    message = 'huehuehue';
    yourDataTable.draw();
    

    I didn't test this specific code so you may need to tweak it if there's any errors. But anyway, hope you get the gist of it.

    @allan, I do hope we get a native api for this. :)

  • allanallan Posts: 63,818Questions: 1Answers: 10,517 Site admin

    For some of them is it relatively easy to do. For others (the length menu for example) its harder and would increase DataTables' code size more than I would like. The reason I've held back from doing this is that I think it should be all or nothing - consider: why should the empty table message be dynamically changeable but not the length menu.

    Having said that, I am starting to think about changing my mind and including an api for the easier ones!

    Allan

This discussion has been closed.