JFernandez
JFernandez

Reputation: 265

Observe datatable pagination event Shiny

I need to change the value of an input every time that the user changes the page of a dataset pagination.

I've tried to use the observeEvent, but it doesn't work.

UI

fluidRow(
  column(10, 
         ""
  ), 
  column(2,              
         textInput("inText", "Input text 2", value = "Default text")
  ),
  column(12,
         dataTableOutput('table')
  )
)

Server

observeEvent(input$table, {
  updateTextInput(session, "inText", value = paste("New text",0))
})

Hope you can help me.

Upvotes: 2

Views: 888

Answers (1)

Tonio Liebrand
Tonio Liebrand

Reputation: 17689

Assuming your table id is table as given in your example, you can use: input$table_state$start / input$table_state$length + 1.

In the following a complete example:

library(DT)
library(shiny)
app <- shinyApp(
  ui = fluidPage(
    tags$head(
      # hides the default search functionality
      tags$style(
        HTML(".dataTables_filter, .dataTables_info { display: none; }")
      )
    ),
    fluidRow(
      column(10, 
             ""
      ), 
      column(2,              
             # adding new page filter
             uiOutput("pageFilter")
      ),
      column(12,
             dataTableOutput('table')
      )
    )
  ),
  server = function(input, output) {

    output$pageFilter <- renderUI({
      val <- input$table_state$start / input$table_state$length + 1
      numericInput("page", "Page", val, min = 1)
    })

    output$table <- DT::renderDataTable({
      iris
    }, options = list(pageLength = 5, stateSave = TRUE))

    # using new page filter
    observeEvent(input$page, {
      dataTableProxy("table") %>% selectPage(input$page)
    })
  }
)

runApp(app, launch.browser =  TRUE)

Upvotes: 2

Related Questions