Customizing defaults -- colvis button columns selector

Customizing defaults -- colvis button columns selector

cmoncmon Posts: 1Questions: 0Answers: 0
edited August 2018 in Free community support

Hi everyone :)
I'm developing a CRM (fullstack: Symfony/AdminLTE/DataTables) having several DTs
containing various entities, sometimes with specific filters. All of DTs have to look and
behave the same way, no matter which datatype they hold. It is important to end users
but also to the application architecture and major extensions planned in the near future.

So I have $.fn.dataTable.defaults with many common settings. Each
datatable should have the same buttons, including the custom ones
I defined (eg. reset columns or clear filters trigerring corresponding
events). The clou is the colvis feature. Some of DTs have columns
that have to be hidden from colvis (end-user).

I tried sth like this:

$.extend( true, $.fn.dataTable.defaults, {
    ...
        buttons: [
        'copy', 'pdf', 'excel',
            {
                text: 'Reset',
                ...
            },
            {
                extend: 'colvis',
                text: 'Translation',
                columns: function () {
                    return $('#object').data('colvis');
                }
            }
    ]
});

where an object existing while initialisation of DT is holding a property
data-colvis="[1, 2, 3, 4]" and we can get it as an array (through $().data())
or as a string (by $().attr()).

Dumping settings after DT init shows that columns setting is bound to the
function and the setting "doesn't like" when function returns a string: colvis menu is empty then. With function returning an array, a colvis menu is visible, but... is showing
all the columns.

The same array imprinted with all the buttons settings in the init call is working ok.

Any ideas, my friends? :) I would give it up, but the matter is "all the buttons settings".
There are some and the app is in the alpha-tests. I wouldn't like to repeat all the defs
each time I instantiate a DT. Please, help :)

regards; cmon

Replies

  • allanallan Posts: 61,683Questions: 1Answers: 10,099 Site admin

    Here is a minimal test case showing that.

    Not sure what is going wrong with it to be honest, but it certainly does look wrong. I'll look into it and post back when done.

    Allan

  • allanallan Posts: 61,683Questions: 1Answers: 10,099 Site admin

    Typical - can spend ages looking at it the night before and then first thing in the morning its obvious... The columns option for the colvis button is a column-selector. So as a function it will be called once for each column and should return true or false depending on if the column should be included.

    It was returning a truthy value, which is why all columns were included. Checking if the column index is in the array is the way to do it: http://live.datatables.net/lefuteju/2/edit .

    Allan

This discussion has been closed.