【问题标题】:How to fill fields with a single click on a table row with ReactJS如何使用 ReactJS 通过单击表格行来填充字段
【发布时间】:2019-11-11 21:40:03
【问题描述】:

我正在制作一个带有简单 CRUD 功能的反应应用程序。在我的环境中,我使用了一个名为 react bootstrap 2 的框架。

链接:https://react-bootstrap-table.github.io/react-bootstrap-table2/docs/getting-started.html

我有一个表格,用户可以填写信息:

<form id="car-form" onSubmit={this.handleSubmit}>
    <input type="hidden" name="carId" />
    <div className="row">
        <div className="form-group col-sm-3">
            <label>Brand</label>
            <input type="text" className="form-control" placeholder="Enter brand" value={this.state.brand} onChange={this.handleChange} />
        </div>
    </div>
    <div className="row">
        <div className="form-group col-sm-3">
            <label>Model</label>
            <input type="text" className="form-control" placeholder="Enter model" value={this.state.model} onChange={this.handleChange} />
        </div>
    </div>
    <div className="row">
        <div className="form-group col-sm-3">
            <label>Color</label>
            <input type="text" className="form-control" placeholder="Enter color" value={this.state.color} onChange={this.handleChange} />
        </div>
    </div>
    <div className="row">
        <div className="form-group col-sm-3">
            <label>TopSpeed</label>
            <input type="number" className="form-control" placeholder="Enter speed" value={this.state.topSpeed} onChange={this.handleChange} />
        </div>
    </div>
    <div className="btn-group mr-2">
        <button type="submit" className="btn btn-danger mr-1">Save changes</button>
        <button type="reset" className="btn btn-danger mr-1">New record</button>
        <button type="submit" className="btn btn-danger mr-1">Delete record</button>
    </div>
</form>

用户可以添加汽车,这可以与 .NET 核心后端一起正常工作。我有一个来自官方 react bootstrap 2 文档的功能来选择一行。

const rowEvents = {
    onClick: (e, row, rowIndex) => {
        console.log(`clicked on row with index: ${rowIndex}`);
    }
};

当我点击一行时,我得到了正确的索引号。现在,当我单击一行时,我想用 data 填充字段。

这是我的 handleChange 方法

handleChange(event) {
        this.setState({
            brand: event.target.brand,
            model: event.target.model,
            color: event.target.color,
            topspeed: event.target.top
        })
};

当我点击一行时它仍然不起作用。

当我点击一行时如何填写字段?

我使用的解决方案:

How do I programatically fill input field value with React?

【问题讨论】:

  • 点击你想在表格中填写数据的行对吗?
  • 是的!所有信息都必须显示在字段中
  • 你在哪里存储数据?
  • 数据进入 SQL 数据库,这在后端运行良好。我使用 React bootstrap 2 请参阅帖子中的链接。
  • 您正在使用 this.state 中的字段值。没有 setState 就无法更改它

标签: html reactjs twitter-bootstrap react-native asp.net-core


【解决方案1】:

假设 this.state.cars 是您存储汽车数据的位置。单击该行将您的功能更改为

const rowEvents = {
    onClick: (e, row, rowIndex) => {
                this.setState({
        brand: this.state.cars[rowIndex].brand,
        model: this.state.cars[rowIndex].model,
        color: this.state.cars[rowIndex].color,
        topspeed: this.state.cars[rowIndex].top
    })
    }
};

并将您的 rowEvents 复制到 render()

【讨论】:

  • 感谢您的宝贵时间!我收到以下错误TypeError: Cannot read property 'setState' of undefined 请记住 rowEvents 是在类之外声明的。
  • 存储数据的位置
  • 你在使用 BootstrapTable 组件吗
猜你喜欢
  • 2016-06-16
  • 2013-05-07
  • 2013-09-04
  • 1970-01-01
  • 1970-01-01
  • 2021-03-28
  • 2020-08-12
  • 2018-12-02
  • 1970-01-01
相关资源
最近更新 更多