【问题标题】:How to get the cursor position in a Shiny textareaInput如何在闪亮的 textareaInput 中获取光标位置
【发布时间】:2018-05-20 07:19:22
【问题描述】:

有谁知道我如何在闪亮的应用程序中获取 textAreaInput 中的光标位置?

library(shiny)

ui <- fluidPage(
  textAreaInput("hop"
                ,label="textarea",value = "Supercalifragilisticexpialidocious"),
  verbatimTextOutput("out")
)

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

  output$out <- renderText({
    "here I would like to get the cursor position (an interger?) \n inside que textArea"

  })

}

shinyApp(ui, server)

我想我必须使用 javascript,但我不知道从哪里开始。

问候

【问题讨论】:

    标签: javascript r shiny


    【解决方案1】:

    这是我找到的解决方案:

    library(shiny)
    
    ui <- fluidPage(tags$head(tags$script(
      'Shiny.addCustomMessageHandler("prout",
      function(NULL) {
    
       var ctl = document.getElementById("hop");
        var startPos = ctl.selectionStart;
      var endPos = ctl.selectionEnd;
      alert(startPos + ", " + endPos);
    
      });'
        )),
      textAreaInput("hop"
                    ,label="textarea",value = "Supercalifragilisticexpialidocious"),
      verbatimTextOutput("out"),
      actionButton("hop","hop")
    )
    
    server <- function(input, output, session) {
    
      output$out <- renderText({
        "here I would like to get the cursor position (an interger?) \n inside que textArea"
    
      })
    
      observeEvent(input$hop,{
        message("hop")
        session$sendCustomMessage(type="prout",message=list(NULL))
      })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-08
      • 2020-02-20
      • 1970-01-01
      • 2011-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多