glin / reactable

Interactive data tables for R
https://glin.github.io/reactable
Other
612 stars 79 forks source link

Execute JS after init #310

Open fabiangehring opened 1 year ago

fabiangehring commented 1 year ago

Hi, I'd like to have a datePicker in my table. When using DT I could do something like below. With reactable I am missing the initComplete, preDrawCallback and drawCallback. Is there a reactable-way of including datePickers in a table?

Minimal example from here

library(shiny)
library(DT)

ui <- fluidPage(

  # define a hidden dateInput in order that Shiny loads the dependencies
  div(style = "display: none;",
    dateInput("x", label = NULL)
  ),

  DTOutput("table")
)

js <- c(
  "function(settings){",
  "  $('#calendar').bsDatepicker({",
  "    format: 'yyyy-mm-dd',",
  "    todayHighlight: true",
  "  });",
  "}"
) # available options: https://bootstrap-datepicker.readthedocs.io/en/latest/

server <- function(input, output, session){

  output[["table"]] <- renderDT({
    dat <- data.frame(
      V1 = "A",
      V2 = 99, 
      V3 = '<input id="calendar" type="text" class="form-control" value = "2019-03-08"/>',
      stringsAsFactors = FALSE
    )
    datatable(dat, escape = FALSE,
              options = 
                list(
                  initComplete = JS(js),
                  preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
                  drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } ')
                )
    )
  })

}

shinyApp(ui, server)
glin commented 1 year ago

Hi, I don't have any guidance on including date pickers specifically, but there are ways to get custom JavaScript-based widgets into tables. It's still in need of better documentation, but here are some quick tips for now: