1

I have a datatable with the first header and the first column fixed when scrolling horizontally and vertically.

Everything works fine thanks to this answer.

But if the first column contains a cell larger than the header I got this after a scroll to the bottom and the right when I get back to the top left of the table:

enter image description here

Here is the code to reproduce the picture:

library(shiny)
library(DT)
library(tidyverse)

(df <- diamonds[1:100,])
df <- mutate(df, carat = as.character(carat))
df[1, 1] <- "qwertyuiopqwertyuiop"
df <- bind_cols(df[1:100, ], df[1:100, ])

runApp(
  list(ui = fluidPage(
    selectInput("here", "Here to reproduce bug", letters),
    dataTableOutput("mytable")
    ),
       server = function(input, output, session){
         output$mytable <- renderDataTable(
           DT::datatable(df,
                         rownames=FALSE,
                         extensions = c("FixedColumns",
                                        "FixedHeader"), 
                         options =
                           list(dom = 't', 
                                scrollX = TRUE, 
                                paging=FALSE,
                                fixedHeader=TRUE,
                                fixedColumns =
                                  list(leftColumns = 1,
                                       rightColumns = 0))))}))

EDIT:

As @r2evans suggested, I try to reproduce the bug with javascript code only. But the wrapping of large columns is different and I cannot reproduce the issue. See the picture and the code below: Could it be a css issue?

enter image description here

index.html

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <title>datatables bug</title>
  <link rel="stylesheet" href="style.css">

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>


  <script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.js"></script>
  <link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/jquery.dataTables.css" />

  <script src="https://cdn.datatables.net/fixedheader/3.4.0/js/dataTables.fixedHeader.min.js"></script>
  <link rel="stylesheet" href="https://cdn.datatables.net/fixedheader/3.4.0/css/fixedHeader.bootstrap.css" />

  <script src="https://cdn.datatables.net/fixedcolumns/4.3.0/js/dataTables.fixedColumns.min.js"></script>
  <link rel="stylesheet" href="https://cdn.datatables.net/fixedcolumns/4.3.0/css/fixedColumns.bootstrap.css" />
  

  <script src="www/script.js"></script>
</head>

<body>
  <h1>Scroll the table to the right and the bottom and go back</h1>
  <p>If the table is not wide or long enough to scroll just zoom in...</p>

  <hr>

</html>

www/script.js

function tableCreate() {
    const table_size = 20;
    const body = document.body,
        tbl = document.createElement('table'),
        hd = document.createElement('thead');


    const r = hd.insertRow();
    for (let i = 0; i < table_size; i++) {
        const h = document.createElement('th');
        h.appendChild(document.createTextNode(`Col ${i}`));
        r.appendChild(h)
    }

    tbl.appendChild(hd);
    const bd = document.createElement('tbody');
    tbl.appendChild(bd);

    for (let i = 1; i < table_size; i++) {
        const tr = bd.insertRow();
        for (let j = 0; j < table_size; j++) {
            const td = tr.insertCell();
            if (i == 0) {
                td.appendChild(document.createTextNode(`Col ${j}`));
            } else {
                td.appendChild(document.createTextNode(`this is a long string !!! Yes it is...`));
            }
        }
    }


    body.appendChild(tbl);

    $("table").attr("id", "myTable")
    $("table").attr("class", "display")
}



$(document).ready(function () {
    tableCreate();

    $('#myTable').DataTable({

        fixedHeader: true,
         fixedColumns: {
             left: 1
         },
        paging: false,
        searching: false
    });
});
pietrodito
  • 1,783
  • 15
  • 24
  • I don't see that in a regular browser, see https://i.stack.imgur.com/C0OkS.png, firefox here. Perhaps it's just an issue with the RStudio IDE? – r2evans Aug 15 '23 at 17:46
  • "meh", I recall having seen something like this question in the past, so it's not without precedence, but ... over to you – r2evans Aug 15 '23 at 17:57
  • Still works https://i.stack.imgur.com/4JpsL.png (tested in both FF and chrome) – r2evans Aug 15 '23 at 18:11
  • Okay, I can reproduce it in FF (https://i.stack.imgur.com/keeQU.png) and chrome, needed to adjust the scaling to get it, sorry I didn't thoroughly check that before. From that, perhaps it's a bug in datatables (js) itself? – r2evans Aug 15 '23 at 18:19
  • @r2evans I have tried to reproduce the bug in JS only... But couldn't make it. – pietrodito Aug 16 '23 at 10:29

1 Answers1

1

Just use the autoWidth = TRUE option see link

runApp(
  list(ui = fluidPage(
    selectInput("here", "Here to reproduce bug", letters),
    dataTableOutput("mytable")
    ),
       server = function(input, output, session){
         output$mytable <- renderDataTable(
           DT::datatable(df,
                         rownames=FALSE,
                         extensions = c("FixedColumns",
                                        "FixedHeader"), 
                         options =
                           list(dom = 't',
                                autoWidth = TRUE,         # ADD THIS
                                scrollX = TRUE, 
                                paging=FALSE,
                                fixedHeader=TRUE,
                                fixedColumns =
                                  list(leftColumns = 1,
                                       rightColumns = 0))))}))
r2evans
  • 141,215
  • 6
  • 77
  • 149
pietrodito
  • 1,783
  • 15
  • 24
  • autoWidth must be set to TRUE – pietrodito Aug 16 '23 at 12:37
  • Duh, facepalm, forgive my stupidity. Thanks! (_drinks more caffeine ..._) – r2evans Aug 16 '23 at 12:44
  • 1
    (I suggested an edit to your answer to be a little more literal, in case follow-on readers need encouragement ... like I did. If you don't like it, my apologies, feel free to [roll it back](https://stackoverflow.com/posts/76913453/revisions). Thanks again!) – r2evans Aug 16 '23 at 12:45
  • 1
    @r2evans I can see why you thought it has to be set to FALSE. Because somehow width is changed when you scroll away from the topleft. This is a kind of bug imho, but I cannot reproduce it and see how and why it is happening. – pietrodito Aug 16 '23 at 12:52