【问题标题】:Reset a DT table to the original sort order将 DT 表重置为原始排序顺序
【发布时间】:2021-01-02 18:56:02
【问题描述】:

我有一个闪亮的应用程序,它带有一个允许列排序的 DT 表。替换数据时,我还想删除用户可能单击的任何列排序。但是,当数据被替换时,它会自动根据表中指定的排序进行排序。我看不到任何将排序重置为replaceData 函数的一部分或作为独立代理函数的选项。是否可以在不重新渲染表格的情况下做到这一点?

library(DT)
library(shiny)

ui <- fluidPage(
    DTOutput(outputId = "table"),
    actionButton(inputId = "replace", label = "Replace Data")
)

server <- function(input, output) {
    
    output$table <- renderDT({
        datatable(data = data.frame(COL_1 = c(1, 3, 2)), rownames = FALSE)
    })
    
    observeEvent(input$replace, {
        
        data <- data.frame(COL_1 = c(4, 6, 5))
        
        replaceData(proxy = dataTableProxy(outputId = "table"),
                    data = data,
                    rownames = FALSE)
        
    })
    
}

shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r shiny dt


    【解决方案1】:

    我想出了一个 javascript 解决方案。

    library(DT)
    library(shiny)
    library(shinyjs)
    
    clearSorting <- function(proxy) {
      runjs(paste0("$('#' + document.getElementById('", proxy$id,"').getElementsByTagName('table')[0].id).dataTable().fnSort([]);"))
    }
    
    ui <- fluidPage(
      DTOutput(outputId = "table"),
      actionButton(inputId = "replace", label = "Replace Data"),
      useShinyjs()
    )
    
    server <- function(input, output) {
      
      output$table <- renderDT({
        datatable(data = data.frame(COL_1 = c(1, 3, 2)), rownames = FALSE)
      })
      
      observeEvent(input$replace, {
        
        data <- data.frame(COL_1 = c(4, 6, 5))
        
        clearSorting(proxy = dataTableProxy(outputId = "table"))
        
        replaceData(proxy = dataTableProxy(outputId = "table"),
                    data = data,
                    rownames = FALSE)
        
      })
      
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      猜你喜欢
      • 2014-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-31
      • 2012-03-16
      相关资源
      最近更新 更多