Dynamically change names in ColVis - Reinit

Dynamically change names in ColVis - Reinit

khrmkhrm Posts: 13Questions: 4Answers: 0
edited April 2022 in ColVis

Regarding this topic:
https://datatables.net/forums/discussion/31748/dynamic-column-names-in-colvis-buttons

There is a solution to change the text of one line manually. But as in the topic meant, how is it possible to reinitalize Colvis after changing dynamically Column Names in Table Header. Is there a function to reinitialize colvis, sothat it greps all of the current columns.
Or maybe to delete the entries and add new entries by Class Name.

Answers

  • colincolin Posts: 15,118Questions: 1Answers: 2,583

    Sorry, I'm not clear what the issue is. We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • khrmkhrm Posts: 13Questions: 4Answers: 0
    edited April 2022

    Sorry. I will try to explain:

    Here, i changed the column text manually, like in other thread described:

    table.columns( ".changetext" ).every( function(idx, t,i){
    $(this.header()).text("Feld "+ (i+1) )
    })

    While i do this, i also want to change the text in the Colvis Dropdown.
    The Dropdown-Menu will be generated on click and takes the title of the column, which is being defined on initialisation.

    The question is, how to access the column-settings to update the "title" attribute.

  • colincolin Posts: 15,118Questions: 1Answers: 2,583

    I see, that's only changing the text in the HTML so, yep, DataTables wouldn't know about it. You would need to make that change when the table is being initialised for the column.title,

    Colin

  • khrmkhrm Posts: 13Questions: 4Answers: 0

    Thank you.

    Is there any possibility to change the column.title after initialization and to re-render the table then? So that there is some dynamically solution for that.

  • colincolin Posts: 15,118Questions: 1Answers: 2,583

    Yep, that is possible, you can use the colvis columnText option to label the columns - something like this -

    $(document).ready(function() {
        var table = $('#example').DataTable();
    
        $('#press').on('click', function() {
            table.columns().every(function(idx, t, i) {
                $(this.header()).text("Feld " + (i + 1))
            })
    
            new $.fn.dataTable.Buttons(table, {
                buttons: [{
                    extend: 'colvis',
                    columnText: function(dt, i, title) {
                        return $(dt.column(i).header()).text();
                    }
                }]
            });
    
            table.buttons(0, null).container().prependTo(
                table.table().container()
            );
        })
    
    });
    
    

    Colin

Sign In or Register to comment.