0

I have done the dynamic inputs now my problem is getting the values of each inputs but its a multiple inputs? how can I achieve this. here is the jsfiddle.

 perRow() {
        return this.state.values.map((el,i) => 

                <tr key={i}>

                    <td className="col-md-4">
                        <div className="form-group">
                            <input type="text" className="form-control" placeholder="Name" value={el||''} onChange={this.handleChange.bind(this, i)}/>
                        </div>
                        <div className="form-group">
                            <input type="text" className="form-control" placeholder="Hourly Rate" />
                        </div>                        
                        <p><strong>Total hours:</strong> 0:00</p>
                        <p><strong>Total pay:</strong> $0.00</p> 
                            <button className="btn btn-danger do-not-print" onClick={this.removeClick.bind(this, i)}>Remove employee</button>
                    </td>
                    <td>Monday</td>
                    <td><input type="text" className="form-control" /></td>
                    <td><input type="text" className="form-control" /></td>
                    <td><input type="text" className="form-control" /></td>
                    <td>0:00</td>
                    <td>$0.00</td>
                </tr>

        )
    }

https://jsfiddle.net/zd208m1a/2/?utm_source=website&utm_medium=embed&utm_campaign=zd208m1a

Tep Tep
  • 59
  • 3
  • 16

3 Answers3

3

First of all, create constructor and we will create an onChange function so we need to bind it inside the constructor

constructor(props){
    super(props);
    this.state = {
        username: '',
        password: '',
    };
    this.onChange = this.onChange.bind(this);
}

Then we need to create onChange function it will handle by the name

onChange(e){
        this.setState({ [e.target.name] : e.target.value });
    }

Finally, call the onChange function with a name attribute, you need to define the input name in the state to avoid any troubles.

<td><input type="text" className="form-control" name="username" onChange={this.onChange} /></td>

<td><input type="text" className="form-control" name="password" onChange={this.onChange} /></td>

2_ Alternative way instead of binding the onChange function in the constructor you can use an anonymous arrow function like so

onChange = (e) => {
        this.setState({ [e.target.name] : e.target.value });
    }

3_ Another Alternative way, you can bind it like so

onChange(e){
   this.setState({ [e.target.name] : e.target.value });
}       

<input 
    type="text" 
    name="username" 
    onChange={this.onChange.bind(this)} 
/>

4_ Another Alternative way with passing data

onChange(e,data){
console.log(data); // result is "something"
   this.setState({ [e.target.name] : e.target.value });
}       

<input 
    type="text" 
    name="username" 
    onChange={(e)=>this.onChange(e, 'something')} 
/>
Liam
  • 6,517
  • 7
  • 25
  • 47
  • Please look at my jsfiddle. my problem is on the state i will have multiple username and password – Tep Tep Mar 21 '18 at 06:13
  • Do you mean you have two problems? the above answer will let you able to create multi inputs depending on the input attribute name – Liam Mar 21 '18 at 06:25
  • yeah I but in my problem is that it is dynamic. Example: in one row I have input for username and password then when I click the button add row I will have another input of username and password. In my current code I have solved the adding of rows of inputs now my problem is getting the values in each object array. – Tep Tep Mar 21 '18 at 06:29
  • Okay, I understand you now, you just want to keep the previous data and add new data. Do am right? like [ 'PrevData', 'NewDate', 'NewNew' ] – Liam Mar 21 '18 at 06:47
0

Yes you can get your input values like this.state.values.join(', ')

since you got your input values in state values as as array

 handleChange(i, event) {
            let values = [...this.state.values];
            values[i] = event.target.value;
            this.setState({ values });
            console.log(this.state.values)
        }

Checck the below example , this will help you for sure Demo done by mayankshukla

Jayavel
  • 3,377
  • 2
  • 21
  • 35
0

Here are the implement using react hook, hope this helps, too.

const [value, setValue] = useState([])

const handleChange = (i, event) => {
 let values = [...value];
 values[i] = e.target.value;

 setValue(values)
 console.log(values)
}

Step to get value inside multiple input text :

  1. Create state with empty element array.
  2. When onChange is runiing...
  3. Copy value state into variable mutable. Example in es6 let values = [...value]
  4. When e.target.value is getting done
  5. Based on index total, value is saved inside values variable
  6. Then, save final values inside value using setValue
Adoel
  • 1
  • 1