Javascript 在R-M模块的DT分页中更改pageType
目前,我正在设计一个R-Shining模块,它生成一个包含分页的Javascript 在R-M模块的DT分页中更改pageType,javascript,r,shiny,datatables,dt,Javascript,R,Shiny,Datatables,Dt,目前,我正在设计一个R-Shining模块,它生成一个包含分页的DT表。默认情况下,DT默认分页类型为“简单数字”。我的目标是将默认分页类型更改为“simple”(请参阅) 虽然我没有找到任何特定于R-shinny的东西,但我发现有一些与我的任务()相关的SO帖子,但不幸的是,我确实没有取得任何进展 我目前的努力是: 我已尝试在UI中插入: 但收到以下错误: jquery.min.js:2 jQuery.Deferred exception: $(...).dataTable is not a
DT
表。默认情况下,DT
默认分页类型为“简单数字”
。我的目标是将默认分页类型更改为“simple”
(请参阅)
虽然我没有找到任何特定于R-shinny的东西,但我发现有一些与我的任务()相关的SO帖子,但不幸的是,我确实没有取得任何进展
我目前的努力是:
jquery.min.js:2 jQuery.Deferred exception: $(...).dataTable is not a function TypeError: $(...).dataTable is not a function
DT::datatable()
objectcallback
选项中包含了这一点: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)
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)
table\u ui=函数(id,name=“table”){
#*使用提供的id创建名称空间函数
ns=闪亮::ns(id)
#*为表视图构建HTML标记列表
标记列表(
闪亮::标记$script(
闪亮::HTML(粘贴0)
$(文档).ready(函数(){
$(“#”,ns(名称),”)。数据表({
“pagingType”:“简单”,
“sPaginationType”:“简单”
});
});'))),
DT::dataTableOutput(outputId=ns(名称))
)
}
table_server=函数(输入、输出、会话、name=“table”){
#*提取数据----
data_df=shinny::reactive({dataset::mtcars})
#*生成HTML表格----
#*注意:将“data_df()”转换为HTML表格
输出[[name]]=DT::renderDataTable({
#*构建HTML表
DT::数据表(
数据_df(),
rownames=FALSE,
选项=列表(
分页=真,
pageLength=5,
dom=“”
)
)
})
#*返回
返回(数据_df)
}
#*创建简单应用程序
#*定义用户界面
ui=闪亮::fluidPage(
表ui(id=“test”,name=“report”),
)
#*定义服务器
服务器=功能(输入、输出、会话){
#*提取文本输入
调用模块(
模块=表\u服务器,
id=“测试”,
会话=会话,
name=“报告”)
}
#*构建应用程序
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(
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)
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)
table\u ui=函数(id,name=“table”){
#*使用提供的id创建名称空间函数
ns=闪亮::ns(id)
#*为表视图构建HTML标记列表
标记列表(
DT::dataTableOutput(outputId=ns(名称))
)
}
table_server=函数(输入、输出、会话、name=“table”){
#*提取数据----
data_df=shinny::reactive({dataset::mtcars})
#*生成HTML表格----
#*注意:将“data_df()”转换为HTML表格
输出[[name]]=DT::renderDataTable({
#*构建HTML表
DT::数据表(
数据_df(),
rownames=FALSE,
选项=列表(
分页=真,
pageLength=5,
dom=“”
),
#**JS支持----
#*注:定义JS修改表
callback=DT::JS(0
//*检查表格
tableObject=table.settings()[0];
console.log(table.settings());
//*分页类型
tableObject.sPaginationType=“简单”;
'))
)
})
#*返回
返回(数据_df)
}
#*创建简单应用程序
#*定义用户界面
ui=闪亮::fluidPage(
表ui(id=“test”,name=“report”),
)
#*定义服务器
服务器=功能(输入、输出、会话){
#*提取文本输入
调用模块(
模块=表\u服务器,
id=“测试”,
会话=会话,
name=“报告”)
}
#*构建应用程序
shiny::shinyApp(ui=ui,server=server)
您不需要求助于JavaScript。只需在选项列表中添加pagingType=“simple”
。谢谢!只是将其应用到示例代码中,它就工作了。