Skip to content Skip to sidebar Skip to footer

R Dt Datatable Not Retaining Row Index/counter Column After Selecting New Page When Using Callback Option

I am using this question as a reference to add a 'row index' or 'counter column' (as described in the datatable documentation here) to a DT::datatable in a Shiny application. The i

Solution 1:

Two possibilities:

1) use server = FALSE:

output$tbl <- renderDT({
  datatable(data, filter = "top", rownames=TRUE, 
            options = list(
              pageLength = 300, lengthMenu = c(100,200,300,400,500,600)
            ),
            callback=JS("table.on( 'order.dt search.dt', function () {
              table.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
              cell.innerHTML = i+1;});}).draw();")
  )
}, server = FALSE)

2) Otherwise, here is DeFKnoL's method:

js <- c(
  "table.on('draw.dt', function(){",
  "  var PageInfo = table.page.info();",
  "  table.column(0, {page: 'current'}).nodes().each(function(cell,i){", 
  "    cell.innerHTML = i + 1 + PageInfo.start;",
  "  });",
  "})")

output$tbl <- renderDT({
  datatable(data, filter = "top", rownames=TRUE, 
            options = list(
              pageLength = 300, lengthMenu = c(100,200,300,400,500,600)
            ),
            callback = JS(js)
  )
})

Post a Comment for "R Dt Datatable Not Retaining Row Index/counter Column After Selecting New Page When Using Callback Option"