Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

218
Visualizações
Two JS functions for initComplete option in Datatable in Shiny

I try to combine two javascript functions into one variable so that I can call both in the DT::datatable initComplete option. The functions are working independently of each other, but when I combine them the table is not rendered.

JS function 1:

rownumber <- 8

jsc <- paste0("function() {
                           $(this.api().table().row(", rownumber, ").node()).addClass('selected');
                           this.api().table().row(", rownumber, ").node().scrollIntoView();
                          }")

JS function #2:

jsc2 <- "function(settings, json) {$(this.api().table().header()).css({'background-color': '#2c3e50', 'color': '#fff'});}"

JS function #3 (the combined one):

jsc3 <- paste0("function() {
                           $(this.api().table().row(", rownumber, ").node()).addClass('selected');
                           this.api().table().row(", rownumber, ").node().scrollIntoView();
                          }
               function(settings, json) {$(this.api().table().header()).css({'background-color': '#2c3e50', 'color': '#fff'});}")

When I run the last one with the code below, I do not get the desired result of a colored header row and that the focused is scrolled to the 8th row.

mtcars %>% datatable(
  options = list(
    dom = 't',
    paging = FALSE,
    scrollX = TRUE,
    scroller = TRUE, 
    paging = TRUE,
    initComplete = JS(jsc3)
)
)

I appreciate if somebody can assist on how to combine those two functions into a line of code that will render properly in the DT::datatable.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

initComplete needs one and only one JS callback. In your jsc3, you defined two functions. This cannot be accepted. You need to merge two functions to one, like below:

library(DT)

rownumber <- 8

jsc3 <- paste0("function() {
                           $(this.api().table().row(", rownumber, ").node()).addClass('selected');
                           this.api().table().row(", rownumber, ").node().scrollIntoView();
                           $(this.api().table().header()).css({'background-color': '#2c3e50', 'color': '#fff'});
                          }
               ")

mtcars %>% datatable(
    options = list(
        dom = 't',
        paging = FALSE,
        scrollX = TRUE,
        scroller = TRUE, 
        paging = TRUE,
        initComplete = JS(jsc3)
    )
)

enter image description here

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda