首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用shinyjs::reset重置自定义输入shiny R

使用shinyjs::reset重置自定义输入shiny R
EN

Stack Overflow用户
提问于 2019-11-19 07:46:18
回答 1查看 444关注 0票数 2

我有一个用于自定义输入的闪亮模块,并且我正在尝试添加一个按钮,以便在每次单击此按钮时重置此自定义输入(包括界面和服务器)。下面是使用shinyjs::reset的示例:

app.R

代码语言:javascript
复制
    library(shinyjs)
library(rintrojs)
library(shinydashboard)
library(shinyWidgets)

source("customTextInput.R", local = TRUE, encoding = "UTF-8")

ui <- fluidPage(

    useShinyjs(),

    # Assembling page
    dashboardPage(

        # Assembling header
        dashboardHeader(title = "Custom Inputs", titleWidth = 1294), 

        # Assembling sidebar
        dashboardSidebar(

            sidebarMenu(
                menuItem("Custom inputs reset", tabName = "custom", icon = icon("search"))
            )

        ),
        # Assembling the body of the page
        dashboardBody(

            tabItems(
                tabItem(tabName = "custom",
                        br(),
                        br(),
                        customTextInputUI("text1"),
                        fluidPage(

                            textInput(inputId = "text2", label = "Native text input", width = "100%"),
                            actionButton(inputId = "reseter1", label = "Reset custom"),
                            actionButton(inputId = "reseter2", label = "Reset native")

                        ),
                )

            ) 

        )

    )
)

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

    {### Reset -----

        observe({

            shinyjs::onclick("reseter1", {
                reset("text1")
            })

        })

        observe({

            shinyjs::onclick("reseter2", {
                reset("text2")
            })

        })

    }

}

shinyApp(ui, server)

customTextInput.R

代码语言:javascript
复制
library(shinyjs)


{# UI Module -----

    customTextInputUI <- function(id, addWidth = "100%", addHeight="64px", addTop="5px", addValue = "") {

        if (is.null(addValue)){addValue <- ""}

        ns <- NS(id)

        return(

                fluidPage(

                    HTML(

                        sprintf(

"
<form id='%s' autocomplete='off' class = 'form-group shiny-input-container' style = 'padding-top:%s; width:%s; height:%s;'>
    <label for='%s'>Custom text input</label>
        <input charset='UTF-8' type='text' class = 'form-control' 
        id='%s' data-slots=' ' value = \"%s\"  
        data-shinyjs-resettable-id='%s' data-shinyjs-resettable-type='Text' data-shinyjs-resettable-value='' required>
</form>", ns('textBloco'), addTop, addWidth, addHeight, ns('textInput'), ns('textInput'), addValue, ns('textInput')))

                )

          )

    }

}

在这种情况下,reseter2可以重置闪亮的text2原生textInput,但是reseter1不能重置自定义输入text1。

为什么会有如此意想不到的行为?有没有解决的办法,比如使用纯javascript?

提前谢谢你!

EN

回答 1

Stack Overflow用户

发布于 2019-11-21 09:03:10

如果您检查text1的输入id,那么您将看到它显示为text1-textBloco

所以,为了让你的reset正常工作,你可以将它更新为reset("text1-textBloco"),然后它就应该可以工作了。

或者,您可以在sprintf中包含ns('textBloco')customerTextInput.R中将其更新为仅id,然后在app.R中使用reset("text1")。所以sprintf()应该是这样的:

代码语言:javascript
复制
sprintf(    
            "
<form id='%s' autocomplete='off' class = 'form-group shiny-input-container' style = 'padding-top:%s; width:%s; height:%s;'>
    <label for='%s'>Custom text input</label>
        <input charset='UTF-8' type='text' class = 'form-control' 
        id='%s' data-slots=' ' value = \"%s\"  
        data-shinyjs-resettable-id='%s' data-shinyjs-resettable-type='Text' data-shinyjs-resettable-value='' required>
</form>", id, addTop, addWidth, addHeight, ns('textInput'), ns('textInput'), addValue, ns('textInput')))

      )
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58924585

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档