1

I would like to add a popover on each row of a datatable. Using the solution provided [here] (tooltip or popover in Shiny datatables for row names?) (I don't know JavaScript so I have blindly copy paste the code) I've managed to add the popover on the first page of the table.

The problem is that the table is big so that i'm forced in splitting it in more pages. When I select another page of the table the popover stop to work.

Here the code that i'm using

output$view_data<-DT::renderDataTable({
    DT::datatable(Extraction(),rownames = FALSE,escape = FALSE,
        callback = JS(paste("
            var tips =",paste0("[",paste0("'",unlist(DrugFilter()),"'",collapse=","),"]"),",
            firstColumn = $('#view_data tr td:first-child');                                    
            for (var i = 0; i < tips.length; i++) 
                {$(firstColumn[i]).attr('title', tips[i]);}"
        ))
     ))
}, server = FALSE)

How can I modify the code to make the popover to work on all the table pages and not only on the first one?

Roberto
  • 745
  • 4
  • 19

1 Answers1

1

I would try with the rowCallback:

rowCallback <- c(
  "function(row, data, displayNum, displayIndex){",
  sprintf("  var tips = [%s];", 
          paste0("'",unlist(DrugFilter()),"'",collapse=",")),
  "  for(var i = 0; i < tips.length; i++){",
  "    if(displayIndex== i){",
  "      $('td:eq(0)',row).attr('title', tips[i]);",
  "    }",
  "  }",
  "}"
)

datatable(Extraction(), 
          rownames = FALSE, 
          escape = FALSE, 
          options = list(
            rowCallback = JS(rowCallback)
          )
)
Roberto
  • 745
  • 4
  • 19
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
  • Thanks @Stephane for your answer. I see an improvement but it is still not working properly. Now the popover appears in the first page of the table and also in the others. BUT the text in the popover is not updated when moving from one page to the others (i.e. the popover text for the rows in page 2,3,4... is still the same as for the rows in page one) – Roberto Jan 09 '19 at 10:03
  • I have solved changing the function call to :"function(row, data, displayNum, displayIndex)" and changing "index" in "displayIndex" in the if statement. If you edit your answer I will approve it. Thanks for the help – Roberto Jan 09 '19 at 10:22