簡體   English   中英

閃亮的數據表模式可編輯 - 限制特定的列和行

[英]Shiny datatable mode editable - restrict specific columns AND ROWS

我想在數據表中限制列和行的可編輯模式。 目前,在這個最小的例子中,我只能編輯特定的列,但它不適用於限制行(不允許行 1 到 5)。

有沒有人有想法?

謝謝你提前

library(shiny)
library(DT)
shinyApp(
  ui = fluidPage(DTOutput('tbl')),
  server = function(input, output) {
    output$tbl = renderDT(
      datatable(data = iris, 
                options = list(lengthChange = FALSE),
                editable = list(target = 'column', disable = list(columns = c(1:3), rows = c(1:5))))
    )
  }
)

您可以修改我提供的鏈接中的代碼,如下所示。

library(shiny)
library(DT)

disabled_rows = paste0("'", paste0("row", c(1,2,3)), "'")   ### disabled rows are listed here

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'});",
  "  }",
  "}"
)

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();});"
)
dat <- iris
dat$ID <- paste0("row", 1:nrow(iris))
rowNames <- TRUE
colIndex <- as.integer(rowNames)

shinyApp(
  ui = fluidPage(DTOutput('tbl')),
  server = function(input, output, session) {
    
    
    ### disable selected rows only
    # output$tbl <- 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)

    ###  disable selected rows and columns
    output$tbl <- renderDT({

      datatable(
        dat,
        rownames = rowNames,
        callback = JS(get_selected_rows),
        class = 'hover row-border order-column',
        options = list(
          lengthChange = FALSE,
          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", 
        editable = list(target = 'column', disable = list(columns = c(2:3) )), selection = 'none',
      )
    }, server = TRUE)

    ### disable selected columns only
    # output$tbl = renderDT(
    #   datatable(data = iris,
    #             options = list(lengthChange = FALSE),
    #             #extensions = "Select", selection = 'none',
    #             editable = list(target = 'column', disable = list( columns = c(2:3) )) )
    # )
  }
)

暫無
暫無

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

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