Collapse DT table columns by default

As per below the documentation (https://rstudio.github.io/DT/extensions.html), the below code should collapse first and second columns by default(0,1), but when tried it is displaying all columns by default.

Can we have only 2 columns displayed by default?

datatable(
        iris2, rownames = FALSE,
        extensions = 'Buttons', options = list(
          dom = 'Bfrtip',
          buttons = list(list(extend = 'colvis', columns = c(2, 3, 4)))
        )
      )

this is a misunderstanding, the example and description show that this code adds a column visibility button that allows some chosen columns to be hidden by pressing their buttons in the column vis butto; in this example the 1st column can not be hidden as it has been left off the list.

Here is an amendment to the code which shows adjusting the initial hidden columns.

library(DT)

datatable(
  iris, rownames = FALSE,
  extensions = 'Buttons', options = list(
    dom = 'Bfrtip',
    buttons = list(list(extend = 'colvis', columns = c(0,1,2,3,4))) # all 5 are hideable/showable by the button
     ,columnDefs = list(list(visible=FALSE, targets=c(0,1))  # first two start hidden
  )
))
1 Like

Awesome thank a lot. Last clarification, Can we add another feature named "ALL" in the drop down so that when the user selects all, all columns should be visible.

In other words, just to avoid user selecting each column one by one. Can this be achieved ?:slight_smile:


datatable(
  iris, rownames = FALSE,
  extensions = 'Buttons', options = list(
    dom = 'Bfrtip',
    buttons = list(list(extend = 'colvis', columns = c(0,1,2,3,4)),
                   list(extend= "colvisGroup",text="Show All",show=":hidden") # show all button
                   ) # all 5 are hideable/showable by the button
    ,columnDefs = list(list(visible=FALSE, targets=c(0,1))  # first two start hidden
    )
  ))

Great thanks :slight_smile: Just wan to check, is it not possible to collapse and extend in same button?

I dont know how to do that

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.