英文:
How to show the rowGroup count in a collapsed DT in R Shiny?
问题
我在查看这个之前的问题https://stackoverflow.com/questions/59896704/collapse-rowgroup-shiny以及提供的解决方案。
library(shiny)
library(DT)
ui <- fluidPage(# Application title
titlePanel("Collapse/Expand table"),
mainPanel(DTOutput("my_table"))
)
callback_js <- JS(
"table.on('click', 'tr.dtrg-group', function () {",
" var rowsCollapse = $(this).nextUntil('.dtrg-group');",
" $(rowsCollapse).toggleClass('hidden');",
"});",
"table.one('init', () => $('#my_table .dtrg-group').trigger('click'))"
)
server <- function(input, output) {
output$my_table <- DT::renderDT({
datatable(
mtcars[1:15, 1:5],
extensions = 'RowGroup',
options = list(rowGroup = list(dataSrc = 2), pageLength = 20),
callback = callback_js,
selection = 'none'
)
})
}
我成功改进了JS代码,以便在表格的唤醒视图中进行了折叠。在此示例中,数据根据dataSrc = 2进行折叠,对应于mtcars表格的第2列,即'cyl'列。我需要的是,当表格折叠时,能够看到每个cyl类别的汽车数量。我该如何做到这一点?
英文:
I'm looking at this previous question https://stackoverflow.com/questions/59896704/collapse-rowgroup-shiny and the solution provided.
library(shiny)
library(DT)
ui <- fluidPage(# Application title
titlePanel("Collapse/Expand table"),
mainPanel(DTOutput("my_table")))
callback_js <- JS(
"table.on('click', 'tr.dtrg-group', function () {",
" var rowsCollapse = $(this).nextUntil('.dtrg-group');",
" $(rowsCollapse).toggleClass('hidden');",
"});",
"table.one('init', () => $('#my_table .dtrg-group').trigger('click'))"
)
server <- function(input, output) {
output$my_table <- DT::renderDT({
datatable(
mtcars[1:15, 1:5],
extensions = 'RowGroup',
options = list(rowGroup = list(dataSrc = 2), pageLength = 20),
callback = callback_js,
selection = 'none'
)
})
}
# Run the application
shinyApp(ui = ui, server = server)
I managed to enhance the JS such that the wakeup view of this table is collapsed. Data in this example is collapsed based off dataSrc = 2 that corresponds to the 2nd column of the mtcards table, that is, the 'cyl' column. What I need: I would need to be able to see the count of cars for each cyl category when the table is collapsed. How can I do that?
答案1
得分: 2
以下是您提供的代码的中文翻译部分:
这里有一个示例,其中每个类别的计数写入折叠的行组,通过编辑相应的 innerHTML
来实现。您可以通过将以下 JS
添加到回调中来实现这一点。
table.on('init', () => $('#my_table .dtrg-group').each(function(i, obj) {
var grpcount = $(this).nextUntil('.dtrg-group').length.toString();
this.innerHTML = this.innerHTML.replace('</th>', ', count: ' + grpcount + '</th>');
}));
library(shiny)
library(DT)
ui <- fluidPage(# Application title
titlePanel("折叠/展开表格"),
mainPanel(DTOutput("my_table"))
)
callback_js <- JS(
"table.on('click', 'tr.dtrg-group', function () {",
" var rowsCollapse = $(this).nextUntil('.dtrg-group');",
" $(rowsCollapse).toggleClass('hidden');",
"});",
"table.one('init', () => $('#my_table .dtrg-group').trigger('click'))",
"table.on('init', () => $('#my_table .dtrg-group').each(function(i, obj) {",
" var grpcount = $(this).nextUntil('.dtrg-group').length.toString();",
" this.innerHTML = this.innerHTML.replace('</th>', ', count: ' + grpcount + '</th>');",
"}));"
)
server <- function(input, output) {
output$my_table <- DT::renderDT({
datatable(
mtcars[1:15, 1:5],
extensions = 'RowGroup',
options = list(
rowGroup = list(dataSrc = 2),
pageLength = 20
),
callback = callback_js,
selection = 'none'
)
})
}
# 运行应用程序
shinyApp(ui = ui, server = server)
请注意,这只是代码的翻译部分,没有其他内容。
英文:
Here is one example where the count of each category is written into the collapsed row groups by editing the corresponding innerHTML
. You can get this by adding the following JS
to your callback.
table.on('init', () => $('#my_table .dtrg-group').each(function(i, obj) {
var grpcount = $(this).nextUntil('.dtrg-group').length.toString();
this.innerHTML = this.innerHTML.replace('</th>', ', count: ' + grpcount + '</th>');
}));"
library(shiny)
library(DT)
ui <- fluidPage(# Application title
titlePanel("Collapse/Expand table"),
mainPanel(DTOutput("my_table")))
callback_js <- JS(
"table.on('click', 'tr.dtrg-group', function () {",
" var rowsCollapse = $(this).nextUntil('.dtrg-group');",
" $(rowsCollapse).toggleClass('hidden');",
"});",
"table.one('init', () => $('#my_table .dtrg-group').trigger('click'))",
"table.on('init', () => $('#my_table .dtrg-group').each(function(i, obj) {",
" var grpcount = $(this).nextUntil('.dtrg-group').length.toString();",
" this.innerHTML = this.innerHTML.replace('</th>', ', count: ' + grpcount + '</th>');",
"}));"
)
server <- function(input, output) {
output$my_table <- DT::renderDT({
datatable(
mtcars[1:15, 1:5],
extensions = 'RowGroup',
options = list(
rowGroup = list(dataSrc = 2),
pageLength = 20
),
callback = callback_js,
selection = 'none'
)
})
}
# Run the application
shinyApp(ui = ui, server = server)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论