我试图让datatable (DT)在地图上的某个点上盘旋时滚动到正确的行/行。我可以选择行,但我不知道如何使datatable Y-滚动到正确的条目。我试图在不分页的情况下做到这一点,因为我的datatable使用子集,而且总体上没有那么大。
我在R/Shiny/DT中这样做,我的javascript体验是0。这篇文章似乎试图做同样的事情,只是这个例子是一个初始的表加载,而不是对最初加载的表之外的事件作出反应。我不明白如何使表滚动响应用户事件。
R Shiny - Scrolling to a given row of datatable with javascript callback
该产品用于内部(学术),使数据操作和错误检查更快。
#Attempt at datatable scrollable, workable example
library(shiny)
library(leaflet)
library(DT)
data("mtcars")
samp_data = cbind(mtcars,mtcars,mtcars)
samp_data[2] = 'just some string thats often quite long and will take multiple lines on a table draw,
one problem I have is that if I do add "scroller = T" to the options list in the renderDT call,
is that this line will collapse into a single line and create a very long horizonal entry.'
ui = fillPage(
fillRow( flex = 1, width = '100%', height = '80%',
leafletOutput("map", width = '100%', height = "100%")
),
fillRow( width = '100%', height='20%', flex = 1,
# style = "overflow-y: auto; overflow-x: auto;",
div( DTOutput('db_info', width="100%", height="100%"), style = "font-size:85%")
)
)
server = shinyServer(function(input, output, session) {
output$map = renderLeaflet( leaflet(options = leafletOptions(zoomControl = FALSE)) %>% addTiles() %>% addMiniMap(zoomLevelFixed=2, toggleDisplay=T) )
# output$map = renderLeaflet( leaflet(options = leafletOptions(zoomControl = FALSE)) %>% addTiles() %>% addMiniMap(zoomLevelFixed=2, toggleDisplay=T) )
#For some reason the first draw is wrong
output$db_info <- renderDT( samp_data,
class = 'cell-border stripe',
# extensions = 'Scroller',
rownames = F,
server = F,
options = list(
dom = 't', # dom 't' is a specific option
autoWidth = T,
scrollX = T,
scrollY = '15vh',
scrollCollapse = T,
# scroller = T,
# paging = F,
# initComplete = JS('function() {
# $(this.api().table().row(4)).addClass("selected");
# this.api().table().row(4).scrollTo();}'),
columnDefs = list(list(
width = '500px', targets = c(1) #sets width of citations, need autoWidth=T and scrollX=T to work
))# END columnDefs
)# End options list
)# END RENDER DATA TABLE
#some event on map doesn't really matter for example, hardcode for now
#observeEvent({
#This is where I would like to scroll to the appropriate row in the datatable
# dataTableProxy("db_info", session = session)
# selectRows(c(4,6)) %>%
# selectPage(which(input$db_info_rows_all == c(4,6)) %/% input$db_info_state$length + 1)
# callback = JS('.scroller.toPosition( 4 );')
# dataTableProxy('db_info', session=session) %>% selectRows(c(5,6))
#})
})
shinyApp(ui, server)我添加了一个示例脚本,在这里我一直在尝试一些解决方案。然而,我没有得到任何成功的暗示。最后,当用户单击或悬停在地图上的对应点时,我希望自动切换到表的部分。如果我能处理好与表的交互,我以后就可以很容易地实现它。在这个例子中,我实际上没有使用观察者,我只是想看看我是否能让事情发生。
谢谢你的帮助!
发布于 2018-11-01 00:53:38
由于我无法解决这个问题,考虑到我似乎需要事先知道表的长度,然后才能滚动它(并分页),所以我不得不接受另一种方法。
observeEvent( input$map_marker_mouseover, {
marker_id = input$map_marker_mouseover$id
dataTableProxy("db_info", session = session) %>%
updateSearch(keywords = list(global = marker_id , columns = "acc_num"))
})
observeEvent( input$map_marker_mouseout, {
dataTableProxy("db_info", session = session) %>%
clearSearch()
})简单地说,我将表的唯一标识符设置为标记的$id,当鼠标移动到某个点时,它会自动对唯一ID列进行搜索,这将使它只将表子集为该记录。当清除时,它会清除搜索,以便表重新出现。我用以下选项制作桌子:
output$db_info <- renderDT( sp_pts,
class = 'cell-border table-condensed table-hover',
rownames = FALSE,
server = F,
options = list(
dom = 'tf',
autoWidth = T,
scrollX = T,
scrollY = '15vh',
scrollCollapse = T,
searching = T,
paging = F,
columnDefs = list(list(
width = '500px',
targets = c(pt_cite_loc)
))# END columnDefs
)# End options list
)# END RENDER DATA TABLEhttps://stackoverflow.com/questions/52492179
复制相似问题