如何使用rhandsontable将单个单元格设置为“日期”格式?

问题描述 投票:0回答:1

使用 {rhandsontable} 包可以创建一个表格,其中日期列包含每个单元格的下拉列表,既提供了流畅的 UI 又验证了用户输入。

我想在列中设置单个单元格以使用此格式,但包的 API 似乎无法实现这种级别的特异性:

对整列使用日期格式

library(rhandsontable)

rhandsontable(
  data.frame(
    date = seq(as.Date("2020-01-01"), by = "days", length.out = 4)
  )
)

对单个单元格使用日期格式

例如,整列的数据可能是这样的:

# E.g. for the third cell in column `x`
rhandsontable(
  data.frame(
    x = c("foo", "bar", "2020/01/01")
  )
)

不幸的是,{rhandsontable} 似乎不支持将第三个单元格设置为开箱即用的日期格式的方法。

hot_cell()
用于设置单个单元格属性,例如注释,但不能用于设置单元格格式。

r htmlwidgets rhandsontable
1个回答
0
投票

浏览

rhandsontable()
hot_cell()
的源代码,我发现我可以相当轻松地实现这样的目标,尽管这绝对是一个“标签外”功能:

library(rhandsontable)

df <- data.frame(
  y = c("foo", "bar", "2020/01/01")
)

hot <- rhandsontable(df)

hot$x$cell <- list(list(
  row = 2,
  col = 0,
  type = "date",
  correctFormat = TRUE,
  dateFormat = "MM/DD/YYYY"
))

hot

© www.soinside.com 2019 - 2024. All rights reserved.