R Shiny-多页可编辑数据表在编辑后跳至第1行 [英] R Shiny - multi-page editable DataTable jumps to row #1 after an edit
问题描述
我正在使用R 3.3.1,Shiny v.1.2.0和DT 0.5版开发Shiny应用程序。元素之一是跨多个页面的可编辑数据表。在我进行编辑之后,焦点所在的行跳到了第一行,这破坏了用户体验。
I am developing a Shiny app using R 3.3.1, Shiny v. 1.2.0 and v. DT 0.5. One of the elements is an editable data table that spans multiple pages. After I make an edit the row in focus jumps to row #1 which kind of ruins the user experience.
以下是使用以下代码段重现此内容的具体步骤:
Here are the specific steps to reproduce this using the snippet below:
- 加载应用程序
- 切换到数据表的第2页
- 编辑第3行,第2列:将Duh更改为Blue,然后按Tab键。
- 观看当前行跳至第1页第1行。这将更容易查看是否有每页还有更多行。
- Load the app
- Switch to page 2 of the data table
- Edit row 3, column 2: change Duh to Blue and press Tab
- Watch the current row jump to page 1 row 1. This would be easier to see if the were many more rows per page.
我在第4步中得到的不是理想的行为。我希望数据表将焦点集中在我刚编辑的同一行上。
What I get in step 4 is not the desirable behavior. I want the data table to keep the focus on the same row I just edited.
我愿意使用自定义JS逻辑来完成这项工作。
I am open to using custom JS logic to make this work.
看似相关的问题- DataTable不记住编辑后的分页页面,但是在这个特定示例中,我不知道如何从R桥接到JS。
Seemingly related question - DataTable doesn't remember paginated page after edit but I do not know how to bridge from R to JS in this particular example.
R.version.string
# "R version 3.3.1 (2016-06-21)"
library(shiny) # v. 1.2.0
library(DT) # v. 0.5
page_length <- 2 # 5 elements should span 3 pages
hardcoded_df <- read.table(text = "Fruit Color
Apple Red
Plum Purple
Blueberry Duh
Orange Carrot
Crocodile Green",
header = TRUE,
stringsAsFactors = FALSE)
ui <- fluidPage(
DT::dataTableOutput('x1')
)
server <- function(input, output) {
x = reactiveValues(df = hardcoded_df)
output$x1 = renderDT(DT::datatable(x$df, options = list(pageLength = page_length), selection = 'none', editable = TRUE))
proxy = dataTableProxy('x1')
observeEvent(input$x1_cell_edit, {
info = input$x1_cell_edit
str(info)
# str(input$x1_state)
i = info$row
j = info$col
v = info$value
# Without this line the table does not change but with it it jumps to row 1 after an edit.
x$df[i, j] <- isolate(DT::coerceValue(v, x$df[i, j]))
# Now we need to scroll to row i somehow ... clearly this does not work. Help!
selectPage(proxy, ceiling(i / page_length))
# selectRow(proxy, i)
})
}
# Run the application
shinyApp(ui = ui, server = server)
推荐答案
在这种情况下, DT :: replaceData
与 resetPaging = FALSE
应该可以正常工作,如此处。但是,将 x
定义为 reactiveValues()
会导致一些问题,我使用 isolate
In this situation DT::replaceData
with resetPaging = FALSE
should work fine as shown here. However, defining x
as a reactiveValues()
cause some problems which I solved using isolate
server <- function(input, output, session) {
x = reactiveValues(df = hardcoded_df)
output$x1 = renderDT(DT::datatable(isolate(x$df),
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)
# str(input$x1_state)
i = info$row
j = info$col
v = info$value
# Without this line the table does not change but with it it jumps to row 1 after an edit.
x$df[i, j] <- isolate(DT::coerceValue(v, x$df[i, j]))
DT::replaceData(proxy, x$df, resetPaging = FALSE) # important
# Now we need to scroll to row i somehow ... clearly this does not work. Help!
#selectPage(proxy, ceiling(i / page_length))
# selectRow(proxy, i)
})
}
这篇关于R Shiny-多页可编辑数据表在编辑后跳至第1行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!