首页 > 解决方案 > 如何在 DT::datatable 中禁用 scrollX

问题描述

我有一张我渲染的桌子using DT::datatable。我想要 Y 滚动但没有 X 滚动,也没有长线的换行。我找到了Scroller扩展,但我无法禁用 X 滚动

例子:

---
title: "dt_render"
output: html_document
---

```{r render dt, echo = FALSE, message = FALSE}
library(DT)
DF = data.frame(x = 1:100, y = rep("a really really really really really really really really really really really really really really really really really really really really really really really long line", 100))
datatable(
  DF, 
  extensions = c('Buttons', 'Scroller'), 
  options = list(
    dom = 'Bfrtip',
    buttons = c('colvis','csv'),
    deferRender = TRUE,
    scrollY = 200,
    scroller = TRUE,
    scrollX = FALSE
  ),
  class = 'display compact nowrap'
)

```

输出:

输出

标签: cssrr-markdowndt

解决方案


The following solves it.

I am not happy with the width = 1000 that seems bad, is there something better ?

---
title: "dt_render"
output: html_document
css: styles.css
---

```{r render dt, echo = FALSE, message = FALSE}
library(DT)
DF = data.frame(x = 1:100, y = rep("a really really really really really really really really really really really really really really really really really really really really really really really long line", 100))
datatable(
  DF, 
  extensions = c('Buttons', 'Scroller', 'KeyTable'), 
  width = 1000,
  options = list(
    dom = 'Bfrtip',
    buttons = c('colvis','csv'),
    scrollY = 200,
    scroller = TRUE,
    keys = TRUE    
  ),
  class = 'display compact nowrap'
)

```

with in the css file

.dataTables_scrollBody
{
 overflow-x:hidden !important;
 overflow-y:auto !important;
}

output:

enter image description here


推荐阅读