r - 在闪亮的 R 中设置 DT 中的展开行按钮的样式
问题描述
我正在尝试设置 DT 中可用的展开行按钮的样式。样式可在此处获得。我用于创建数据表的代码是-
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('⊕');
} else {
row.child(format(row.data())).show();
td.html('⊖');
}
});"
))*
我不太熟悉 JS,所以我不知道从哪里开始。我尝试使用链接中的 JS 和 CSS 文件,但没有取得任何进展。任何线索表示赞赏,谢谢!
解决方案
如果您在浏览器中打开表格,这将像这样工作(这在 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\"/>');
}
});"
))
推荐阅读
- typescript - 使用 Jest 模拟 @apollo/react-hooks useQuery
- selenium - Selenium, groovy, 不能执行任何 click(), sendKeys() 或类似功能
- ionic-framework - 如何在离子电容器中进行产品构建
- python - TypeError:无法腌制“weakref”对象
- c# - ZedGraph 在所有图表中显示范围
- python - 在另一个文件python中定义一个函数中使用的变量
- node.js - 节点的查询字符串:相同查询参数的解析数组的顺序
- wordpress - 我的网站加载非常缓慢,我不知道为什么
- discord.js - 从 client.once('ready' 等 discord.js 中运行 client.setActivity
- c - 重新排序如何消除反向循环携带依赖