简体   繁体   English

如何在 DT::datatable 中禁用 scrollX

[英]How can I disable scrollX in DT::datatable

I have a table that I render using DT::datatable .我有一个using DT::datatable呈现的表。 I would like to have a Y scrolling but no X scrolling, and no wraping of long lines either.我想要 Y 滚动但没有 X 滚动,也没有长线的换行。 I found the Scroller extension but I cannot disable X scroll .我找到了Scroller扩展,但我无法禁用 X 滚动

example:例子:

---
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'
)

```

output:输出:

输出

The following solves it.下面解决它。

I am not happy with the width = 1000 that seems bad, is there something better ?我对看起来很糟糕的width = 1000不满意,还有更好的吗?

---
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在css文件中

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

output:输出:

在此处输入图像描述

Here is what worked for me:这对我有用:

In the css file (or in the datatables.css lines of Scroller extension):在 css 文件中(或在 Scroller 扩展的 datatables.css 行中):

  1. to disable the x scrolling, I added:要禁用 x 滚动,我添加了:

     .dataTables_scrollBody{ overflow-x:hidden !important; overflow-y:auto !important; }
  2. to disable the wraping of long lines, I commented the lines:为了禁用长行的换行,我评论了这些行:

     /* div.dts tbody th,div.dts tbody td{ white-space:nowrap } */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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