在reactJs中添加动态状态

2024-03-26

我创建了一个 ReactJs 页面,允许管理员将用户添加到平台。现在,管理员可以在提交表单之前添加尽可能多的用户,而不是为每个新用户提交表单。默认情况下,会显示一个包含输入字段的表行,然后单击“添加”按钮,会添加一个新行,管理员可以填写必要的详细信息。

我想向每个新行的输入字段添加一个 onChange 事件。我不知道该怎么做,因为这是非常动态的。随着新行的添加,状态必须发生变化。我不知道如何更新此状态并为每行中的每个字段添加 onChange 事件。这是我现有的代码:

export default class Admins extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            errors : '',
            success : '',
            rows : [1]
        }
        this.addRow = this.addRow.bind(this);
        this.fetchRows = this.fetchRows.bind(this);
        this.removeRow = this.removeRow.bind(this);
    }
    addRow(){
        var last = this.state.rows[this.state.rows.length-1];
        var current = last + 1;
        this.setState({
            rows : this.state.rows.concat(current)
        });
    }
    removeRow(index){
        var array = this.state.rows;
        array.splice(index, 1)
        this.setState({
            rows : array
        })
    }
    fetchRows(){
        return this.state.rows.map((row, index) => (
            //console.log(row, index)
            <tr key={row}>
                <td className="text-center">
                    <button type="button" onClick={(e)=> this.removeRow(index)} data-toggle="tooltip" className="btn btn-xs btn-danger"
                            data-original-title=""><i className="fa fa-trash"></i>
                    </button>
                </td>
                <td>
                    <input type="text" className="form-control" onChange={}/>
                </td>
                <td>
                    <input type="text" className="form-control" onChange={}/>
                </td>
                <td>
                    <input type="text" className="form-control" onChange={}/>
                </td>
            </tr>
        ));
    }
    render(){
        return(
            <div>
                <Top/>
                <SideBar/>
                <div className="breadcrumb-holder">
                    <div className="container-fluid">
                        <ul className="breadcrumb">
                            <li className="breadcrumb-item"><Link to="/">Dashboard</Link></li>
                            <li className="breadcrumb-item active">Admins</li>
                        </ul>
                    </div>
                </div>
                <section className="forms">
                    <div className="container-fluid">
                        <header>
                            <h3 className="h5 display">Admins</h3>
                        </header>

                        <div className="row">
                            <div className="col-lg-6">
                                <h5 className="text-danger">{this.state.errors}</h5>
                                <h5 className="text-success">{this.state.success}</h5>
                            </div>
                        </div>
                        <div className="row">
                            <div className="col-lg-6">
                                <div className="card">
                                    <div className="card-header d-flex align-items-center">
                                        <h5></h5>
                                    </div>
                                    <div className="card-body">
                                        <table className="table table-bordered">
                                            <thead>
                                            <tr>
                                                <th  width="5%">Actions</th>
                                                <th>Name</th>
                                                <th>Email</th>
                                                <th>Password</th>
                                            </tr>
                                            </thead>
                                            <tbody>
                                                {this.fetchRows()}
                                                <tr>
                                                    <td className="text-center">
                                                        <button type="button" onClick={this.addRow} data-toggle="tooltip" className="btn btn-xs btn-primary"
                                                                data-original-title=""><i className="fa fa-plus"></i>
                                                        </button>
                                                    </td>
                                                </tr>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </section>
            </div>
        );
    }
}

你可以创建一个方法onChange这需要在eventindex已更改的行,并使用namevalue of the input结合改变了index of the row在数组中找出要更新的字段。

Example

class Admins extends React.Component {
  state = {
    errors: "",
    success: "",
    rows: []
  };

  addRow = () => {
    this.setState(previousState => {
      return {
        rows: [...previousState.rows, { name: "", email: "", password: "" }]
      };
    });
  };

  removeRow = index => {
    this.setState(previousState => {
      const rows = [...previousState.rows];
      rows.splice(index, 1);
      return { rows };
    });
  };

  onChange = (event, index) => {
    const { name, value } = event.target;

    this.setState(previousState => {
      const rows = [...previousState.rows];
      rows[index] = { ...rows[index], [name]: value };
      return { rows };
    });
  };

  render() {
    return (
      <div>
        <div className="breadcrumb-holder">
          <div className="container-fluid">
            <ul className="breadcrumb">
              <li className="breadcrumb-item active">Admins</li>
            </ul>
          </div>
        </div>
        <section className="forms">
          <div className="container-fluid">
            <header>
              <h3 className="h5 display">Admins</h3>
            </header>
            <div className="row">
              <div className="col-lg-6">
                <h5 className="text-danger">{this.state.errors}</h5>
                <h5 className="text-success">{this.state.success}</h5>
              </div>
            </div>
            <div className="row">
              <div className="col-lg-6">
                <div className="card">
                  <div className="card-header d-flex align-items-center">
                    <h5 />
                  </div>
                  <div className="card-body">
                    <table className="table table-bordered">
                      <thead>
                        <tr>
                          <th width="5%">Actions</th>
                          <th>Name</th>
                          <th>Email</th>
                          <th>Password</th>
                        </tr>
                      </thead>
                      <tbody>
                        {this.state.rows.map((row, index) => (
                          <tr key={row}>
                            <td className="text-center">
                              <button
                                type="button"
                                onClick={e => this.removeRow(index)}
                                data-toggle="tooltip"
                                className="btn btn-xs btn-danger"
                                data-original-title=""
                              >
                                <i className="fa fa-trash" />
                              </button>
                            </td>
                            <td>
                              <input
                                type="text"
                                name="name"
                                className="form-control"
                                value={row.name}
                                onChange={e => this.onChange(e, index)}
                              />
                            </td>
                            <td>
                              <input
                                type="text"
                                name="email"
                                className="form-control"
                                value={row.email}
                                onChange={e => this.onChange(e, index)}
                              />
                            </td>
                            <td>
                              <input
                                type="text"
                                name="password"
                                className="form-control"
                                value={row.password}
                                onChange={e => this.onChange(e, index)}
                              />
                            </td>
                          </tr>
                        ))}
                        <tr>
                          <td className="text-center">
                            <button
                              type="button"
                              onClick={this.addRow}
                              data-toggle="tooltip"
                              className="btn btn-xs btn-primary"
                              data-original-title=""
                            >
                              <i className="fa fa-plus" />
                            </button>
                          </td>
                        </tr>
                      </tbody>
                    </table>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </section>
      </div>
    );
  }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在reactJs中添加动态状态 的相关文章

随机推荐

  • JAXB-XJC X属性访问器

    根据 JAXB 规范http jaxb java net 2 2 4 docs xjc html http jaxb java net 2 2 4 docs xjc html如果您想运行 JAXB XJC 编译器 您可能传递的扩展 参数之一
  • OpsHub VSO 迁移 - DataValidationException - 测试套件已存在

    在 OpsHub 从本地 TFS 2013 3 服务器迁移到 VSO 期间 我收到来自 OpsHub 的错误消息 com opshub exceptions DataValidationException OpbsHug 012017 名称
  • 从转义 ASCII 序列中读取 UTF8/UNICODE 字符

    我的文件中有以下名称 我需要将该字符串读取为 UTF8 编码的字符串 因此 test 303 246 303 270 303 245 txt 我需要获得以下信息 test txt 你知道如何使用 C 来实现这一点吗 假设你有这个字符串 st
  • 将输入传递到批处理文件中的程序提示符

    我正在使用 mpich2 并行运行模拟 我的工作站有相当严格的安全措施 每次运行模拟时都必须使用新密码进行注册 我必须输入 mpiexec register 然后提示我输入用户名 然后提示我输入密码 不幸的是 似乎没有办法在一行上将用户 通
  • 通过 Javascript 返回视图 MVC3 Razor 将值传递给控制器

    我是 MVC 的新手 我试图将使用地理位置获得的经度和纬度值传递给我的控制器 以便我可以使用这些值来识别并从数据库中提取正确的数据 这是我的 JavaScript function auto locate alert called from
  • 如何应用圆角边框来突出显示/选择

    我用过视觉工作室在线 http visualstudio com对于一个项目来说有一段时间 他们将圆形边框应用于在线代码查看器中的选择的方式非常有趣 我尝试检查该元素并寻找某种自定义 CSS 但没有成功 我有一种感觉 这需要一些复杂的 技巧
  • 如何在 makefile 中定义变量,然后在 Fortran 代码中使用它

    我试图在 makefile 中定义一个变量 然后根据是否设置了该变量 更改在我的 Fortran 例程中编译的代码块 简单的例子我无法工作 program test implicit none integer a ifdef MYVAR a
  • 在WebView中显示Android资源文件?

    我看过关于从资产提供 WebView 页面问题的各种讨论 但没有一个似乎是明确的 我希望能够使用 webview 来显示存储在项目资产中的 html 以及包含的 css 文件 我发现 wv loadUrl file android asse
  • 通过标题获取 NSMenu 树的 NSMenuItem

    我有一个NSMenu 比如说主菜单 有很多NSMenu里面有 并且NSMenuItem处于不同级别 我希望能够获取指定树路径的 NSMenuItem 实例 其中包含相应 NSMenus NSMenuItems 的标题 例子 Menu Fil
  • 如何更改输入类型=“日期”日期选择器的外观?

    我想将输入 type date 日期选择器的默认外观从箭头更改为日历图标 并使其始终可见 谷歌搜索这个问题并没有透露什么信息 我在 2012 年看到了下面的帖子 上面说这是不可能的 事情有变化吗 https developers googl
  • 为什么有些对象在 Objective-C 中使用前不需要初始化?

    为什么有些对象在 Objective C 中使用前不需要初始化 例如这是为什么NSDate today NSDate date legal 它们在内部初始化date方法 这是在 Objective C 中创建自动释放对象的常见方法 这种形式
  • 按钮标签中的 R 闪亮断线

    请看下面的玩具示例 任何帮助将不胜感激 谢谢 shinyApp ui fluidPage actionButton btnId I want a line break here br since the label is too long
  • 如何在 Android 上正确初始化和终止 EGL

    虽然有很多关于在 Android 上使用 OpenGL ES 的示例 但所有这些示例在 EGL 的初始化 终止方面似乎都是不正确的 即使是 Android SDK NDK 附带的示例 问题的根源在于 Android 应用程序模型 这使得 E
  • 在 Linq to SQL 中重写 NHibernate 应用程序

    我有一个使用 NHibernate 编写的过时的应用程序 现在我想重写它 包括新功能和模型的重大变化 使用 Linq to SQL 而不是 NHibernate 的主要缺点是什么 使用 LINQ to SQL 可能会出现哪些问题 将 Dat
  • GIT - 无法忽略 .suo 文件

    我正在尝试与同事一起在用 C 编写的应用程序中使用 Git 我们已将条目 project1 suo 添加到 gitignore 文件中 但每次我们必须提交该项目时 Git 似乎都会告诉我们也提交文件 project1 suo 我们尝试了很多
  • 安卓smali问题

    我目前正在对 smali 代码混淆器 进行一些研究 并且目前正在尝试熟悉反编译的源代码 为此 我创建了一个简单的应用程序并通过 smali 对其进行了反编译 我现在试图了解反编译的源代码 以改进和比较稍后使用代码混淆器后的安全性 针对反编译
  • 下载失败:未安装自制安装的“curl”

    命令 brew install php 每次我想在 Mac 上更新 php 时 我总是会收到如下错误 Downloading https www freetds org files stable freetds 1 3 13 tar bz2
  • Python 的 time.clock() 与 time.time() 的准确性?

    Python 中计时用哪个更好 time clock 还是 time time 哪一个提供更高的准确性 例如 start time clock do something elapsed time clock start vs start t
  • Google App Engine 始终开启功能

    有人可以解释一下 Google App Engine 中的 始终开启 功能是什么吗 它有什么用 举例说明将不胜感激 始终开启功能是否适用于任务队列 基本上 Google 会在没有流量时关闭应用程序引擎应用程序实例 通过此功能 您可以支付额外
  • 在reactJs中添加动态状态

    我创建了一个 ReactJs 页面 允许管理员将用户添加到平台 现在 管理员可以在提交表单之前添加尽可能多的用户 而不是为每个新用户提交表单 默认情况下 会显示一个包含输入字段的表行 然后单击 添加 按钮 会添加一个新行 管理员可以填写必要