R Shiny Datatable: Prevent already selected row from being deselected when it is clicked again


Shri

R (Version 3.4.1) I'm using the DT (Version 0.4) package in DT (Version 1.0.5) and need to prevent already selected rows from being deselected when the user clicks again. For example, in the image below, if the user clicks on row 3 again, the row should not be deselected. However, if the user selects a new row, that new row will be selected and the previous row will be deselected.

I think what I need is for the datatable to completely ignore click events on selected rows.

If the user clicks row 3 again, it should not deselect the row, but if he clicks on a new row, it should deselect the new row while deselecting the old row

I tried using shinyjs::onclickthe method shown below, but it's not ideal, as it seems to "deselect and reselect" the selected row (the blue highlight disappears and reappears when clicked again), rather than preventing deselect .

library(shiny)
library(DT)
library(shinyjs)

shinyApp(
  ui <- shinyUI(
    fluidPage(
      shinyjs::useShinyjs(),
      DTOutput("test")
    )
  ),  
  server <- shinyServer(function(input, output, session) {

    output$test <- renderDT({
      datatable(head(iris), selection = 'single')
    })    
    shinyjs::onclick("test",
               selectRows(dataTableProxy("test"), selected = input$test_rows_selected)
             )    
  })
)

I wonder if there is an easy way to do this. Thanks!

R. Dimas Bagas Herlambang

Try using pointer-events: nonein CSS :

table.dataTable tbody tr.selected {
  pointer-events: none
}

The only limitation is that it blocks the entire hover/click event on the currently selected row, so you can't use it on columns with hover/clickable HTML content.

Hope it helps you!

Related


Get selected row from datatable in shiny app

evolutionary microbes I am going to modify this application: https://demo.shinyapps.io/029-row-selection/ So only one row can be selected at a time, so that I can get the item in the first column of the selected row to plot the data. Does anyone know how to do

Get selected row from DataTable in Shiny App

evolutionary microbes I am going to modify this application: https://demo.shinyapps.io/029-row-selection/ So only one row can be selected at a time, so that I can get the item in the first column of the selected row to plot the data. Does anyone know how to do

Get selected row from datatable in shiny app

evolutionary microbes I am going to modify this application: https://demo.shinyapps.io/029-row-selection/ So only one row can be selected at a time, so that I can get the item in the first column of the selected row to plot the data. Does anyone know how to do

Get selected row from datatable in shiny app

evolutionary microbes I am going to modify this application: https://demo.shinyapps.io/029-row-selection/ So only one row can be selected at a time, so that I can get the item in the first column of the selected row to plot the data. Does anyone know how to do

Prevent dateInput from being NULL in R Shiny

Altamash Rafic I'm trying to make it's dateInput accept only the values from the dateInput dropdown options and nothing else. For example, if the user tries to enter nothing or delete the date, "1995-11-13" is returned. My code works, but nothing is entered an

Prevent dateInput from being NULL in R Shiny

Altamash Rafic I'm trying to make it's dateInput accept only the values from the dateInput dropdown options and nothing else. For example, if the user tries to enter nothing or delete the date, "1995-11-13" is returned. My code works, but nothing is entered an