Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pasting multiple items into selectizeInput #1663

Closed
b-dawes opened this issue Apr 11, 2017 · 4 comments
Closed

Pasting multiple items into selectizeInput #1663

b-dawes opened this issue Apr 11, 2017 · 4 comments

Comments

@b-dawes
Copy link

b-dawes commented Apr 11, 2017

I'm building an application which uses selectize inputs with multiple = TRUE. I was looking for a way to allow users to paste in a comma separated list of items instead of having to enter each one in individually. It looks like selectize.js supports this as of version 0.12.0 (see here).

Is there a way to do this in Shiny? I tried setting both delimiter and splitOn in options but neither seemed to have any effect.

@corinnend
Copy link

Hello - were you able to find a solution? I'm running into the same issue.

@wch
Copy link
Collaborator

wch commented Sep 27, 2017

It looks like you have to add a create function, like so:

shinyApp(
  ui = fluidPage(
    selectizeInput("x", "Choose:",
      letters,
      multiple = TRUE,
      options = list(
        delimiter = ',',
        create = I("function(input, callback){
          return {
            value: input,
            text: input
           };
        }")
      )
    )
  ),
  server = function(input, output) { }
)
# Copy and paste a,b,c,d

I found this by trying it with just Javascript here: https://jsfiddle.net/winstonchang/462qjrru/1/

Note that this will also attempt to create entries that don't exist. That's how the delimiter option works, according to https://github.com/selectize/selectize.js/blob/master/docs/usage.md.

Instead of delimiter, you can pass a regular expression to splitOn: https://jsfiddle.net/winstonchang/462qjrru/3/

However, to get it from R to Javascript takes a little more work - you can't pass a Javascript regex directly, but you can pass an immediately-invoked function that returns a regex.

shinyApp(
  ui = fluidPage(
    selectizeInput("x", "Choose:",
      letters,
      multiple = TRUE,
      options = list(
        splitOn = I("(function() { return /[,;]/; })()"),
        create = I("function(input, callback){
          return {
            value: input,
            text: input
           };
        }")
      )
    )
  ),
  server = function(input, output) {
  }
)

# Copy and paste 1,2,3,4,5;6;7

@wch wch closed this as completed Sep 27, 2017
@saif003
Copy link

saif003 commented Mar 14, 2018

@wch This, fails if you try entering case sensitive characters, try entering 'a,b,c,d' to this fiddle.

@JacobBumgarner
Copy link

JacobBumgarner commented Feb 22, 2023

Here is an example of built off of @wch's solution that allows paste input to selectize dropdowns, but this version only accepts values that are present in the options list.

shinyApp(
  ui = fluidPage(
    selectizeInput(
      "x",
      "Choose:",
      letters,
      multiple = TRUE,
      options = list(
        splitOn = I("(function() { return /[,; ]/; })()"),
        create = I("function(input, callback){
                   var selectize_options = $('#x')[0].selectize.options;
                   if (selectize_options[input] == undefined) {
                    return {};
                   } else {
                    return {value: input, label: input};
                   }}")
      )
    )
  ),
server = function(input, output, session) {
}
)

# example paste: a,1,b,2,c,3,d,xyz;f hello g

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants