Commit 4ebbd5bb authored by 皮倩雯's avatar 皮倩雯

解决表字段附加表全选回显错误问题,新增表搜索功能

parent 5c5f61a0
Pipeline #42337 passed with stages
in 6 minutes 56 seconds
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect } from 'react';
import { Form, Modal, Input, Select, Checkbox, notification, Card } from 'antd'; import { Form, Modal, Input, Select, Checkbox, notification, Card } from 'antd';
import { import { loadTableFields, addTables, addFields } from '@/services/platform/bs';
loadTableFields, addTables, addFields import styles from './index.less';
} from '@/services/platform/bs'
import styles from './index.less'
const CheckboxGroup = Checkbox.Group; const CheckboxGroup = Checkbox.Group;
import qs from 'qs'; import qs from 'qs';
const { Option } = Select; const { Option } = Select;
const AddModal = props => { const AddModal = props => {
const { callBackSubmit = () => { }, type, visible, tableList, formObj } = props; const { callBackSubmit = () => {}, type, visible, tableList, formObj } = props;
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [form] = Form.useForm(); const [form] = Form.useForm();
const { Item } = Form; const { Item } = Form;
const [plainOptions, setPlainOptions] = useState([]);//复选框所有内容 const [plainOptions, setPlainOptions] = useState([]); //复选框所有内容
const [checkedList, setCheckedList] = useState([]);//选中的复选框内容 const [checkedList, setCheckedList] = useState([]); //选中的复选框内容
const [indeterminate, setIndeterminate] = useState(true); const [indeterminate, setIndeterminate] = useState(true);
const [checkAll, setCheckAll] = useState(false); const [checkAll, setCheckAll] = useState(false);
const onChange = list => { const onChange = list => {
setCheckedList(list); setCheckedList(list);
setIndeterminate(!!list.length && list.length < plainOptions.length); setIndeterminate(!!list.length && list.length < plainOptions.length);
...@@ -34,22 +31,27 @@ const AddModal = props => { ...@@ -34,22 +31,27 @@ const AddModal = props => {
const onSubmit = () => { const onSubmit = () => {
let fieldNames = []; let fieldNames = [];
(checkedList || []).map(item => { (checkedList || []).map(item => {
fieldNames.push({ fieldNames: item }) fieldNames.push({ fieldNames: item });
}) });
form.validateFields().then(validate => { form.validateFields().then(validate => {
if (validate) { if (validate) {
let obj = form.getFieldsValue(); let obj = form.getFieldsValue();
setLoading(true); setLoading(true);
let data = {}, url = JSON.stringify(formObj) == '{}' ? addTables : addFields let data = {},
url = JSON.stringify(formObj) == '{}' ? addTables : addFields;
if (JSON.stringify(formObj) == '{}') { if (JSON.stringify(formObj) == '{}') {
data = { tableName: obj.tableName, alias: obj.tableAlias || '', fieldNames: checkedList.join(',') } data = {
} tableName: obj.tableName,
else { alias: obj.tableAlias || '',
data = { tableName: formObj.tableName, fieldNames: checkedList.join(',') } fieldNames: checkedList.join(','),
};
} else {
data = { tableName: formObj.tableName, fieldNames: checkedList.join(',') };
} }
url(data).then(res => { url(data)
.then(res => {
setLoading(false); setLoading(false);
if (res.msg === "Ok" || res.msg === "") { if (res.msg === 'Ok' || res.msg === '') {
form.resetFields(); form.resetFields();
callBackSubmit(); callBackSubmit();
notification.success({ notification.success({
...@@ -57,7 +59,6 @@ const AddModal = props => { ...@@ -57,7 +59,6 @@ const AddModal = props => {
duration: 3, duration: 3,
description: JSON.stringify(formObj) == '{}' ? '新增成功' : '表字段新增成功', description: JSON.stringify(formObj) == '{}' ? '新增成功' : '表字段新增成功',
}); });
} else { } else {
notification.error({ notification.error({
message: '提示', message: '提示',
...@@ -74,41 +75,43 @@ const AddModal = props => { ...@@ -74,41 +75,43 @@ const AddModal = props => {
}); });
setLoading(false); setLoading(false);
}); });
} }
}); });
}; };
useEffect(() => { useEffect(() => {
if (type != '' && JSON.stringify(formObj) == '{}') { if (type != '' && JSON.stringify(formObj) == '{}') {
renderField(tableList[0].text) renderField(tableList[0].text);
form.setFieldsValue({ form.setFieldsValue({
tableName: tableList[0].text tableName: tableList[0].text,
}) });
} else if (type != '' && JSON.stringify(formObj) != '{}') { } else if (type != '' && JSON.stringify(formObj) != '{}') {
renderField(formObj.tableName) renderField(formObj.tableName);
} }
}, [visible]); }, [visible]);
const renderField = (name) => { const renderField = name => {
loadTableFields({ tableName: name }).then(response => { loadTableFields({ tableName: name }).then(response => {
if (response.data.root.length) { if (response.data.root.length) {
let arr = [], newArr = [] // eslint-disable-next-line one-var
let arr = [],
newArr = [];
response.data.root.map(item => { response.data.root.map(item => {
newArr.push(item.fieldName) newArr.push(item.fieldName);
if (item.isCheck) { if (item.isCheck) {
arr.push(item.fieldName) arr.push(item.fieldName);
} }
}) });
setCheckAll(false) setCheckAll(arr.length === newArr.length);
setPlainOptions(newArr) setIndeterminate(!!arr.length && arr.length < newArr.length);
setCheckedList(arr) setPlainOptions(newArr);
} setCheckedList(arr);
}) } else {
setCheckAll(false);
setIndeterminate(false);
} }
});
};
const layout = { const layout = {
layout: 'horizontal', layout: 'horizontal',
labelCol: { labelCol: {
...@@ -119,9 +122,9 @@ const AddModal = props => { ...@@ -119,9 +122,9 @@ const AddModal = props => {
}, },
}; };
const handleChange = (value) => { const handleChange = value => {
renderField(value) renderField(value);
} };
return ( return (
<Modal <Modal
...@@ -140,31 +143,53 @@ const AddModal = props => { ...@@ -140,31 +143,53 @@ const AddModal = props => {
getContainer={false} getContainer={false}
> >
{visible && ( {visible && (
<Form form={form} {...layout} > <Form form={form} {...layout} style={{ marginBottom: '30px' }}>
{JSON.stringify(formObj) == '{}' ? <><Item {JSON.stringify(formObj) == '{}' ? (
<>
<Item
label="表名" label="表名"
name="tableName" name="tableName"
rules={[{ required: true, message: '请选择表名' }]} rules={[{ required: true, message: '请选择表名' }]}
> >
<Select onChange={handleChange}> <Select onChange={handleChange} showSearch>
{tableList.length ? tableList.map((item, index) => { return <Select.Option key={index} value={item.text}>{item.text}</Select.Option>; }) : ''} {tableList.length
? tableList.map((item, index) => {
return (
<Select.Option key={index} value={item.text}>
{item.text}
</Select.Option>
);
})
: ''}
</Select> </Select>
</Item> </Item>
<Item <Item label="别名" name="tableAlias">
label="别名"
name="tableAlias"
>
<Input allowClear /> <Input allowClear />
</Item></> : <div className={styles.paneTitle}>{formObj.tableName}</div>} </Item>
</>
) : (
<div className={styles.paneTitle}>{formObj.tableName}</div>
)}
<div className={styles.field}> <div className={styles.field}>
<Card title="字段名" extra={<Checkbox indeterminate={indeterminate} onChange={onCheckAllChange} checked={checkAll}> </Checkbox>} > <Card
{ title="字段名"
plainOptions.length ? <CheckboxGroup options={plainOptions} value={checkedList} onChange={onChange} /> : '' extra={
<Checkbox
indeterminate={indeterminate}
onChange={onCheckAllChange}
checked={checkAll}
>
{' '}
</Checkbox>
} }
>
{plainOptions.length ? (
<CheckboxGroup options={plainOptions} value={checkedList} onChange={onChange} />
) : (
''
)}
</Card> </Card>
</div> </div>
</Form> </Form>
)} )}
</Modal> </Modal>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment