我已经编写了一个代码,用闪亮的R创建数据表,当我单击感兴趣的行时,它应该扩展并显示子行。 但是,这仅适用于我的数据表的最后一行。在前几行中,仅显示子表的标题,而不显示数据。
我的数据表: Data Table
我的输出:My result
callback = JS("table.column(1).nodes().to$().css({cursor: 'pointer'});
// Format cars object into another table
var format = function (d) {
if (d != null) {
var result = ('<table id=\"child_' + d[2] + '_' + d[3] +
'\">').replace('.', '_') + '<thead><tr>'
for (var col in d[4][0]) {
result += '<th>' + col + '</th>'
}
result += '</tr></thead></table>'
return result
} else {
return '';
}
}
var format_datatable = function (d) {
var dataset = [];
for (i = 0; i <= d[4].length - 1; i++) {
var datarow = $.map(d[4][i], function (value, index) {
return [value];
});
dataset.push(datarow)
}
var subtable = $(('table#child_' + d[2] + '_' +
d[3]).replace('.', '_')).DataTable({
'data': dataset,
'autoWidth': true,
'deferRender': true,
'info': false,
'lengthChange': false,
'ordering': true,
'paging': false,
'scrollX': false,
'scrollY': false,
'searching': false
});
};
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('⊕');
} else {
row.child(format(row.data())).show();
td.html('⊖');
format_datatable(row.data())
}
});
")
答案 0 :(得分:0)
在事件中使用表的jQuery选择器来代替使用table
变量。
$('.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('⊕');
} else {
row.child(format(row.data())).show();
td.html('⊖');
format_datatable(row.data())
}
});
这应该有效
答案 1 :(得分:0)
问题是Description
列中存在空白(尝试扩展第3行,因为没有空白,它应该可以工作)。因此,JS代码生成带有空格的子表的ID,这些ID无效。在下面的代码中,空格由_
替换,并且可以正常工作。此外,通过执行replace('.', '_')
替换点(因为用点的id无效)不是可行的方法,因为这仅替换了第一次出现的点。您必须使用全局替换:replace(/\./g, '_')
。我还对空格进行了全局替换:replace(/\s/g, '_')
。
dat <- data.frame(
' ' = rep('⊕',2),
Sr = c(1, 2),
Description = c("A - B", "X - Y"),
Details = I(list(list(list(Chromosome = "chr18", SNP = "rs2")),
list(list(Chromosome = "chr19", SNP = "rs3"),
list(Chromosome = "chr20", SNP = "rs4")))),
check.names = FALSE
)
callback = JS(
"table.column(1).nodes().to$().css({cursor: 'pointer'});",
"// Format the nested table into another table",
"var format = function (d) {",
" if (d != null) {",
" var result = ('<table class=\"display compact\" id=\"child_' + ",
" ((d[2] + '_' + d[3]).replace(/\\s/g, '_')) +",
" '\">').replace(/\\./g, '_') + '<thead><tr>';",
" for (var col in d[4][0]) {",
" result += '<th>' + col + '</th>';",
" }",
" result += '</tr></thead></table>';",
" return result;",
" } else {",
" return '';",
" }",
"}",
"var format_datatable = function (d) {",
" var dataset = [];",
" for (i = 0; i < d[4].length; i++) {",
" var datarow = $.map(d[4][i], function (value, index) {",
" return [value];",
" });",
" dataset.push(datarow);",
" }",
" var subtable = $(('table#child_' + d[2] + '_' + d[3])",
" .replace(/\\./g, '_').replace(/\\s/g, '_')).DataTable({",
" 'data': dataset,",
" 'autoWidth': true,",
" 'deferRender': true,",
" 'info': false,",
" 'lengthChange': false,",
" 'ordering': true,",
" 'paging': false,",
" 'scrollX': false,",
" 'scrollY': false,",
" 'searching': false,",
" 'sortClasses': false,",
" 'columnDefs': [{targets: '_all', className: 'dt-center'}]",
" });",
"};",
"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('⊕');",
" } else {",
" row.child(format(row.data())).show();",
" td.html('⊖');",
" format_datatable(row.data())",
" }",
"});")
datatable(dat, callback = callback, escape = FALSE,
options = list(
columnDefs = list(
list(visible = FALSE, targets = 4),
list(orderable = FALSE, className = 'details-control', targets = 1)
)
))