我想重新呈现切片的数据并显示在表单字段中,我正在使用array.slice来切片数据。
constructor() {
super();
this.state = {
currentPage: 1,
todosPerPage: 3,
};
this.handleClick = this.handleClick.bind(this);
}
handleClick = (event) => {
this.setState({
currentPage: Number(event.target.id)
}, () => {
this.forceUpdate();
});
}
const pageNumbers = [];
for (let i = 1; i <= Math.ceil(this.props.facilityDataInputs.length / this.state.todosPerPage); i++) {
pageNumbers.push(i);
}
const renderPageNumbers = pageNumbers.map(number => {
return (
<li key={number}>
<div style={{ display: 'inline-flex', cursor : 'pointer' }}>
<input type="button"
name="facilityId"
value={number}
id={number}
onClick={this.handleClick}
/>
</div>
</li>
);
});
facilityList = ({ fields, meta: { error, submitFailed } }) => {
const { currentPage, todosPerPage } = this.state;
// Logic for displaying current todos
const indexOfLastTodo = currentPage * todosPerPage;
const indexOfFirstTodo = indexOfLastTodo - todosPerPage;
let fetchFields = fields.getAll();
let displayFetchedFields = fetchFields.slice(indexOfFirstTodo, indexOfLastTodo);
if (displayFetchedFields) {
return displayFetchedFields.map((facility, index) => {
return (
<li key={index} style={{ margin: '10px 0', color: '#071F5D' }}>
<div style={{ display: 'inline-flex' }}>
ID:
<Field
name={`facilityData[${index}].facilityId`}
component='input'
value={this.props.facilityDataInputs[index].facilityId}
/>
</div>
<div style={{ display: 'inline-flex' }}>
Ref:
<Field
name={`facilityData[${index}].facilityRef`}
component='input'
value={this.props.facilityDataInputs[index].facilityRef}
/>
</div>
</li>
)
})
}
}
<fieldarray name ="facilitiData" component ={this facilityList}>
我可以获取切片数据,但无法重新呈现表单,请帮助我
答案 0 :(得分:1)
尝试以下代码。
facilityList = ({ fields, meta: { error, submitFailed } }) => {
const { currentPage, todosPerPage } = this.state;
// Logic for displaying current todos
const indexOfLastTodo = currentPage * todosPerPage;
const indexOfFirstTodo = indexOfLastTodo - todosPerPage;
let fetchFields = fields.getAll();
let displayFetchedFields = fetchFields.slice(indexOfFirstTodo, indexOfLastTodo);
if (displayFetchedFields) {
return displayFetchedFields.map((facility, index) => {
return (
<li key={indexOfFirstTodo+index} style={{ margin: '10px 0', color: '#071F5D' }}>
<div style={{ display: 'inline-flex' }}>
ID:
<Field
name={`facilityData[${indexOfFirstTodo+index}].facilityId`}
component='input'
value={this.props.facilityDataInputs[indexOfFirstTodo+index].facilityId}
/>
</div>
<div style={{ display: 'inline-flex' }}>
Ref:
<Field
name={`facilityData[${indexOfFirstTodo+index}].facilityRef`}
component='input'
value={this.props.facilityDataInputs[indexOfFirstTodo+index].facilityRef}
/>
</div>
</li>
)
})
}
}
答案 1 :(得分:0)
FieldArray在切片数组时无法识别redux表单中的任何更改。 为此,您需要在fieldarray中传递任何更新道具
<fieldarray name ="facilitiData" update={this.state.currentPage} component ={this facilityList}>
在fieldsArray中传递道具后,redux表单会识别状态值的更改,并自动重新呈现组件