【问题标题】:How to start DT datatable with all child rows expanded?如何在展开所有子行的情况下启动 DT 数据表?
【发布时间】:2021-08-03 05:24:49
【问题描述】:

在这个带有子行的DT example 中,如何启动所有子行都展开的表?

library(DT)
datatable(
  cbind(' ' = '⊕', mtcars), escape = -2,
  options = list(
    columnDefs = list(
      list(visible = FALSE, targets = c(0, 2, 3)),
      list(orderable = FALSE, className = 'details-control', targets = 1)
    )
  ),
  callback = JS("
  table.column(1).nodes().to$().css({cursor: 'pointer'});
  var format = function(d) {
    return '<div style=\"background-color:#eee; padding: .5em;\"> Model: ' +
            d[0] + ', mpg: ' + d[2] + ', cyl: ' + d[3] + '</div>';
  };
  table.on('click', 'td.details-control', function() {
    var td = $(this), row = table.row(td.closest('tr'));
    if (row.child.isShown()) {
      row.child.hide();
      td.html('&oplus;');
    } else {
      row.child(format(row.data())).show();
      td.html('&CircleMinus;');
    }
  });"
))

PS:stackoverflow 迫使我在问题中包含更多细节,但没有其他要添加的...

【问题讨论】:

    标签: r datatables dt


    【解决方案1】:

    您还可以使用现有的回调来遍历表中的每一行。在该迭代中,您可以创建并打开每个子记录:

    table.rows().every( function () { 
      this.child( format(this.data()) ).show(); 
    } );
    

    此 sn-p 需要附加到您的 callback = JS(...) 选项的末尾,如下所示:

      callback = JS(
        "
      table.column(1).nodes().to$().css({cursor: 'pointer'});
      var format = function(d) {
        return '<div style=\"background-color:#eee; padding: .5em;\"> Model: ' +
                d[0] + ', mpg: ' + d[2] + ', cyl: ' + d[3] + '</div>';
      };
      table.on('click', 'td.details-control', function() {
        var td = $(this), row = table.row(td.closest('tr'));
        if (row.child.isShown()) {
          row.child.hide();
          td.html('&oplus;');
        } else {
          row.child(format(row.data())).show();
          td.html('&CircleMinus;');
        }
      });
      table.rows().every( function () { 
        this.child( format(this.data()) ).show(); 
      } );"
      )
    

    结果:

    【讨论】:

      猜你喜欢
      • 2014-08-21
      • 2015-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-11
      相关资源
      最近更新 更多