Reputation: 9018
I'd like to add one more row to the answer posted here:
Determine if DT datatable is clicked in shiny app
Specifically I want to return the "a" field of the row that is clicked so I added
var a = table.rows[row_].cells[col].innerhtml
but nothing happens.
Here is the code. Is it possible?
library(shiny)
library(DT)
runApp(shinyApp(
ui = fluidPage(DT::dataTableOutput('table')),
server = function(input, output, session) {
output$table <- DT::renderDataTable({
datatable(data.frame(a = c(1,2),b=c(2,3)), rownames = FALSE, selection = 'none', callback = JS("table.on('click.dt', 'td', function() {
var row_=table.cell(this).index().row;
var col=table.cell(this).index().column;
var rnd= Math.random();
var a = table.rows[row_].cells[col].innerhtml
var data = [row_, col, rnd,a];
Shiny.onInputChange('rows',data );
});")
)}
)
observeEvent(input$rows, {
print(input$rows)
#print(Sys.time())
})}
))
Thank you.
Upvotes: 2
Views: 1030
Reputation: 21443
If you want to look at clicks from rows, you could use the following callback function:
callback=JS("table.on('click.dt', 'tr', function() {
var data=table.row(this).data();
Shiny.onInputChange('rows',data[0]);
});")
This looks at click events on row (tr
) and gets the data directly then returns the value in the first column.
Upvotes: 3