rstudio / DT

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

Retain the pagination on editing the cells; don't want datatable to refresh and reset to page 1 on editing #864

Open aman-malik3010 opened 3 years ago

aman-malik3010 commented 3 years ago

What's happening: On editing any cell from page 2 or 3 the data table refreshing and moves back to page 1. Also, the editable field gets blank on editing. What is needed: Retain the data after editing as well as the pagination. Also to keep the filter functionality.

library(shiny) 
library(DT) 

page_length <- 2 

hardcoded_df <- read.table(text = "Fruit Color
                                   Apple Red
                                   Plum Purple
                                   Blueberry Duh
                                   Orange Carrot
                                   Crocodile Green
                                   Grapes Green",
                           header = TRUE)

ui <- fluidPage(
  DT::dataTableOutput('x1')
)

server <- function(input, output, session) {
  x = reactiveValues(df = hardcoded_df)

  output$x1 = renderDT(DT::datatable(x$df, filter = list(position = 'top', clear = FALSE) ,
                                     options = list(pageLength = page_length), selection = 'none', editable = TRUE))

  proxy = dataTableProxy('x1')

  data = reactiveValues()
  observeEvent(input$x1_cell_edit, {
    info = input$x1_cell_edit
    str(info)

    i = info$row
    j = info$col
    v = info$value

    View(x$df)

    x$df[i, j] <- DT::coerceValue(v, x$df[i, j])
    DT::replaceData(proxy, x$df, resetPaging = FALSE)  
  })
}

shinyApp(ui = ui, server = server)

By filing an issue to this repo, I promise that

I understand that my issue may be closed if I don't fulfill my promises.

andysw90 commented 2 years ago

Did you find any solution to this?

hyfzi commented 2 months ago

you need change x$df to isolate(x$df) in this code -》"output$x1 = renderDT(DT::datatable(x$df, filter = list(position = 'top', clear = FALSE) ,options = list(pageLength = page_length), selection = 'none', editable = TRUE))"。But i meet another problem, if you add selectinput condition, this will useless!!