获取表格左侧滚动位置

问题描述 投票:0回答:1

我正在尝试让 DT 表的左侧滚动位置变得闪亮。

这是一个示例应用程序,展示了我如何尝试捕获它。

library(shiny)
library(DT)

js_save_scroll <- '$(document).on("shiny:connected", function(settings, json) {
  console.log("Test log");
  $("#tbl").on("click", function() {
    console.log("Table clicked");
    var table = $("#tbl");
    var scrollPos = $("#tbl").scrollLeft();
    console.log("Scroll position: " + scrollPos);
  });
});
'

df <- data.frame(matrix(rnorm(1000), nrow = 100, ncol = 50))

shinyApp(
  ui = fluidPage(
    tags$script(js_save_scroll),
    
    DTOutput('tbl')
    ),
  
  server = function(input, output) {
    output$tbl = renderDT(df)
  }
)

当我在浏览器中检查控制台时,scrollPos 始终设置为零。

javascript r shiny dt
1个回答
0
投票

我认为这符合您的要求。

library(shiny)
library(DT)

js_save_scroll <- '$(document).on("shiny:connected", function(settings, json) {
  $("#tbl").on("click", function() {
    var scroll_x = $(document).scrollLeft();
    var scroll_y = $(document).scrollTop();
    console.log("Scroll position: " + scroll_x + ", " + scroll_y);
  });
});
'

df <- data.frame(matrix(rnorm(1000), nrow = 100, ncol = 50))

shinyApp(
  ui = fluidPage(
    tags$script(js_save_scroll),
    
    DTOutput('tbl')
  ),
  
  server = function(input, output) {
    output$tbl = renderDT(df)
  }
)
© www.soinside.com 2019 - 2024. All rights reserved.