2017-12-20 12 views
2

Ich bin in ein Problem mit Databases und glänzend, speziell innerhalb einer Flexdashboard, aber ich denke, das ist irrelevant.R Shiny - Scrollen zu einer bestimmten Zeile der Datentabelle mit Javascript Callback

Ich möchte zu einer bestimmten Zeile in der Datentabelle blättern, wenn ich auf den entsprechenden Punkt in einem Diagramm klicke. Aber das minimale Problem, das ich habe, ist, einfach zu irgendeiner Reihe zu scrollen. Ich kann eine Reihe mit JavaScript mit der Option initComplete auswählen, aber scrollTo() wird nichts für mich tun.

Mit Blick auf eine vorherige Frage, Scroll to specific row in Datatable API, kam ich zu diesem Beispiel https://codepen.io/anon/pen/KWmpjj. Es zeigt die JavaScript-Funktion, die Sie mit initComplete verwenden können, aber dies wurde nicht mit R/Shiny gemacht. Insbesondere wird die folgende Option für eine kleine Datentabelle finden:

initComplete: function() { 
     this.api().row(14).scrollTo(); 
     $(this.api().row(14).node()).addClass('selected'); 
    } 

Da mein Ziel ist dies in einem flexdashboard verwende ich ein minimales Beispiel in R Abschlags-Format haben. Ein hübscher Standardanruf zu DT::renderDataTable mit gelegentlichen Daten. Ich verstehe nicht, warum this.api().table().row(15).scrollTo(); nichts tun wird. Ich habe eine Warnung hinzugefügt, um zu bestätigen, dass das JavaScript von initComplete tatsächlich ausgeführt wurde.

--- 
title: "Scroll to row in datatable" 
date: "20 december 2017" 
output: html_document 
runtime: shiny 
--- 

```{r setup, include=FALSE} 
knitr::opts_chunk$set(echo = TRUE) 
``` 

## Datatable automatically scroll to given row 
The goal is to have a datatable rendered in a flexdashboard. Upon selecting a point in a scatter plot, the corresponding row in the table gets selected and needs to be scrolled into view. Selecting a row by clicking a point in a plot (with ggplot) works, but scrolling will not. 

Preferably without using shinyApp(), since scrolling is a JavaScript functionality rather than a shiny one (?). 

```{r} 
library(dplyr) 
library(DT) 

# Generate random data 
df <- data.frame(matrix(runif(1000), ncol = 5)) 

# Render datatable with shiny 
DT::renderDataTable({ 
    DT::datatable(df, 
    extensions = 'Scroller', 
    # selection = 'single',     # Eventually only allow single selection 
    escape = FALSE, 
    # callback = JS('this.api().row(15).scrollTo();'),  # Attempt to use callback instead 
    options = list(scrollX = TRUE, 
       scrollY = 200, 
       paging = FALSE, 
       initComplete = JS('function() { 
            $(this.api().table().row(15).node()).addClass("selected"); 
            this.api().table().row(15).scrollTo(); 
            alert("scrolled");}')))}, 
    server = TRUE) # Setting server = TRUE results in the selection with initComplete breaking 

``` 

Was ich bemerkt habe ist, dass, wenn Sie die Tabelle in der previously linked example bewegen Sie den Text am unteren Rand tatsächlich aktualisieren und sagen „1 bis 6 von 20 Einträge angezeigt“ oder „Zeige 6 bis 11 von 20 Einträge“ usw. Dies passiert nicht in meinem Beispiel für eine Datentabelle, die immer 1 bis 200 von 200 Einträgen anzeigt. Das führt mich zu der Annahme, dass es nicht zu der angegebenen Zeile scrollt, weil alles schon "in Sicht" ist, obwohl es nicht wirklich ist.

Antwort

0

Sie müssen scroller = TRUE und paging = TRUE im datatable()options Argument festlegen. Dies funktioniert bei mir:

--- 
title: "Scroll to row in datatable" 
date: "20 december 2017" 
output: html_document 
runtime: shiny 
--- 

```{r setup, include=FALSE} 
knitr::opts_chunk$set(echo = TRUE) 
``` 

## Datatable automatically scroll to given row 
The goal is to have a datatable rendered in a flexdashboard. Upon selecting a point in a scatter plot, the corresponding row in the table gets selected and needs to be scrolled into view. Selecting a row by clicking a point in a plot (with ggplot) works, but scrolling will not. 

Preferably without using shinyApp(), since scrolling is a JavaScript functionality rather than a shiny one (?). 

```{r} 
library(dplyr) 
library(DT) 

# Generate random data 
df <- data.frame(matrix(runif(1000), ncol = 5)) 

# Render datatable with shiny 
DT::renderDataTable({ 
    DT::datatable(df, 
    extensions = 'Scroller', 
    # selection = 'single',     # Eventually only allow single selection 
    escape = FALSE, 
    # callback = JS('this.api().row(15).scrollTo();'),  # Attempt to use callback instead 
    options = list(scrollX = TRUE, 
       scrollY = 200, 
       paging = TRUE, 
       scroller = TRUE, 
       initComplete = JS('function() { 
            $(this.api().table().row(15).node()).addClass("selected"); 
            this.api().table().row(15).scrollTo(); 
            alert("scrolled");}')))}, 
    server = TRUE) # Setting server = TRUE results in the selection with initComplete breaking 
Verwandte Themen