簡體   English   中英

閃亮:不能刪除行超過1次

[英]Shiny: cannot delete rows more than 1 time

我正在開發一個閃亮的應用程序。 server.R包含類似......的代碼

dFt1 <- reactiveValues()
dFt1$dat <- data.frame(loadTransactionData())
...
output$t_tab_1 <- DT::renderDataTable({
    DT::datatable(
        dFt1$dat
        ,selection = list(mode = "single")
        ,options = list(
            rownames = TRUE
            ,pageLength = 10
            ,order = list(list(2,"desc"),list(1,"asc"))
        )
    )
})
...
observe({
    if (is.null(input$delete) || input$delete == 0){return()}
    session$sendCustomMessage(
        type = 'jsCode'
        ,list(value = 'confirm("Are You Sure?");')
    )
})

observeEvent(input$deleteConfirmChoice, {
    if (input$deleteConfirmChoice == "TRUE") {
        x <- input$t_tab_1_rows_selected
        deleteTransaction(x)
        isolate(dFt1$dat <- dFt1$dat[row.names(dFt1$dat) != x, ])
    }
})  

deleteTransaction <- function(x) {
    qy <- "DELETE FROM Transactions where timestamp = '<t>'"
    qy <- gsub("<t>",x,qy)
    db <- dbConnect(SQLite(), dbname=systemDatabase)
    dbGetQuery(db,qy)
    dbDisconnect(db)
}

當然,在ui.R中有刪除按鈕,並有一個調用javascript的確認框。

該應用程序運行良好。 我可以選擇一條記錄並將其刪除。 然后,我可以選擇另一條記錄。 但是我無法刪除它。 javascript確認框將運行,但是單擊“是”不會刪除該記錄。 我想知道為什么一次可以但下次不能。

有什么幫助嗎?

好! 終於來到了起作用的東西...

    observeEvent(input$deleteConfirmChoice, {
    if (input$deleteConfirmChoice == "TRUE") {
        x <- input$t_tab_1_rows_selected
        deleteTransaction(x)
        isolate(dFt1$dat <- dFt1$dat[row.names(dFt1$dat) != x, ])
        shinyjs::disable("delete")
        shinyjs::disable("edit")
        session$sendCustomMessage(
            type = 'jsCode'
            ,list(value = '1 != 1;')
        )
    }
})  

我添加了另一個session $ sendCustomMessage調用,評估為FALSE。

想知道是否有更好的解決方案?

這是另一種方法...

我更改了傳遞給session $ sendCustomMessage的javascript代碼。 單擊“否”按鈕將返回0。單擊“是”按鈕將返回一個隨機數。 我希望這可以保證連續兩次單擊“是”按鈕時具有不同的值。

    observe({
    if (is.null(input$delete) || input$delete == 0){return()}
    session$sendCustomMessage(
        type = 'jsCode'
        ,list(value = 
            '
            (function() {
                if (confirm("Are you sure?")) {
                    return Math.random()*3 + 1;
                } else {
                    return 0;
                }
            })()
            '
        )
    )
})

observeEvent(input$deleteConfirmChoice, {
    if (input$deleteConfirmChoice == "TRUE") {
        x <- input$t_tab_1_rows_selected
        deleteTransaction(x)
        isolate(dFt1$dat <- dFt1$dat[row.names(dFt1$dat) != x, ])
    }
})  

還有其他更清潔的解決方案嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM