R Shiny - Disabling specific rows in a datatable with column sorting

前端 未结 1 543
难免孤独
难免孤独 2021-01-21 01:40

The app below contains a datatable of the iris dataset with row selection enabled. I would like to disable selection for the first 3 rows specifically. I can do thi

相关标签:
1条回答
  • 2021-01-21 01:45

    It's strange that dataIndex does not work.

    You can use some id for the rows instead.

    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)
    
    0 讨论(0)
提交回复
热议问题