简体   繁体   English

如何从 reactjs 中的 select2 获取值

[英]how to get value from select2 in reactjs

I am using adminlte HTML theme, I converted this theme into reactjs, everything is working fine except select2(Multi-select)我正在使用 adminlte HTML 主题,我将这个主题转换为 reactjs,除了 select2(多选)之外一切正常

onselect I am trying to trigger the handler that is userIdHandler but it does not trigger onselect 我正在尝试触发 userIdHandler 的处理程序,但它不会触发

There is two cases:有两种情况:

  1. user name with multi-select: userIdHandler not working多选用户名:userIdHandler 不工作
  2. Status with single select: userIdHandler working单个 select 的状态:userIdHandler 正在工作

please help me to trigger userIdHandler from multi-select also请帮助我也从多选中触发 userIdHandler

import React, { useEffect, useState } from "react";
import { getTeam } from "../services/ApiCall/attendanceApiCall";

export default function TeamattendanceComponent() {
  const [team, setTeam] = useState([]);

  const userIdHandler = (e) => {
    console.log("hi", e.target.value);
  };

  useEffect(() => {
    const script = document.createElement("script");
    script.src = "myjs/content.js";
    script.async = true;
    document.body.appendChild(script);

    getTeam().then((res) => {
      console.log(res.data);
      setTeam(res.data);
    });
  }, []);
  return (
    <div className="content-wrapper">
      {/* Content Header (Page header) */}
      <div className="card">
        <div className="card-body row">
          <div className="col-4">
            <div className="form-group ">
              <label>User Name</label>
              
              <select
                id
                className="form-control select2"
                multiple="multiple"
                data-placeholder="Select a State"
                style={{ width: "100%" }}
                onChange={userIdHandler}
              >
                {team.map((user, key) => {
                  console.log("team data", team);
                  return (
                    <option key={key} value={user._id}>
                      {user.name}
                    </option>
                  );
                })}
              </select>
            </div>
          </div>

          <div className="col-4">
            <div className="form-group">
              <label>Status</label>
              <select id className="form-control" onChange={userIdHandler}>
                <option>Select</option>
                <option>ON</option>
                <option>OFF</option>
              </select>
            </div>
          </div>

          <div className="col-4">
            <div className="form-group">
              <label className="hidden-xs">&nbsp;</label>
              <input
                type="submit"
                className="btn btn-primary form-control"
                defaultValue="Filter"
              />
            </div>
          </div>
        </div>
      </div>
      {/* /.content */}
    </div>
  );
}

I'm afraid that you are not using a Select2 elements that's a regular select.恐怕您没有使用常规 select 的Select2元素。

First install react-select : npm i --save react-select首先安装react-select : npm i --save react-select

This is how you define a multiselect on Select2:这是在 Select2 上定义多选的方式:

import Select from 'react-select';

const options = [{label: "option 1", value: 1}, {label: "option 2", value: 2}];

<Select
    isMulti
    options={options}
    onChange={userIdHandler}
  />

And then change your userIdHandler` function to this:然后将您的 userIdHandler` function 更改为:

const userIdHandler = (value) => {
    console.log(value);
  };

This way it should print you the label and value selected.这样它应该会打印出label和所选的value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM