R DT通过分配' child'来扩展/折叠嵌套行。和父母'行到

时间:2016-12-07 19:56:14

标签: javascript r shiny dt

我正在尝试通过分配一个" child"来实现R数据表中的可折叠子行。对于我想要成为子行的行以及" parent"我希望成为父行的行的类。是否可以在R数据表中将子行作为数据表中的实际行?我找到了一个示例,用于根据R数据表行中的数据为每行定义子行:http://rstudio.github.io/DT/002-rowdetails.html。但是,我希望只显示' total'来自我的数据的行,并使所有非总行子行。子行将具有与父行相同的列和格式。

我找到了JS的解决方案,但是我很难用回调来解决它。

这是JS解决方案: Expand/Collapse table rows with nested rows

以下是我在R中的基本示例的尝试:

library(DT)
datatable(
  cbind(' ' = '⊕', mtcars), escape = -2,
  options = list(
   rowCallback=DT::JS('function(row,data,index){
       if(index%2==1) {$("td",row).addClass("child")};
       if(index%2==0) {$("td",row).addClass("parent")};
    }'),
    columnDefs = list(
      list(visible = FALSE, targets = c(0, 2, 3)),
      list(orderable = FALSE, className = 'details-control', targets = 1)
    )
  ),

  callback = JS("

    function getChildren($row) {
        var children = [];
        while($row.next().hasClass('child')) {
             children.push($row.next());
             $row = $row.next();
        }            
        return children;
    }        

    $('.parent').on('click', function() {

        var children = getChildren($(this));
        $.each(children, function() {
            $(this).toggle();
        })
    });"



))

感谢您的帮助!

0 个答案:

没有答案
相关问题