在Reactjs中添加和删除值未重置的输入字段
Adding and removing input fields in Reactjs with values not reseting
嗨,我正在开发一个React应用程序,其中有四个选项。当用户选择一个选项时,相应的输入元素将添加到包装器中。在下面的代码中,添加操作可以正常工作,但删除操作不能正常工作,它没有删除相应的元素。另一个问题是,当组件重新渲染时,输入字段上的值不存在。因此,专家指导我如何在单击删除按钮时删除相应的行。和组件重新渲染时,不应重置输入值。
import React from 'react';
import ReactDOM from 'react-dom';
var fields = "";
var options = ['one','two','three','four','five','six','seven','eight','nine','Ten','eleven','twelve'];
var SelectOptions = React.createClass({
render:function(){
var options = this.props.options;
return (
<select>
{options.map(function(col,index){
return (
<option key={index} value ={col}> {col} </option>
);
})}
</select>
);
}
});
var PriceMultiply = React.createClass({
render:function(){
return (
<tr>
<td>
Adjust Price Multiply
</td>
<td>
Multiply <SelectOptions options={options} />
</td>
<td>
by <input type="text" name="" />
</td>
<td>
<button className="btn btn-danger" onClick={this.props.removeOperation} > Remove </button>
</td>
</tr>
);
}
});
var PriceAdd = React.createClass({
render:function(){
return (
<tr>
<td>
Adjust Price (Add)
</td>
<td>
Add <input type="text" name="" />
</td>
<td>
to <SelectOptions options={options} />
</td>
<td>
<button className="btn btn-danger" onClick={this.props.removeOperation} > Remove </button>
</td>
</tr>
);
}
});
var ExcludeProducts = React.createClass({
render:function(){
return (
<tr>
<td>
Filter Products (Includes)
</td>
<td>
Exclude Products Where <SelectOptions options={options} />
</td>
<td>
Includes <input type="text" name="" />
</td>
<td>
<button className="btn btn-danger" onClick={this.props.removeOperation} > Remove </button>
</td>
</tr>
);
}
});
var IncludeProducts = React.createClass({
render:function(){
return (
<tr>
<td>
Filter Products (Includes)
</td>
<td>
Exclude Products Where <SelectOptions options={options} />
</td>
<td>
does not equals <input type="text" name="" />
</td>
<td>
<button className="btn btn-danger" onClick={this.props.removeOperation} > Remove </button>
</td>
</tr>
);
}
});
var DynamicFields = React.createClass({
getInitialState:function(){
return {
operations:[]
}
},
removeOperation:function(index){
var operations = this.state.operations;
operations.splice(index,1);
this.setState({operations:operations});
},
addOperation:function(){
var value = this.refs.operationsDropDown.value;
this.setState({operations:this.state.operations.concat([value])});
},
renderAdjustmentRows:function(){
fields = this.state.operations.map((operation,index) =>{
if(operation == "adjust-price-multiply"){
return (<PriceMultiply key={index} removeOperation={this.removeOperation} />);
}else if(operation == "adjust-price-add"){
return (<PriceAdd key={index} removeOperation={this.removeOperation} />);
}else if(operation == "filter-products-includes"){
return (<IncludeProducts key={index} removeOperation={this.removeOperation} />);
}else if(operation == "filter-products-excludes"){
return (<ExcludeProducts key={index} removeOperation={this.removeOperation} />);
}
});
return (
<table>
<tbody>
{fields}
</tbody>
</table>
);
},
render:function(){
return (
<div>
<select className="browser-default" ref="operationsDropDown" id="operations-drop-down">
<option value="adjust-price-multiply"> Adjust Price (Multiply) </option>
<option value="adjust-price-add"> Adjust Price (Add) </option>
<option value="filter-products-includes"> Filter products (Includes) </option>
<option value="filter-products-excludes"> Filter products excludes </option>
</select>
<button onClick={this.addOperation} > Add Operation </button>
<div id="adjust-import-data-rows">
{this.renderAdjustmentRows()}
</div>
</div>
);
}
});
ReactDOM.render(<DynamicFields />,document.getElementById('container'));
问题是您没有为removeOperation
函数提供索引。在发送索引之前,您的组件应该具有索引。因此,使用如下索引道具进行传递:
renderAdjustmentRows:function(){
fields = this.state.operations.map((operation,index) =>{
if(operation == "adjust-price-multiply"){
return (<PriceMultiply key={index} index={index} removeOperation={this.removeOperation} />);
}else if(operation == "adjust-price-add"){
return (<PriceAdd key={index} index={index} removeOperation={this.removeOperation} />);
}else if(operation == "filter-products-includes"){
return (<IncludeProducts key={index} index={index} removeOperation={this.removeOperation} />);
}else if(operation == "filter-products-excludes"){
return (<ExcludeProducts key={index} index={index} removeOperation={this.removeOperation} />);
}
});
并从每个动态组件的移除按钮点击发送索引道具。为此,用替换所有删除按钮
<button className="btn btn-danger" onClick={this.props.removeOperation.bind(null, this.props.index)} > Remove </button>
你会得到你想要的。
完整代码可在https://jsfiddle.net/KishoreBarik/f8h3c82m/
可能是因为在这段代码中:
removeOperation:function(index){
var operations = this.state.operations;
operations.splice(index,1);
this.setState({operations:operations});
},
您正在直接改变状态,如下所示:
var operations = this.state.operations; // operations now points to array in state
operations.splice(index,1); // splice mutates operations directly
// effect of these 2 statements is the same as
this.state.operations.splice(index,1);
您需要首先制作状态数组的COPY
如果您将第一行更改为:
var operations = this.state.operations.slice();
你应该没事的。。
对于删除问题:removeOperation()
没有被赋予索引,而是一个对象。当你调用它时,给它一个正确的索引
相关文章:
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 而循环只设置php中输入字段中的第一个值
- 在输入字段中将最小金额设置为
- 如何在输入字段中的按钮的帮助下打开日历,该字段的类型为“=”;日期”;
- 如何在HTML输入字段中添加不可删除的后缀
- 互斥单选按钮和相应的输入字段
- 在IE9中的输入字段中输入焦点最近按钮
- 选中单选框时将属性添加到输入字段
- 当设置addFromAutocompleteOnly时,剩余文本将保留在输入字段中
- 我如何在数字插入中使用逗号,这样它就不会'不要破坏我的输入字段
- 在输入字段上有两个函数调用,一个在Blur上,一个不在Angular中
- 输入字段将't获取更新的值
- 如何选择多个输入字段并删除所需的属性
- 输入字段,只接受0到12之间的数字
- 单击鼠标,用MySQL数据填充html表单输入字段
- 使用jquery将输入字段转换为文本
- Model中的Typeahead返回空值以形成输入字段
- 如何从查询字符串中的输入字段发回文本
- 如何验证日期、月份和日期的3个独立输入字段;年使用jquery或javascript
- 将值传递给jquery创建的输入字段