简体   繁体   English

通过R DT :: datatables中的JS回调呈现HTML

[英]Rendering HTML via JS callback within R DT::datatables

Working off of tomasreigl's example here ( https://github.com/rstudio/DT/issues/393#issuecomment-279627237 ), I have a slight variation that is not working, I think due to basic JS issue. 在这里处理tomasreigl的示例( https://github.com/rstudio/DT/issues/393#issuecomment-279627237 ),由于基本的JS问题,我有一些不起作用的变化。

Create Data 建立资料

library(DT)
dataSet <- data.frame(name=c("Jack", "Jill"),
                  age=c(25,25),
                  tableHtml=c("<table><tr><th>Value A</th><th>Value B</th></tr><tr><td>1</td><td>2</td></tr></table>",
                              "<div><table><tr><th>Value A</th><th>Value B</th></tr><tr><td>3</td><td>4</td></tr></table></div>"),
                  stringsAsFactors=FALSE)'

Approach 1, Render an HTML Table in the DT Table WORKING, But Not What I Need 方法1,在DT表中呈现HTML表工作,但不是我需要的

Now, what I would like to do is render an html table as a nested child for each row in the DT interactive table. 现在,我想为DT交互式表中的每一行呈现一个html表作为嵌套子级。 This first chunk works, but obviously will render the same table for each DT row, as the HTML is hard coded in the callback: 第一个块有效,但显然会为每个DT行呈现相同的表,因为HTML在回调中进行了硬编码:

## Working, but same child table for every row
DT::datatable(
    cbind(' ' = '&oplus;', dataSet),
    escape = -2,
    options = list(
        columnDefs = list(
            list(visible = FALSE, targets = c(0,4)),
            list(orderable = FALSE, className = 'details-control', targets = 1)
        )
    ),
    callback = JS("
                  table.column(1).nodes().to$().css({cursor: 'pointer'});
                  var format = function(d) {
                    return '<div><table><tr><th>Value A</th><th>Value B</th></tr><tr><td>1</td><td>2</td></tr></table></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;');
                      }
                    });"
                  )
)

Approach 2 Render Different HTML Tables for each row NOT WORKING 方法2为每行呈现不同的HTML表无效

In comes the tableHtml column of the data frame dataSet . 进入数据框dataSettableHtml列。 For each row of data frame dataSet I would like to render a DT table row with a child row containing a table using the HTML in column tableHtml of data frame dataSet . 对于数据帧dataSet每一行,我想使用数据帧dataSet tableHtml列中的HTML渲染DT表行,其中子行包含一个表。 Below, I try just returning d[4] , but that returns the raw HTML without rendering the table. 在下面,我尝试仅返回d[4] ,但这将返回原始HTML而不呈现表。

## Attempt at different child table for each row
datatable(
    cbind(' ' = '&oplus;', dataSet),
    escape = -2,
    options = list(
        columnDefs = list(
            list(visible = FALSE, targets = c(0,4)),
            list(orderable = FALSE, className = 'details-control', targets = 1)
        )
    ),
    callback = JS("
                  table.column(1).nodes().to$().css({cursor: 'pointer'});
                  var format = function(d) {
                    return d[4]
                  };
                  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;');
                      }
                    });"
                  )
)

I've tried about 20 variations but none of them work as expected. 我已经尝试了大约20种变体,但是它们都没有按预期工作。 The fact that my first modification of thomasreigl's example does work, makes me think it is just a minor JS issue that is beyond me. 我对thomasreigl的示例进行的第一次修改确实有效,这使我认为这只是我之外的一个小JS问题。 Any help appreciated. 任何帮助表示赞赏。

好吧,又花了2个小时摆弄,结果将escape = -2更改为escape = FALSE可以解决问题。

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

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