gpt4 book ai didi

javascript - 在 Shiny 的R中自动调整textAreaInput的大小

转载 作者:行者123 更新时间:2023-12-02 22:16:41 24 4
gpt4 key购买 nike

我正在努力适应this SO answer关于如何通过javascript为shiny R自动调整文本区域输入的大小。理想情况下,我想避免使用shinyJS等帮助程序包。

首先,我尝试了一个纯 JavaScript 实现,其中我将 JavaScript 按原样加载到应用程序中(方法 1)。然后我尝试从 Shiny 的observeEvent中触发javascript函数(方法2)。

这两种方法都不起作用。看来我漏掉了一些东西。

方法 1:

library(shiny)

jsCode1 <- "
var observe;
if (window.attachEvent) {
observe = function (element, event, handler) {
element.attachEvent('on'+event, handler);
};
}
else {
observe = function (element, event, handler) {
element.addEventListener(event, handler, false);
};
}
function init () {
var text = document.getElementById('text');
function resize () {
text.style.height = 'auto';
text.style.height = text.scrollHeight+'px';
}
/* 0-timeout to get the already changed text */
function delayedResize () {
window.setTimeout(resize, 0);
}
observe(text, 'change', resize);
observe(text, 'cut', delayedResize);
observe(text, 'paste', delayedResize);
observe(text, 'drop', delayedResize);
observe(text, 'keydown', delayedResize);

text.focus();
text.select();
resize();
}

init();
"

shinyApp(ui =

fluidPage(

tags$script(jsCode1),

tags$head(

tags$style("
textarea {
border: 0 none white;
overflow: hidden;
padding: 0;
outline: none;
background-color: #D0D0D0;
}
"
)

),

shiny::tagAppendAttributes(
textAreaInput(inputId = "text",
label = "Enter text here",
placeholder = "insert your text here",
width = "100%"),
style = "width: 100%;")

),

server = function(input, output, session) {

}
)

方法 2:

library(shiny)

jsCode2 <- "

Shiny.addCustomMessageHandler('handler1', init);

function init (el) {
var text = document.getElementById(el);
function resize () {
text.style.height = 'auto';
text.style.height = text.scrollHeight+'px';
}
/* 0-timeout to get the already changed text */
function delayedResize () {
window.setTimeout(resize, 0);
}
observe(text, 'change', resize);
observe(text, 'cut', delayedResize);
observe(text, 'paste', delayedResize);
observe(text, 'drop', delayedResize);
observe(text, 'keydown', delayedResize);

text.focus();
text.select();
resize();
}"

shinyApp(ui =

fluidPage(

tags$script(jsCode2),

tags$head(

tags$style("
textarea {
border: 0 none white;
overflow: hidden;
padding: 0;
outline: none;
background-color: #D0D0D0;
}
"
)

),

shiny::tagAppendAttributes(
textAreaInput(inputId = "text",
label = "Enter text here",
placeholder = "insert your text here",
width = "100%"),
style = "width: 100%;")

),

server = function(input, output, session) {

observeEvent(input$text,{

session$sendCustomMessage("handler1", message = "text")

})

}
)

最佳答案

可能是这样的情况:您尝试将调整大小事件附加到 textinput(),然后再将后者添加到 DOM。

我添加了一个事件监听器,它会等到 DOM 加载完毕后再附加调整大小事件。

document.addEventListener('DOMContentLoaded', function(event) {...})

shinyjs 会自动为您执行此操作。添加上面的事件监听器,就可以在不使用shinyjs的情况下使其工作。

Javascript代码:

jsCode <- "document.addEventListener('DOMContentLoaded', function(event) {
var observe;
if (window.attachEvent) {
observe = function (element, event, handler) {
element.attachEvent('on'+event, handler);
};
}
else {
observe = function (element, event, handler) {
element.addEventListener(event, handler, false);
};
}
function init () {
var text = document.getElementById('text');
function resize () {
text.style.height = 'auto';
text.style.height = text.scrollHeight+'px';
}
/* 0-timeout to get the already changed text */
function delayedResize () {
window.setTimeout(resize, 0);
}
observe(text, 'change', resize);
observe(text, 'cut', delayedResize);
observe(text, 'paste', delayedResize);
observe(text, 'drop', delayedResize);
observe(text, 'keydown', delayedResize);

text.focus();
text.select();
resize();
};init()
})
"

应用程序:

library(shiny)

ui <- fluidPage(
shiny::tags$script(jsCode),
textAreaInput(inputId = "text", label = "a", value = "b")
)

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

shinyApp(ui, server)

关于javascript - 在 Shiny 的R中自动调整textAreaInput的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59365261/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com