R语言 如何手动输入页码?

8dtrkrch  于 2023-04-27  发布在  其他
关注(0)|答案(2)|浏览(132)

我有一个超过100页的长表。我需要添加一些功能,以便用户可以键入页数,例如50,并轻松转到此页。当需要打开间隔中间的某些页面时,会出现非常长的表的问题。在这种情况下,用户必须单击“上一页”或“下一页”按钮多次。
在下面的例子中,我不能在一次点击打开第8页。

if (interactive()) {
  library(shiny)
  library(DT)
  shinyApp(
    ui = fluidPage(fluidRow(column(12, DTOutput('tbl')))),
    server = function(input, output) {
      output$tbl = renderDT(
        iris, options = list(lengthChange = FALSE)
      )
    }
  )
}
kmpatx3s

kmpatx3s1#

library(shiny)
library(DT)

shinyApp(
  ui = fluidPage(
    fluidRow(
      div(style = "display:inline-block;", 
          numericInput("page", "Go to page:", value = 1, min = 1)),
      div(style = "display:inline-block;", 
          actionButton("gotopage", "Go"))
    ),
    fluidRow(
      column(12, DTOutput('tbl'))
    )
  ),
  server = function(input, output) {
    output$tbl = renderDT({
      datatable(
        iris, 
        callback = JS(c(
          "$('#gotopage').on('click', function(){",
          "  var page = parseInt($('#page').val())-1;",
          "  table.page(page).draw('page');",
          "});"
        ))
      )
    })
  }
)

另一个选择:

library(shiny)
library(DT)

shinyApp(
  ui = fluidPage(
    tags$head(tags$style(".pagination {float: right;}")),
    fluidRow(
      div(id="pagination", 
          div(style = "display:inline-block;", 
              tags$a(id = "first", style = "cursor: pointer;", "First")),
          div(style = "display:inline-block;", 
              tags$a(id = "previous", style = "cursor: pointer;", " Previous")),
          div(style = "display:inline-block;", 
              tags$input(id="page", type="number", class="input-sm", value="1", min="1")
          ),
          div(style = "display:inline-block;", 
              tags$span(id = "of")),
          div(style = "display:inline-block;", 
              tags$a(id = "next", style = "cursor: pointer;", "Next ")),
          div(style = "display:inline-block;", 
              tags$a(id = "last", style = "cursor: pointer;", "Last"))
      )
    ),
    fluidRow(
      column(12, DTOutput('tbl'))
    )
  ),
  server = function(input, output) {
    output$tbl = renderDT({
      datatable(
        iris, options = list(
          dom = "lfrti<'pagination'>", 
          initComplete = JS(c(
            "function(settings, json){",
            "  var table = settings.oInstance.api();",
            "  var pageinfo = table.page.info();",
            "  $('#of').text('of ' + pageinfo.pages);",
            "}"
          ))
        ),
        callback = JS(c(
          "$('div.pagination').append($('#pagination'));",
          "$('#first').on('click', function(){", 
          "  table.page('first').draw('page');",
          "  $('#page').val(1);",
          "});",
          "$('#previous').on('click', function(){", 
          "  table.page('previous').draw('page');",
          "  $('#page').val(table.page.info().page + 1);",
          "});",
          "$('#next').on('click', function(){", 
          "  table.page('next').draw('page');",
          "  $('#page').val(table.page.info().page + 1);",
          "});",
          "$('#last').on('click', function(){", 
          "  table.page('last').draw('page');",
          "  $('#page').val(table.page.info().pages);",
          "});",
          "$('#page').on('change', function(){",
          "  var page = parseInt($('#page').val());",
          "  if(!isNaN(page)){ table.page(page-1).draw('page'); }",
          "});"
        ))
      )
    })
  }
)

vojdkbi0

vojdkbi02#

我想分享一下我自己在寻找pagingType: "input" DT实现方面的经验,如https://jsfiddle.net/qammar/ad4t12r8/2/所演示的
幸运的是,在浏览this official website时,他们解释了如何使用插件。原来有一些插件可用于DT界面,其中一个是pagingType: "input"选项。
按照他们网站上给出的例子,我能够在我的项目中实现它,如下所示:

DT::datatable(
 plugins = "input",
 options = list(
   pagingType = "input"
   )
 )

你可以看到我目前的应用程序的解决方案(抱歉质量不好)是:https://i.stack.imgur.com/3PEUL.gif
下面的代码显示了一个通用示例:

library(shiny)
library(DT)

shinyApp(
  ui = fluidPage(
    fluidRow(
      column(12, DTOutput('tbl'))
    )
  ),
  server = function(input, output) {
    output$tbl = renderDT({
      datatable(
        iris,
        plugins = "input",
        options = list(
          pagingType = "input"
        )
      )
    })
  }
)

然而,最好的答案是纯粹的裂纹!💪

相关问题