DT2’s options argument maps directly to the DataTables
JavaScript configuration object. An R named list becomes a JS object; R
vectors become JS arrays. This means you can translate any example from
datatables.net directly
to R.
| JavaScript | R |
|---|---|
{ key: value } |
list(key = value) |
[1, 2, 3] |
c(1, 2, 3) or list(1, 2, 3) |
true / false |
TRUE / FALSE |
null |
NULL |
"string" |
"string" |
function(d) { ... } |
htmlwidgets::JS("function(d) { ... }") |
DataTables 2 replaced the old dom string with
layout, a structured way to position elements around the
table. This is the most important change from DataTables 1.x.
+------------------+------------------+
| topStart | topEnd |
+------------------+------------------+
| top (full width) |
+------------------+------------------+
| top2Start | top2End |
+------------------+------------------+
| TABLE |
+------------------+------------------+
| bottomStart | bottomEnd |
+------------------+------------------+
| bottom (full width) |
+------------------+------------------+
| bottom2Start | bottom2End |
+------------------+------------------+
| Element | Description |
|---|---|
"search" |
Search/filter input |
"paging" |
Page navigation |
"info" |
“Showing X to Y of Z entries” |
"pageLength" |
Entries per page selector |
"buttons" |
Buttons toolbar (requires Buttons extension) |
"searchBuilder" |
SearchBuilder (requires extension) |
"searchPanes" |
SearchPanes (requires extension) |
NULL |
Remove whatever would normally be in that position |
When you don’t specify layout, DataTables uses:
Move search to the left, page length to the right:
Set any position to NULL:
Wrap them in a list:
Use top, top2, bottom,
bottom2 for full-width rows:
dt2(mtcars[1:20, ], options = list(
pageLength = 10,
buttons = list(
list(extend = "collection", text = "Export \u25BC",
buttons = list("copyHtml5", "csvHtml5", "excelHtml5")),
list(extend = "colvis", text = "Columns")
),
layout = list(
topStart = "buttons",
topEnd = list(search = list(placeholder = "Filter...")),
bottomStart = "info",
bottomEnd = "paging"
)
))domIf you’re coming from DataTables 1.x or the DT package:
Old dom |
New layout |
|---|---|
"frtip" |
(default — no need to specify) |
"tp" |
list(topStart = NULL, topEnd = NULL, bottomStart = NULL, bottomEnd = "paging") |
"Bfrtip" |
list(topStart = "buttons") |
"lfBrtip" |
list(topStart = list("pageLength", "buttons")) |
DT2 will automatically convert dom strings containing
"B" to the layout equivalent, but using
layout directly is recommended.
htmlwidgets::JS() for CallbacksWhenever DataTables expects a JavaScript function, wrap it in
htmlwidgets::JS():
dt2(iris, options = list(
pageLength = 5,
createdRow = htmlwidgets::JS("
function(row, data, dataIndex) {
if (data['Sepal.Length'] > 5) {
row.style.backgroundColor = '#fff3cd';
}
}
")
))| DataTables Option | Purpose |
|---|---|
createdRow |
Modify each row after creation |
initComplete |
Run code after table initializes |
drawCallback |
Run code after each draw |
headerCallback |
Modify the header after draw |
footerCallback |
Modify the footer after draw |
columns.render |
Custom cell rendering |
DataTables v2 provides built-in renderers accessible via
DataTable.render.*:
progress_render <- htmlwidgets::JS("
function(data, type, row, meta) {
if (type !== 'display') return data;
var pct = Math.min(100, Math.max(0, parseFloat(data)));
var color = pct > 70 ? '#198754' : (pct > 40 ? '#ffc107' : '#dc3545');
return '<div style=\"background:#eee;border-radius:4px;overflow:hidden\">' +
'<div style=\"width:' + pct + '%;background:' + color +
';height:14px;border-radius:4px\"></div></div>';
}
")
df <- data.frame(
task = c("Design", "Backend", "Testing", "Deploy"),
progress = c(85, 60, 30, 95)
)
dt2(df, options = list(
pageLength = 10,
columnDefs = list(
list(targets = 1, render = progress_render)
)
))dt2(iris[1:10, ], options = list(
pageLength = 5,
language = list(
search = "Buscar:",
lengthMenu = "Mostrar _MENU_ registros",
info = "Mostrando _START_ a _END_ de _TOTAL_",
paginate = list(
first = "<<",
previous = "<",
`next` = ">",
last = ">>"
),
zeroRecords = "Nenhum registro encontrado",
emptyTable = "Tabela vazia"
)
))Open the browser console and look for [DT2] log
messages. Access the DataTables API object directly via JavaScript: