简体   繁体   中英

Passing column name to target option in a shiny datatable

I'm stuck in trying to pass the column name (instead the column number) in the target option of columnDefs. The table is dynamic so I definitely need the option to target the column name. Below is a reproducible example. The example is not dynamic, however.

datatable(iris[c(1:20, 51:60, 101:120), ], options = list(columnDefs = list(list(
  targets = 5,
  render = JS(
    "function(data, type, row, meta) {",
    "return type === 'display' && data.length > 6 ?",
    "'<span title=\"' + data + '\">' + data.substr(0, 6) + '...</span>' : data;",
    "}")
))), callback = JS('table.page(3).draw(false);'))

Tried with targets = 'Species' , targets = iris$Species but they didn't work.

if you are interested in setting multiple columns to different widths, consider the following ( take care that R is one based, while javascript is one based ):

  col_a <- which(names(dat) %in% c("column_name1", "column_name2"))
  col_b <- which(names(dat) %in% c("column_name3", "column_name4"))
  col_c <- which(names(dat) %in% c("column_name5"))

  columnDefs = list(list(width = '30px', targets = as.list(col_a - 1)),
                    list(width = '80px', targets = as.list(col_b - 1)),
                    list(width = '200px', targets = as.list(col_c - 1)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM