【问题标题】:Styling the expand row button in DT in shiny R在闪亮的 R 中为 DT 中的展开行按钮设置样式
【发布时间】:2018-07-30 22:29:55
【问题描述】:

我正在尝试设置 DT 中可用的展开行按钮的样式。样式可用here。我用于创建数据表的代码是-

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;');
    }
  });"
))*

我不太熟悉 JS,所以我不知道从哪里开始。我尝试使用链接中的 JS 和 CSS 文件,但没有取得任何进展。任何线索表示赞赏,谢谢!

【问题讨论】:

    标签: r shiny dt


    【解决方案1】:

    如果您在浏览器中打开表格,这将像这样工作(这在 RStudio 查看器中不起作用)。但我怀疑有一个更简单的解决方案......而且这个解决方案依赖于托管在 Github 上的图像,这不是很酷......

    library(DT)
    datatable(
      cbind(' ' = '<img src=\"https://raw.githubusercontent.com/DataTables/DataTables/master/examples/resources/details_open.png\"/>', 
            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('<img src=\"https://raw.githubusercontent.com/DataTables/DataTables/master/examples/resources/details_open.png\"/>');
                    } else {
                    row.child(format(row.data())).show();
                    td.html('<img src=\"https://raw.githubusercontent.com/DataTables/DataTables/master/examples/resources/details_close.png\"/>');
                    }
                    });"
    ))
    

    【讨论】:

    • 嗨@stephane,这太好了,我有一个疑问,我们不能让return '&lt;div style=\"background-color:#eee; padding: .5em;\"&gt; Model: ' + d[0] + ', mpg: ' + d[2] + ', cyl: ' + d[3] + '&lt;/div&gt;'; }; 动态化,就像将反应变量传递给JS?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    • 2019-12-06
    • 1970-01-01
    • 2019-02-10
    • 2016-01-02
    • 2017-07-25
    相关资源
    最近更新 更多