最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Dynamically Select Options From Multiple Select Dropdown With Reactstrap In React js - Stack Overflow

matteradmin7PV0评论

I have the following function:

loadTag(data, td) {
    var tag = this.state.session.tag.map((el) => 
    $.inArray( el.name, data ) ? 
    <option key={el.name} value={el.key} defaultValue>{el.name}</option> : 
    <option key={el.name} value={el.key}>{el.name}</option>);
    ReactDOM.render(
      <Input type="select" className="dropselect_tag" name="tag" multiple>
        {tag}
      </Input>,
      td);
}

I want to be able to dynamically select mulitple options within the list of options provided into my select drop down. For my select drop down i'm using reactstrap and select2 plugins and it does initialize but with nothing selected.

I have tried this also:

loadTag(data, td) {
    var tag = this.state.session.tag.map((el) => 
    <option key={el.name} value={el.key}>{el.name}</option>);
    ReactDOM.render(
      <Input type="select" className="dropselect_tag" defaultValue={data} name="tag" multiple>
        {tag}
      </Input>,
      td);
}

But it doesn't produce any result. And I am very sure that i'm sending in a array like this ["item"]. I parse my array from my db and test like so:

 data = JSON.parse(data.replace(/&quot;/g,'"'));
 console.log(typeof data); 
 console.log(data);

Is there a way that i can pull this stuff out?

I have the following function:

loadTag(data, td) {
    var tag = this.state.session.tag.map((el) => 
    $.inArray( el.name, data ) ? 
    <option key={el.name} value={el.key} defaultValue>{el.name}</option> : 
    <option key={el.name} value={el.key}>{el.name}</option>);
    ReactDOM.render(
      <Input type="select" className="dropselect_tag" name="tag" multiple>
        {tag}
      </Input>,
      td);
}

I want to be able to dynamically select mulitple options within the list of options provided into my select drop down. For my select drop down i'm using reactstrap and select2 plugins and it does initialize but with nothing selected.

I have tried this also:

loadTag(data, td) {
    var tag = this.state.session.tag.map((el) => 
    <option key={el.name} value={el.key}>{el.name}</option>);
    ReactDOM.render(
      <Input type="select" className="dropselect_tag" defaultValue={data} name="tag" multiple>
        {tag}
      </Input>,
      td);
}

But it doesn't produce any result. And I am very sure that i'm sending in a array like this ["item"]. I parse my array from my db and test like so:

 data = JSON.parse(data.replace(/&quot;/g,'"'));
 console.log(typeof data); 
 console.log(data);

Is there a way that i can pull this stuff out?

Share Improve this question asked May 13, 2019 at 15:25 IkechukwuIkechukwu 1,1451 gold badge13 silver badges30 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

My advice is to use react-select, It is able to select mulitple elegantly. Can you give it a try?

npm install react-select

import React, { Component } from 'react'
import Select from 'react-select'

const options = [
  { value: 'chocolate', label: 'Chocolate' },
  { value: 'strawberry', label: 'Strawberry' },
  { value: 'vanilla', label: 'Vanilla' }
]

const MyComponent = () => (
  <Select options={options} />
)

react-select docs

I was able to resolve it this way:

loadTag(data, td) {
  data = JSON.parse(data.replace(/&quot;/g,'"'));
  var tag = this.state.session.tag.map((el) => <option key={el.index} value={el.index}>{el.index}</option>);
  ReactDOM.render(
      <Input type="select" multiple={true} defaultValue={data} className="dropselect_tag" name="tag">
        {tag}
      </Input>,
      td);
}

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far