这个大 map 遍历怎么写呢
有如下代码结构,这是antDesign <Table>一行三列的表格,单元格内是 select下拉框.
conditionListSplit是回显渲染的数据数组list,也就是说,要在3个if判断之外,用map遍历.其值在 initialValue: 处使用.
但是这么复杂的结构,用map套上去后,语法不识别了,有什么办法吗 ?
另 dataIndex 也需要传进去使用
conditionListSplit(item=>{
3个if语句
})
在线编辑(需将左侧antD依赖改为3.26.10):
https://codesandbox.io/s/ferv...
下面展示了一个if语句内容.
if (dataIndex == "MainTablefield") {
return (
<Form.Item style={{ margin: 0 }}>
{form.getFieldDecorator(dataIndex, {
initialValue: {key:item.mainTableFieledName,label:item.mainTableFieledName} //在此处使用
})(
<Select
className="table-user-name-input"
showSearch
placeholder="请选择主表字段"
optionFilterProp="children"
onChange={this.onChange}
onBlur={this.save}
labelInValue
filterOption={(input, option) =>
option.props.children
.toLowerCase()
.indexOf(input.toLowerCase()) >= 0
}
>
{tableFormData.map((item, index) => {
if (item.id.slice(-8) == connectFormData.nodeSourceId) {
return (
<Option key={index} value={index}>
{item.name}
</Option>
);
}
})}
</Select>
)}
</Form.Item>
);
} else if(){}
else if(){}
======================================
备用代码:
import React from 'react';
import ReactDOM from 'react-dom';
import 'antd/dist/antd.css';
import './index.css';
import { Table, Input, Button, Popconfirm, Form,Select } from 'antd';
const EditableContext = React.createContext();
const EditableRow = ({ form, index, ...props }) => (
<EditableContext.Provider value={form}>
<tr {...props} />
</EditableContext.Provider>
);
const EditableFormRow = Form.create()(EditableRow);
class EditableCell extends React.Component {
state = {
editing: false,
selectList: [],
entityNameOptions_kLevelExpand_1: [ //select 临时数据读取
{
key: "1",
value: 1,
text: "info1"
},
{
key: "2",
value: 2,
text: "info2"
},
{
key: "3",
value: 3,
text: "info3"
}
],
entityNameOptions_kLevelExpand_2: [
{
key: "4",
value: 4,
text: "info4"
},
{
key: "5",
value: 5,
text: "info5"
},
{
key: "6",
value: 6,
text: "info6"
}
]
};
save = e => {
const { record, handleSave } = this.props;
this.form.validateFields((error, values) => {
if (error && error[e.currentTarget.id]) {
return;
}
this.toggleEdit();
handleSave({ ...record, ...values });
});
};
renderCell = form => {
this.form = form;
const { children, dataIndex, record, title } = this.props;
const { editing } = this.state;
if (dataIndex == "field") {
return (
<Form.Item style={{ margin: 0 }}>
{form.getFieldDecorator(dataIndex, {
initialValue: "请选择主表字段" //record[dataIndex]
})(
<Select
className="table-user-name-input"
showSearch
style={{width:100}}
placeholder="请选择主表字段"
optionFilterProp="children"
onChange={this.onChange}
onBlur={this.save}
labelInValue
filterOption={(input, option) =>
option.props.children
.toLowerCase()
.indexOf(input.toLowerCase()) >= 0
}
>
{this.state.entityNameOptions_kLevelExpand_1.map(item => (
<Option key={item.key} title={item.value}>
{item.text}
</Option>
))}
{/* {connectFormData.mainTableTitle.map(item => (
<Option key={i++} title={item}>
{item}
</Option>
))} */}
</Select>
)}
</Form.Item>
);
} else if (dataIndex == "operator") {
return (
<Form.Item style={{ margin: 0 }}>
{form.getFieldDecorator(dataIndex, {
initialValue: "请选择运算符" //record[dataIndex]
})(
<Select
className="table-user-name-input"
showSearch
style={{width:100}}
placeholder="请选择运算符"
optionFilterProp="children"
onChange={this.onChange}
onBlur={this.save}
labelInValue
filterOption={(input, option) =>
option.props.children
.toLowerCase()
.indexOf(input.toLowerCase()) >= 0
}
>
<Option value="21">=</Option>
<Option value="22">></Option>
<Option value="23">=</Option>
</Select>
)}
</Form.Item>
);
} else if (dataIndex == "tablefield") {
return (
<Form.Item style={{ margin: 0 }}>
{form.getFieldDecorator(dataIndex, {
initialValue: "请选择连接表字段" //record[dataIndex]
})(
<Select
className="table-user-name-input"
showSearch
style={{width:100}}
placeholder="请选择连接表字段"
optionFilterProp="children"
onChange={this.onChange}
onBlur={this.save}
labelInValue
filterOption={(input, option) =>
option.props.children
.toLowerCase()
.indexOf(input.toLowerCase()) >= 0
}
>
{this.state.entityNameOptions_kLevelExpand_2.map(item => (
<Option key={item.key} title={item.value}>
{item.text}
</Option>
))}
</Select>
)}
</Form.Item>
);
}
};
render() {
const {
editable,
dataIndex,
title,
record,
index,
handleSave,
children,
...restProps
} = this.props;
return (
<td {...restProps}>
{editable ? (
<EditableContext.Consumer>{this.renderCell}</EditableContext.Consumer>
) : (
children
)}
</td>
);
}
}
class EditableTable extends React.Component {
constructor(props) {
super(props);
this.columns = [
{
title: "主表字段",
dataIndex: "field",
editable: true
},
{
title: "运算符",
dataIndex: "operator",
editable: true
},
{
title: "连接表字段",
dataIndex: "tablefield",
editable: true
},
{
title: 'operation',
dataIndex: 'operation',
render: (text, record) =>
this.state.dataSource.length >= 1 ? (
<Popconfirm title="Sure to delete?" onConfirm={() => this.handleDelete(record.key)}>
<a>Delete</a>
</Popconfirm>
) : null,
},
];
this.state = {
dataSource: [
{
key: '0',
name: 'Edward King 0',
age: '32',
address: 'London, Park Lane no. 0',
},
{
key: '1',
name: 'Edward King 1',
age: '32',
address: 'London, Park Lane no. 1',
},
],
dataSource2: [ //此数据控制<Table>内容的行数,值无法传入select内
{
key: '0',
name: '1',
value: '字段名称1'
},
{
key: '1',
name: '2',
value: '字段名称2'
},
],
count: 2,
};
}
handleDelete = key => {
const dataSource = [...this.state.dataSource];
this.setState({ dataSource: dataSource.filter(item => item.key !== key) });
};
handleAdd = () => {
const { count, dataSource } = this.state;
const newData = {
key: count,
name: `Edward King ${count}`,
age: 32,
address: `London, Park Lane no. ${count}`,
};
this.setState({
dataSource: [...dataSource, newData],
count: count + 1,
});
};
handleSave = row => {
const newData = [...this.state.dataSource];
const index = newData.findIndex(item => row.key === item.key);
const item = newData[index];
newData.splice(index, 1, {
...item,
...row,
});
this.setState({ dataSource: newData });
};
render() {
const { dataSource,dataSource2 } = this.state;
const components = {
body: {
row: EditableFormRow,
cell: EditableCell,
},
};
const columns = this.columns.map(col => {
if (!col.editable) {
return col;
}
return {
...col,
onCell: record => ({
record,
editable: col.editable,
dataIndex: col.dataIndex,
title: col.title,
handleSave: this.handleSave,
}),
};
});
return (
<div>
<Button onClick={this.handleAdd} type="primary" style={{ marginBottom: 16 }}>
Add a row
</Button>
<Table
components={components}
rowClassName={() => 'editable-row'}
bordered
dataSource={dataSource2}
columns={columns}
/>
</div>
);
}
}
ReactDOM.render(<EditableTable />, document.getElementById('container'));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
修改了下 其实原理就是把公共的提取出来,你看下还有什么问题
https://codesandbox.io/s/nift...