简体   繁体   English

在reactjs中处理来自数组的动态onchange事件

[英]handing dynamic onchange event from array in reactjs

I am trying to handle dynamic onchange name input within an array.I am calling a function for setting the total number of input.Based on the list size no of input boxes will be placed.我正在尝试处理数组中的动态 onchange 名称输入。我正在调用一个函数来设置输入的总数。基于列表大小,将不会放置任何输入框。

    componentDidMount() {

        this.handleLevelNum();
}
    handleLevelNum() {
        const levelNum = [];
        var getValue = 2;
        if(getValue > 0) {
            var i;
            for (i = 1; i <= getValue; i++) {
                console.log(getValue)
                levelNum.push({
                    'cogsId': i,
                    'riskFactor': ''
                });
                this.setState({
                    levelNum: levelNum,
                });
            }

            console.log("print level number",levelNum)
        }

        else{
            this.setState({
                levelNum: [],
            });
        }
    }



             <Grid container spacing={24}>
                                  {
                                    this.state.levelNum.map((design, key) =>
                                        <Grid item xs={12} >
                                    <input style={{width:'100px'}}
                                           type="text" onChange={this.handleCogsId(design.cogsId}  placeholder=""/>
                                           <input name="col2"
                                           onChange={this.handleRiskFactor(design.cogsId)} value={this.state.riskFactor}
                                           type="text" placeholder=""/>
                                    <input name="col2"
                                             onChange={this.handlePrice(key)} value={this.state.price}
                                                   type="text" placeholder=""/>

                                </Grid>

                            )}
         <button   type="button" onClick={this.saveProjectFileItem} >Submit</button>

These are the onchange function i wrote:这些是我写的 onchange 函数:

handleRiskFactor = key => evt => {
        const newlevelNum = this.state.levelNum.map((design, sidx) => {
            if (key !== sidx) return design;
            return { ...design,cogsId:'', riskFactor: evt.target.value };
        });

        this.setState({ levelNum: newlevelNum });
    };

    handleCogsId = key => evt => {
        const newlevelNum = this.state.levelNum.map((design, sidx) => {
            if (key !== sidx) return design;
            return { ...design,cogsId:key, riskFactor: ''};
        });

        this.setState({ levelNum: newlevelNum });
    };

Now if the the index size is 2 no of input boxes will be placed accordingly.First input box will contain the value of the id from database and second input box will have an onchange event which value will be set with the first input id.I want to save the value like this in onchange event.The idea is to save an array like this based on onchange event of the input box:现在,如果索引大小为 2,则不会相应地放置任何输入框。第一个输入框将包含来自数据库的 id 值,第二个输入框将有一个 onchange 事件,该值将使用第一个输入 id 设置。想在onc​​hange事件中保存这样的值。想法是根据输入框的onchange事件保存这样的数组:

{
  "levelNum": [
    {
      "cogsId": "1",
      "riskFactor": "critical"

    },
    {
      "cogId": "2",
      "riskFactor": "None"

    }
  ]
}

Right now i am not being able to type anything in input box except the last one.But i am not being able to manipulate it dynamically.How can i manipulate it?现在,除了最后一个,我无法在输入框中输入任何内容。但我无法动态操作它。我该如何操作? any help regarding manipulating this dynamic input in onchange will be appreciated.任何有关在 onchange 中操纵此动态输入的帮助将不胜感激。

将 cogList 设置为数组 - levelNum:

this.setState({cogsList:response.data.data.levelNum });

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

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