我正在试验shinyjqui套餐:

一个特别有趣的函数是orderInput().使用connect参数,我们可以拖放项目,如下所示:https://cran.r-project.org/web/packages/shinyjqui/vignettes/orderInput.html

我试图将项目拖放到网格表中,但不幸的是,它没有按预期工作.

以下是我到目前为止开发的代码:


library(shiny)
library(shinyjqui)



connections <- c('my_grid') # id of the grid table


ui <- fluidPage(
 
  # some styling
   tags$head(
    tags$style(HTML("
      .grid-table {
        width: 150px;
        border-collapse: collapse;
      }
      .grid-cell {
        width: 100%;
        height: 50px;
        border: 1px solid black;
        background-color: white;
        text-align: center;
        margin: 0;
        padding: 0;
      }
      .grid-cell-text {
        display: flex;
        align-items: center;
        justify-content: center;
        height: 100%;
        background-color: steelblue;
        color: white;
        font-size: 18px;
      }
      .droppable-cell {
        background-color: lightgray;
      }
      .table-container {
        display: flex;
        position: absolute;
        left: 550px;
        top: 30px;
        margin-top: 0px;
        overflow: hidden;
      }
    "))
  ),
  
  div(class = "table-container",
      div(class = "grid-table", id = "my_grid",
          div(class = "grid-row",
              div(class = "grid-cell grid-cell-text", "my_grid"),
              div(id = "droppable_cell_1", class = "grid-cell droppable-cell", ""),
              div(id = "droppable_cell_2", class = "grid-cell droppable-cell", ""),
              div(id = "droppable_cell_3", class = "grid-cell droppable-cell", ""),
              div(id = "droppable_cell_4", class = "grid-cell droppable-cell", ""),
              div(id = "droppable_cell_5", class = "grid-cell droppable-cell", ""),
              div(id = "droppable_cell_6", class = "grid-cell droppable-cell", ""),
              div(id = "droppable_cell_7", class = "grid-cell droppable-cell", "")
          )
      ),
      
      orderInput('letters', 'Letters', items = LETTERS[1:7],
                 connect = connections) # defined above
  )
)

server <- function(input, output, session) {
  
  
}

shinyApp(ui, server)


推荐答案

下面是一个有两个变化的例子:第一,orderInputconnections参数不应该引用网格表,而应该引用网格单元格:

connections <- paste0("droppable_cell_", 1:7)

其次,每个网格单元必须是sortable,它连接到letters,并且在将字母放在其上时具有append().由于我目前没有让它与shinyjqui个内置函数一起运行,所以我用一个小的定制JS来实现它:

$(function() {
    $('[id^=droppable_cell]').sortable({
        connectWith: '#letters',
        drop: function(event, ui) {
                  $(this).append(ui.draggable);
              }
    })
});

enter image description here

我还以.grid-cell的样式将padding设置为5px,这样字母就会出现在单元格的中间.

library(shiny)
library(shinyjqui)

connections <- paste0("droppable_cell_", 1:7) # id of the grid cells

ui <- fluidPage(
    tags$head(tags$script(
        JS(
            "
                       $(function() {
                           $('[id^=droppable_cell]').sortable({
                               connectWith: '#letters',
                               drop: function(event, ui) {
                                         $(this).append(ui.draggable);
                                     }
                           })
                        });
                        "
        )
    ),
    # some styling
    tags$style(
        HTML(
            "
            .grid-table {
                width: 150px;
                border-collapse: collapse;
            }
            .grid-cell {
                width: 100%;
                height: 50px;
                border: 1px solid black;
                background-color: white;
                text-align: center;
                margin: 0;
                padding: 5px;
            }
            .grid-cell-text {
                display: flex;
                align-items: center;
                justify-content: center;
                height: 100%;
                background-color: steelblue;
                color: white;
                font-size: 18px;
            }
            .droppable-cell {
                background-color: lightgray;
            }
            .table-container {
                display: flex;
                position: absolute;
                left: 550px;
                top: 30px;
                margin-top: 0px;
                overflow: hidden;
            }
            "
        )
    )),
    
    div(
        class = "table-container",
        div(
            class = "grid-table",
            id = "my_grid",
            div(
                class = "grid-row",
                div(class = "grid-cell grid-cell-text", "my_grid"),
                div(id = "droppable_cell_1", class = "grid-cell droppable-cell", ""),
                div(id = "droppable_cell_2", class = "grid-cell droppable-cell", ""),
                div(id = "droppable_cell_3", class = "grid-cell droppable-cell", ""),
                div(id = "droppable_cell_4", class = "grid-cell droppable-cell", ""),
                div(id = "droppable_cell_5", class = "grid-cell droppable-cell", ""),
                div(id = "droppable_cell_6", class = "grid-cell droppable-cell", ""),
                div(id = "droppable_cell_7", class = "grid-cell droppable-cell", "")
            )
        ),
        
        orderInput('letters', 'Letters', items = LETTERS[1:7],
                   connect = connections) # defined above
    )
)

server <- function(input, output, session) {
    
}

shinyApp(ui, server)

Html相关问答推荐

如何使div按钮链接到另一个网页

如何从卷轴中排除粘性元素?

悬停效果在新西兰表上不起作用

天使17:令人惊叹的动画

如果DIV没有特定的sibling 姐妹,则以CSS为目标

使用`<;use>;`与`<;img>;`时呈现的SVG大小不同

从网页中提取URL

如何从多个TO中获取一个范围标记以溢出其父标记

如何删除FONT创建的文本下方的空格

使用单个粗体字符串向段落添加页边空白

将三个div转换为显示flex

理解图像与其内容框之间的关系

网页设计不适合移动设备

从 Vue 应用程序的容器元素渲染 HTML

如何只为 Navbar 中的一个元素提供动画,其余元素没有使用 css 的动画

如何调整网格中单元格的高度?

单击时 HTML 锚元素不重定向到相对路径

让 Iframe 覆盖整个页面

CSS 斜角与 3 行对齐

html tailwindcss 给 li 标签添加边框