Skip to content

the server mode supports sub data.frame in its column now #530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 17, 2018

Conversation

shrektan
Copy link
Collaborator

@shrektan shrektan commented Apr 17, 2018

Closes #525

Some user wants to implement some heavy customized datatable style, such as in #525, a child table for each row. The current implement cleanDataFrame() doesn't support this and leads to a broken datatable in the server-side processing mode. This PR is going to fix this.

Example Code

  • Before this PR : The browser will throw an error says
DataTables warning: table id=DataTables_Table_0 - Error in `[<-.data.frame`(`*tmp*`, , j, value = list(structure(list(A = c("A", : replacement element 1 is a matrix/data frame of 3 rows, need 2
  • After this PR : It works.

library(data.table)
library(shiny)
library(DT)

dat <- local({
  n <- 6
  DT <- data.table(
    HEADER = '&#9658;',
    A = rep_len(LETTERS, n),
    B = rep_len(1:10, n),
    C = rep_len(c("GRP1", "GRP2"), n)
  )
  DT[, .(D=.(.SD)), by = .(HEADER, C)]
})


callback_js <-
  JS("
var init_tbl = function(id) {
  return '<table id=\"' + id + '\" class=\"display\" width=\"100%\"></table>';
}

var init_datatable = function(id, d) {
  id = '#'+id;
  $(id).DataTable({
    data: d,
    columns: [
    { data: 'A' },
    { data: 'B' }
    ]  
  }).draw();
}
var sub_tbl_id = 0;
table.on('click', 'td.details-control', function() {
  var table = $(this).closest('table');
  var td = $(this);
  var row = $(table).DataTable().row(td.closest('tr'));
if (row.child.isShown()) {
  row.child.hide();
  td.html('&#9658;');
} else {
  sub_tbl_id = sub_tbl_id + 1;
  var id = 'sub_tbl_id_' + sub_tbl_id;
  row.child(init_tbl(id)).show();
  init_datatable(id, row.data()[3]);
  td.html('&#9660;');
}
})
     ")

runApp(list(
  ui = fluidPage(DT::DTOutput("tbl")),
  server = function(input, output) {
    output$tbl <- DT::renderDT({
      datatable(
        dat,
        escape = FALSE,
        options = list(
          columnDefs = list(
            list(orderable = FALSE, className = 'details-control', targets = 1),
            list(visible = FALSE, targets = -1)
          )
        ),
        callback = callback_js
      )
    }, server = TRUE)
  }), port = 33333)

Copy link
Member

@yihui yihui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This is an elegant fix.

@yihui yihui merged commit d2d02cd into rstudio:master Apr 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Child Row & List Structure Error after updating to DT 0.4
2 participants