自动滚动到数据的右侧

发布于 2025-02-12 00:37:18 字数 388 浏览 2 评论 0原文

有什么方法可以在R Shiny中以将滚动条设置为从数据呈现的滚动条时自动在渲染时(即到最后一列)将滚动条设置为右侧,而不是默认设置的左侧?

基本示例:

library(shiny)
runApp(shinyApp(
  ui = fluidPage(
    DT::dataTableOutput("results", width = 300)
  ),
  server = function(input, output, session) {
    output$results <- DT::renderDataTable(
      mtcars,
      options = list(scrollX = TRUE, bPaginate = F)
    )
  }
))

Is there any way, in R Shiny, to set the scroll bar from a datatable automatically to the right side when rendered (i.e. to the last column), instead of the left side as it is set by default ?

Base example :

library(shiny)
runApp(shinyApp(
  ui = fluidPage(
    DT::dataTableOutput("results", width = 300)
  ),
  server = function(input, output, session) {
    output$results <- DT::renderDataTable(
      mtcars,
      options = list(scrollX = TRUE, bPaginate = F)
    )
  }
))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

廻憶裏菂餘溫 2025-02-19 00:37:18
library(shiny)
runApp(shinyApp(
  ui = fluidPage(
    tags$div(id = "parent",
      DT::dataTableOutput("results", width = 300)
    ),
    tags$style("
               #parent {direction: rtl; max-height: 80vh; overflow: auto; margin: 0 auto} 
               #results {direction: ltr; float: left}")
  ),
  server = function(input, output, session) {
    output$results <- DT::renderDataTable(
      mtcars,
      options = list(scrollX = TRUE, bPaginate = F)
    )
  }
))

在这里编辑

是您想要的:

library(shiny)
library(DT)

callback <- "
$('div.dataTables_scrollBody').animate({scrollLeft: '+=500'}, 1000);
"

runApp(shinyApp(
  ui = fluidPage(
    DTOutput("results", width = 300)
  ),
  server = function(input, output, session) {
    output$results <- renderDT(
      datatable(
        mtcars, 
        callback = JS(callback),
        options = list(scrollX = TRUE)
      )
    )
  }
))

1000是1000ms,这是动画的持续时间。我不明白为什么 += 300还不够。

library(shiny)
runApp(shinyApp(
  ui = fluidPage(
    tags$div(id = "parent",
      DT::dataTableOutput("results", width = 300)
    ),
    tags$style("
               #parent {direction: rtl; max-height: 80vh; overflow: auto; margin: 0 auto} 
               #results {direction: ltr; float: left}")
  ),
  server = function(input, output, session) {
    output$results <- DT::renderDataTable(
      mtcars,
      options = list(scrollX = TRUE, bPaginate = F)
    )
  }
))

EDIT

Here is what you want:

library(shiny)
library(DT)

callback <- "
$('div.dataTables_scrollBody').animate({scrollLeft: '+=500'}, 1000);
"

runApp(shinyApp(
  ui = fluidPage(
    DTOutput("results", width = 300)
  ),
  server = function(input, output, session) {
    output$results <- renderDT(
      datatable(
        mtcars, 
        callback = JS(callback),
        options = list(scrollX = TRUE)
      )
    )
  }
))

1000 is 1000ms, this is the duration of the animation. I don't understand why +=300 is not enough.

℡Ms空城旧梦 2025-02-19 00:37:18

在UI中添加此行:tags $ style('#result {Direction {rtl;}')

library(shiny)
runApp(shinyApp(
    ui = fluidPage(
        DT::dataTableOutput("results", width = 300),
        tags$style('#results {direction: rtl;}')
    ),
    server = function(input, output, session) {
        output$results <- DT::renderDataTable(
            mtcars,
            options = list(scrollX = TRUE, bPaginate = F)
        )
    }
))

Add this line in the ui: tags$style('#results {direction: rtl;}')

library(shiny)
runApp(shinyApp(
    ui = fluidPage(
        DT::dataTableOutput("results", width = 300),
        tags$style('#results {direction: rtl;}')
    ),
    server = function(input, output, session) {
        output$results <- DT::renderDataTable(
            mtcars,
            options = list(scrollX = TRUE, bPaginate = F)
        )
    }
))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文