[英]Change pageType in DT pagination in R-Shiny module
目前,我正在設計一個 R-Shiny 模塊,該模塊生成一個包含分頁的DT
表。 默認情況下, DT
默認分頁類型為"simple_numbers"
。 我的目標是將默認分頁類型更改為"simple"
(請參閱DataTable pagingType 文檔)。
雖然我沒有找到任何特定於 R-Shiny 的內容,但我發現了一些與我的任務相關的 SO 帖子( 這是最有幫助的),但不幸的是,我確實沒有取得任何進展。
我目前的嘗試是:
shiny::tags$script(
shiny::HTML(paste0('
$(document).ready(function() {
$("#', ns(name), '").dataTable({
"pagingType": "simple",
"sPaginationType": "simple"
});
});')))
但收到以下錯誤:
jquery.min.js:2 jQuery.Deferred exception: $(...).dataTable is not a function TypeError: $(...).dataTable is not a function
DT::datatable()
對象callback
選項中: callback = DT::JS(paste0('
// * Examine the table
tableObject = table.settings()[0];
console.log(table.settings());
// * Pagination Type
tableObject.sPaginationType = "simple";
'))
然而,雖然檢查器控制台中沒有任何錯誤,但分頁類型不會改變。
所以,在這一點上,我有點困惑,想知道是否有人可以幫助我彌合文檔和 SO 帖子與我當前邏輯之間的差距。
為了幫助回答這個問題,我使用來自 R 的預加載數據創建了一個非常基本的可重復示例,用於說明我在下面的每次嘗試。
嘗試 1:
table_ui = function(id, name = "table") {
# * Create a namespace function using the provided id
ns = shiny::NS(id)
# * Build HTML shiny taglist for table view
shiny::tagList(
shiny::tags$script(
shiny::HTML(paste0('
$(document).ready(function() {
$("#', ns(name), '").dataTable({
"pagingType": "simple",
"sPaginationType": "simple"
});
});'))),
DT::dataTableOutput(outputId = ns(name))
)
}
table_server = function(input, output, session, name = "table") {
# * Extract Data ----
data_df = shiny::reactive({ datasets::mtcars })
# * Produce HTML Table ----
# * NOTE: Transform "data_df()" into HTML table
output[[name]] = DT::renderDataTable({
# * Build HTML table
DT::datatable(
data_df(),
rownames = FALSE,
options = list(
paging = TRUE,
pageLength = 5,
dom = "<f<t>ip>"
)
)
})
# * Return
return(data_df)
}
# * Create simple app
# * Define UI
ui = shiny::fluidPage(
table_ui(id = "test", name = "report"),
)
# * Define Server
server = function(input, output, session) {
# * Extract text input
shiny::callModule(
module = table_server,
id = "test",
session = session,
name = "report")
}
# * Build App
shiny::shinyApp(ui = ui, server = server)
嘗試 2:
table_ui = function(id, name = "table") {
# * Create a namespace function using the provided id
ns = shiny::NS(id)
# * Build HTML shiny taglist for table view
shiny::tagList(
DT::dataTableOutput(outputId = ns(name))
)
}
table_server = function(input, output, session, name = "table") {
# * Extract Data ----
data_df = shiny::reactive({ datasets::mtcars })
# * Produce HTML Table ----
# * NOTE: Transform "data_df()" into HTML table
output[[name]] = DT::renderDataTable({
# * Build HTML table
DT::datatable(
data_df(),
rownames = FALSE,
options = list(
paging = TRUE,
pageLength = 5,
dom = "<f<t>ip>"
),
# ** JS Support ----
# * NOTE: Define JS to modify table
callback = DT::JS(paste0('
// * Examine the table
tableObject = table.settings()[0];
console.log(table.settings());
// * Pagination Type
tableObject.sPaginationType = "simple";
'))
)
})
# * Return
return(data_df)
}
# * Create simple app
# * Define UI
ui = shiny::fluidPage(
table_ui(id = "test", name = "report"),
)
# * Define Server
server = function(input, output, session) {
# * Extract text input
shiny::callModule(
module = table_server,
id = "test",
session = session,
name = "report")
}
# * Build App
shiny::shinyApp(ui = ui, server = server)
您不需要求助於 JavaScript。 只需在選項列表中添加pagingType = "simple"
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.