gpt4 book ai didi

jquery - R Shiny - 通过列排序禁用数据表中的特定行

转载 作者:行者123 更新时间:2023-12-01 04:35:38 26 4
gpt4 key购买 nike

下面的应用包含启用了行选择的 iris 数据集的数据表。我想专门禁用前 3 行的选择。我可以使用发布的解决方案here来做到这一点。当表在应用程序启动时初始化时,该解决方案工作正常:

enter image description here

但是,当您对列上的行进行排序时,例如在 Species 上按降序排列,它会禁用观测值 101、102 和 103,因为排序后它们现在是表的前 3 行:

enter image description here

我猜测发生这种情况是因为 rowCallbackdisplayIndex 来禁用行。因此,在启动时,displayIndex 0、1 和 2 分别对应于 iris 数据集中的观测值 1、2 和 3,并且行回调会禁用它们的行选择,因为 indices.indexOf(displayIndex) >对于这些行,-1true。但在对 Species 进行排序后,displayIndex 0、1 和 2 分别对应于观测值 101、102 和 103,因此回调会禁用它们。

为了解决此问题,我尝试通过将 rowCallback 函数中的 displayIndex 更改为 dataIndex 来禁用基于行名称的行。但是,这会造成一些奇怪的情况,每次我在不同的列上进行筛选时,附加行都会被禁用。以下是表格首先按 Sepal.Length 排序,然后按 Sepal.Length 排序,最后按 Petal.Length 排序后的示例:

enter image description here

这是重现上述内容的代码:

library(DT)
library(shiny)

disabled_rows = c(1,2,3)

#NOTE: displayIndex changed to dataIndex
rowCallback <- c(
"function(row, data, displayNum, dataIndex){",
sprintf(" var indices = [%s]", toString(disabled_rows - 1)),
" if(indices.indexOf(dataIndex) > -1){",
" $(row).find('td').addClass('notselectable').css({'background-color': '#eee', 'color': '#bbb'});",
" }",
"}"
)

get_selected_rows <- c(
"var id = $(table.table().node()).closest('.datatables').attr('id');",
"table.on('click', 'tbody', function(){",
" setTimeout(function(){",
" var indexes = table.rows({selected:true}).indexes();",
" var indices = Array(indexes.length);",
" for(var i = 0; i < indices.length; ++i){",
" indices[i] = indexes[i];",
" }",
" Shiny.setInputValue(id + '_rows_selected', indices);",
" }, 0);",
"});"
)

drag_selection <- c(
"var dt = table.table().node();",
"$(dt).selectable({",
" distance : 10,",
" selecting: function(evt, ui){",
" $(this).find('tbody tr').each(function(i){",
" if($(this).hasClass('ui-selecting')){",
" table.row(i).select();",
" }",
" });",
" }",
"}).on('dblclick', function(){table.rows().deselect();});"
)

shinyApp(
ui = fluidPage(
tags$head(tags$script(src = "https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js")),
DTOutput('table')
),

server = function(input, output, session) {

output$table <- renderDT({

datatable(
iris,
callback = JS(get_selected_rows),
class = 'hover row-border order-column',
options = list(
rowCallback = JS(rowCallback),
select = list(style = "multi", selector = "td:not(.notselectable)")
),
extensions = "Select", selection = 'none'
)
}, server = F)

observe({

print(input$table_rows_selected)

})
}
)

我不知道为什么将 displayIndex 更改为 dataIndex 不起作用,而且我不知道还可以尝试什么。 dataIndex 参数的 DataTables 定义 here由于我对 DT 插件不太熟悉,所以对我来说很模糊,所以我将不胜感激。

编辑:我还尝试直接根据行名禁用,如下所示:

rowCallback <- c(
"function(row, data, displayNum, displayIndex){",
" var indices = [0, 2, 4, 15]",
" if(indices.indexOf(data[0]) > -1){",
" $(row).find('td').addClass('notselectable');",
" }",
"}"
)

最佳答案

奇怪的是dataIndex不起作用。

您可以使用一些行 ID 来代替。

disabled_rows = paste0("'", paste0("row", c(1,2,3)), "'")

rowCallback <- c(
"function(row, data, displayNum, displayIndex){",
sprintf(" var indices = [%s];", toString(disabled_rows)),
" if(indices.indexOf($(row).attr('id')) > - 1){",
" $(row).find('td').addClass('notselectable').css({'background-color': '#eee', 'color': '#bbb'});",
" }",
"}"
)

dat <- iris
dat$ID <- paste0("row", 1:nrow(iris))
rowNames <- TRUE
colIndex <- as.integer(rowNames)


output$table <- renderDT({

datatable(
dat,
rownames = rowNames,
callback = JS(get_selected_rows),
class = 'hover row-border order-column',
options = list(
rowId = JS(sprintf("function(data){return data[%d];}",
ncol(dat)-1+colIndex)),
rowCallback = JS(rowCallback),
select = list(style = "multi", selector = "td:not(.notselectable)")
),
extensions = "Select", selection = 'none'
)
}, server = TRUE)

关于jquery - R Shiny - 通过列排序禁用数据表中的特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57505250/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com