简体   繁体   English

单击 R 中的传单地图按钮时显示和隐藏数据表

[英]Show and hide Datatable upon button click on leaflet map in R

I drawn a map with a 'datatable' using some packages in R. To have more space in leaflet map, I need a button to show/hide the datatable part.我使用 R 中的一些包绘制了一张带有“数据表”的地图。为了在传单地图中有更多空间,我需要一个按钮来显示/隐藏数据表部分。 How to do it?怎么做? The R code is below: R代码如下:

library(leaflet)
library(DT)
library(crosstalk)

df <- read.csv(textConnection(
    "Name,Lat,Long
    Samurai Noodle,47.597131,-122.327298
    Kukai Ramen,47.6154,-122.327157
    Tsukushinbo,47.59987,-122.326726"
))

sdf <- SharedData$new(df, df$Name)

lflt<-leaflet(sdf) %>% addTiles() %>%
    addMarkers(~ Long, ~ Lat)%>%addEasyButtonBar(
easyButton(
 icon = htmltools::span(class = "star", htmltools::HTML("+")),
 onClick = JS("function(btn, map){ alert(\"Button 1\");}")))

bscols(lflt,      datatable(sdf, width = "100%"))

You could play around with the javascript and css settings?您可以尝试使用 javascript 和 css 设置吗? Something like below.像下面这样的东西。 This maybe isn't the best method, but it just changes the data table display to 'none' when the button is clicked.这可能不是最好的方法,但它只是在单击按钮时将数据表显示更改为“无”。 And also change the width of the map to 100%.并且还将地图的宽度更改为 100%。

lflt<-leaflet(sdf, elementId = "map") %>% addTiles() %>%
    addMarkers(~ Long, ~ Lat)%>%addEasyButtonBar(
easyButton(
 icon = htmltools::span(class = "star", htmltools::HTML("+")),
 onClick = JS("function(btn, map){ 
    let table = document.getElementsByClassName('datatables')[0];
    let map2 = document.getElementById('map');
    table.style.display = table.style.display == 'none' ? 'block' : 'none';
    map2.parentNode.style.width = table.style.display == 'none' ? '100%' : '50%';
    map2.style.position = map2.style.position == '' ? 'relative' : '';
    map.invalidateSize()
              }")))

bscols(lflt,      datatable(sdf))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM