Customize ColVis menu layout (Bootstrap 4).

Customize ColVis menu layout (Bootstrap 4).

washuit-iammwashuit-iamm Posts: 133Questions: 55Answers: 2

I have reviewed the ColVis button docs here https://datatables.net/reference/button/colvis

Outside of columnText, and columns I don't see an easy way to truly customize the menu.

Some examples of things id like to do:

  • Sort the column text in the menu differently than the column order on the table
  • Group some column buttons next to each other

I have 50+ columns on there so users are having to scroll to get to the bottom of the dropdown.

I tried to search the forums for an answer but you can image how muddied the results get with keywords like "ColVis Columns Button Extensions Sort Order Customize Dropdown"

Answers

  • washuit-iammwashuit-iamm Posts: 133Questions: 55Answers: 2

    Just stumbled on thishttps://datatables.net/release-datatables/examples/api/show_hide.html via https://datatables.net/forums/discussion/8129

    I think that about answers it. Ill have to do a custom collection button id imagine. Id like to see the source code for the colvis button if possible

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395
  • washuit-iammwashuit-iamm Posts: 133Questions: 55Answers: 2

    @tangerine thanks but if you read my post you would see the very first line is me mentioning this documentation.

    I realize the forums are packed full of incredibly basic questions from people who do not or cant find the correct documentation.

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    edited February 2019

    Hi @washuit-iamm ,

    Yep, as you say, those customisations (columnText and columns) are the main places to customise the ColVis button. Bear in mind though you could just create groups with colvisGroup, see the example here, these don't have to be real groups, they could just be single columns reordered and retitled as you want.

    Cheers,

    Colin

  • washuit-iammwashuit-iamm Posts: 133Questions: 55Answers: 2
    edited February 2019

    @colin this is the solution I ended up with, although I am not super happy with it because its so global.

                initComplete: function (settings, json) {
                    var table = this.api();            
    
                    // Sort colvis menu
                    if (table.button('.buttons-columnVisibility').length > 0) {
    
                        let $colVisButtonCollectionContainer = $(table.button('.buttons-columnVisibility').node()).parent();
    
                        $colVisButtonCollectionContainer.children('[data-cv-idx]')
                            .sort(function (a, b) {
                                return $(a).text().toUpperCase().localeCompare($(b).text().toUpperCase());
                            })
                            .appendTo($colVisButtonCollectionContainer);
    
                    }
                }
    

This discussion has been closed.