gpt4 book ai didi

javascript - ReactJs 中父项下出现多个字段

转载 作者:行者123 更新时间:2023-11-30 19:15:56 25 4
gpt4 key购买 nike

class CriteriaSetValue extends Component {
state = {

count: 0,

tableName: "",
fieldName:"",

tables: [],
fields: [],
addLine: [],
addField: []
};

onSubmit = e => {
e.preventDefault();
const addField = this.state.addField;
const size = addField.length + 1;
addField.push(size);
this.setState({
addField
});

addNewLine = event => {
event.preventDefault();
const addLine = this.state.addLine;
const size = addLine.length + 1;
const nextLine = this.state.count + 1;
addLine.push(size);
this.setState({
count: nextLine,
addLine
});
};

render() {
const {showForm, tableName, fieldName } = this.state;

const { props, state } = this;

return (
<React.Fragment>


<div className="form-wrapper">
<div className="row">
<div className="col-10 container">
<form onSubmit={this.submit} className="card">
<div className="card-header">
<h3 className="card-title">
Criteria Set
{/* <Locale value="std.formupload" /> */}
</h3>
</div>
<div className="card-body">
<div className="row">
<div className="col-md-7 col-lg-8">

<div className="add-line">
<Button
icon
labelPosition="left"
onClick={this.addNewLine}
>
Add Line
<Icon name="plus" />
</Button>
{this.state.addLine.map(index => {
return (
<div
className="field-line"
style={{ marginTop: "30px" }}
key={index}
id={index}
>
<h4 className="field-button">Line {index}</h4>
<Button
className="field-button"
icon
onClick={this.toggleForm}
>
<Icon name="box" />
</Button>
</div>
);

})
}

{
this.state.addField.map(index => {
return (
<React.Fragment>
<div
className="field-button"
style={{
marginTop: "20px",
paddingLeft: "20px"
}}
key={index}
id={index}
>

<h4
className="field-button"
onclick={this.addCriteriaValue}
>
<span>
table.field

</span>
</h4>

<Button
className="field-button"
icon
onClick={this.toggleDelete}
>
<Icon name="delete calendar" />
</Button>
</div>
<br></br>
</React.Fragment>
);
})
}
</div>

</div>
</div>
</div>
</form>
</div>
</div>
</div>
</React.Fragment>
);
}
};

这与我要实现的目标有点相关: https://codesandbox.io/s/3vqyo8xlx5

但我希望 child 出现在点击“添加 child ”按钮的首选项下,而不是最后一个。

我正在努力使多个字段在单击第 1 行、第 2 行等旁边的按钮时出现在每一行下方,但目前,单击按钮时该字段跳转到最后一行,它没有出现在适当的位置线。 enter image description here

我已经能够在单击“添加行”按钮时显示行,并且在单击“第 1 行”旁边的按钮时也能够显示该字段。

enter image description here

我希望“第 1 行”的字段在单击字段按钮时显示在第 1 行下方,“第 2 行”的字段在单击旁边的字段按钮时显示在第 2 行下方,依此类推

enter image description here

最佳答案

你可以尝试这样的事情:

const AddButton = ({ label, onClick }) => (
<button onClick={onClick}>
{label} [+]
</button>
)

const FieldData = ({ label, criterias, onAdd, onDelete }) => (
<React.Fragment>
<div
className='field-button'
style={{
marginTop: '20px',
paddingLeft: '20px'
}}
>
<h4 className='field-button'>
<AddButton
label='Add criteria'
onClick={onAdd}
/>
<span>
{label}
</span>
</h4>
{criterias.map((item, idx) => (
<p key={idx}>{item.id}</p>
))}
<button
className='field-button'
onClick={onDelete}
>
Del [-]
</button>
</div>
<br />
</React.Fragment>
)

class App extends React.PureComponent {
state = {
lines: []
}

handleSubmit = e => {
e.preventDefault()
console.log('DATA TO SAVE ' + JSON.stringify(this.state.lines))
}

handleAddLine = event => {
event.preventDefault()
this.setState(prevState => ({
...prevState,
lines: [
...prevState.lines,
{
id: (prevState.lines.length + 1),
fields: []
}
]
}))
}

handleAddField = lineId => e => {
e.preventDefault()
this.setState(prevState => {
const newLines = [ ...prevState.lines ]
const curLine = newLines[newLines.findIndex(({ id }) => id === lineId)]

curLine.fields.push({
id: curLine.fields.length + 1,
criterias: []
})

return {
...prevState,
lines: newLines
}
})
}

handleAddCriteria = (lineId, fieldId) => event => {
event.preventDefault()

this.setState(prevState => {
const newLines = [ ...prevState.lines ]
const curLine = newLines[newLines.findIndex(({ id }) => id === lineId)]
const curField = curLine.fields[curLine.fields.findIndex(({ id }) => id === fieldId)]

curField.criterias.push({
id: curField.criterias.length + 1
})

return {
...prevState,
lines: newLines
}
})
}

handleDeleteField = (lineId, fieldId) => event => {
event.preventDefault()
this.setState(prevState => {
const newLines = [ ...prevState.lines ]
const curLine = newLines[newLines.findIndex(({ id }) => id === lineId)]
curLine.fields = curLine.fields.filter(item => item.id !== fieldId)

return {
...prevState,
lines: newLines
}
})
}

render() {
const { lines } = this.state

return (
<React.Fragment>
<div className='form-wrapper'>
<div className='row'>
<div className='col-10 container'>
<form
onSubmit={this.handleSubmit}
className='card'
>
<div className='card-header'>
<h3 className='card-title'>
Criteria Set
</h3>
</div>
<div className='card-body'>
<div className='row'>
<div className='col-md-7 col-lg-8'>
<div className='add-line'>
<AddButton
label='Add Line'
onClick={this.handleAddLine}
/>
{lines.map((line, idx) => {
return (
<React.Fragment key={idx}>
<div
className='field-line'
style={{ marginTop: '30px' }}
>
<h4 className='field-button'>
Line {idx+1}
</h4>
<AddButton
label='Add field'
onClick={this.handleAddField(line.id)}
/>
</div>
{line.fields.map((lField, idx) => (
<FieldData
label={idx+1}
criterias={lField.criterias}
onAdd={this.handleAddCriteria(line.id, lField.id)}
onDelete={this.handleDeleteField(line.id, lField.id)}
/>
))}
</React.Fragment>
)
})}
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</React.Fragment>
)
}
}

ReactDOM.render(
<App />,
document.getElementById('app')
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

<div id="app"></div>

我为状态定义了一个新的数据结构,所以考虑嵌套数据更好,我稍微重构了代码。

关于javascript - ReactJs 中父项下出现多个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58010340/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com