rstudio / DT

R Interface to the jQuery Plug-in DataTables
https://rstudio.github.io/DT/
Other
598 stars 181 forks source link

Add support for list of classes in `DT2BSClass` #1089

Closed pedropark99 closed 1 year ago

pedropark99 commented 1 year ago

This PR seeks to bring support for a list of classes in DT2BSClass(), with the end goal of fixing the issue #1052.

In more details, when you select style='bootstrap', DT::datatable() tries to translate the class argument, to a css rule with all the Bootstrap classes that were mentioned in the class argument. To make this translation, the DT2BSClass() function is called with the class argument. However, the DT2BSClass() function only supports a character vector as input.

This PR will allow DT2BSClass() to receive a list of booleans as input, as mentioned in the example from issue #1052.

# Now these examples below works
DT::datatable(mtcars, class=list(stripe=F), style = "bootstrap")
DT::datatable(mtcars, class=list(stripe=T, compact=F, hover=T), style = "bootstrap")

In essence, we use base::Filter() to select the elements of the list that are TRUE, and simply extract the name of these filtered elements.

  if (is.list(class)) {
    chosen_classes = Filter(function(x) isTRUE(x), class)
    class = names(chosen_classes)
  }
CLAassistant commented 1 year ago

CLA assistant check
All committers have signed the CLA.

pedropark99 commented 1 year ago

Thank you @yihui for the review! I updated NEWS.md with the changes, and, also changed the source code to use which() instead of Filter() as you suggested 😉