我正在尝试在Shiny中呈现的DataTable中设置列的宽度,并且无法使用aoColumnDefs选项实现它。有人曾尝试过这个吗?我的表有1个文本,后跟3个数字列。数字列需要更窄,第1列(文本)更宽。
output$result <- renderDataTable({
z <- as(dataInput(), "data.frame")
setnames(z, c("Rules", "Support", "Confidence", "StatDep"))
z
}, options = list(aLengthMenu = c(5, 30, 50), iDisplayLength = 5, bSortClasses = TRUE,
aoColumnDefs = list(sWidth = "50px", aTargets = list(1))))
谢谢,
**更新**这似乎有效,但也可能有其他选择。
output$result <- renderDataTable({
z <- as(dataInput(), "data.frame")
setnames(z, c("Rules", "Support", "Confidence", "StatDep"))
z
}, options = list(aLengthMenu = c(5, 30, 50), iDisplayLength = 5, bSortClasses = TRUE,
bAutoWidth = FALSE,
aoColumn = list(list(sWidth = "150px", sWidth = "30px",
sWidth = "30px", sWidth = "30px"))
))
答案 0 :(得分:26)
试试这个
<script>
// The following variables are defined in the global scope
var num1 = 2,
num2 = 3,
name = "Chamahk";
// This function is defined in the global scope
function multiply(num1, num2) {
return num1 * num2;
}
console.log(multiply(num1, num2)); // Returns 6!!!
// A nested function example
function getScore () {
var num1 = 20,
num2 = 30;
function add(num1, num2) {
return name + " scored " + (num1 * num2);
}
return add(num1, num2);
}
console.log(getScore()); // Returns "Chamahk scored 60000"
</script>
将所有列的宽度设置为200px。
要设置所选列的宽度,请将#OUTPUT - dtdata
output$table <- DT::renderDataTable({
data.frame(a=c(1,2,3,4,5),b=c("A","B","C","D","E"))
},
options = list(
autoWidth = TRUE,
columnDefs = list(list(width = '200px', targets = "_all"))
))
更改为数字或向量。
targets
答案 1 :(得分:13)
顺便说一下,如果你喜欢我,并且在版本1.10出现之前从未使用过DataTables - 上面的例子一开始让我感到困惑,因为他们使用版本1.9中使用的符号,但1.10引入了新的符号: http://datatables.net/upgrade/1.10-convert
我一直在使用新语法,即
columnDefs而不是aoColumnDefs http://datatables.net/reference/option/columnDefs
宽度而不是sWidth http://datatables.net/reference/option/columns.width 等