Commit a0d2fee9 authored by beisir's avatar beisir

fix: 批量设置

parent 8a5a0130
import { InputNumber, InputRef } from 'antd'; import { InputNumber, InputRef } from 'antd';
import { Button, Form, Input, Popconfirm, Table } from 'antd'; import { Button, Form, Input, Popconfirm, Table } from 'antd';
import type { FormInstance } from 'antd/es/form'; import type { FormInstance } from 'antd/es/form';
import React, { useContext, useEffect, useRef, useState } from 'react'; import React, { useContext, useEffect, useRef, useState, forwardRef, useImperativeHandle } from 'react';
const EditableContext = React.createContext<FormInstance<any> | null>(null); const EditableContext = React.createContext<FormInstance<any> | null>(null);
interface DataType {
rowSpanCount?: number;
supplyPrice: number;
commissionRate: number;
marketPrice: number;
salePrice: number;
stock: number;
productStockWarning: number;
}
interface PropType { interface PropType {
initData: any[]; initData: any[];
defaultColumns: any[];
setTableData: (item: DataType[]) => {};
}; };
interface Item { interface Item {
key: string; key: string;
...@@ -26,15 +37,7 @@ interface EditableCellProps { ...@@ -26,15 +37,7 @@ interface EditableCellProps {
interface EditableRowProps { interface EditableRowProps {
index: number; index: number;
} }
interface DataType {
rowSpanCount?: number;
supplyPrice: number;
commissionRate: number;
marketPrice: number;
salePrice: number;
stock: number;
productStockWarning: number;
}
type EditableTableProps = Parameters<typeof Table>[0]; type EditableTableProps = Parameters<typeof Table>[0];
type ColumnTypes = Exclude<EditableTableProps['columns'], undefined>; type ColumnTypes = Exclude<EditableTableProps['columns'], undefined>;
...@@ -74,16 +77,15 @@ const EditableCell: React.FC<EditableCellProps> = (props) => { ...@@ -74,16 +77,15 @@ const EditableCell: React.FC<EditableCellProps> = (props) => {
}; };
let childNode = children; let childNode = children;
// if (editable) {
if (editable) {
childNode = <Form.Item childNode = <Form.Item
style={{ margin: 0 }} style={{ margin: 0 }}
name={['tableList', rowIndex, dataIndex]} name={['tableList', rowIndex, dataIndex]}
// initialValue={record[dataIndex]} // initialValue={record[dataIndex]}
rules={[{ required: true, message: `${title} is required.` }]}> rules={[{ required: true, message: `${title} is required.` }]}>
<InputNumber onBlur={save} /> {editable ?<InputNumber onBlur={save} /> : children[1]}
</Form.Item> </Form.Item>
} // }
return <td {...restProps}>{childNode}</td>; return <td {...restProps}>{childNode}</td>;
}; };
...@@ -91,9 +93,9 @@ const EditableCell: React.FC<EditableCellProps> = (props) => { ...@@ -91,9 +93,9 @@ const EditableCell: React.FC<EditableCellProps> = (props) => {
const EditFormTable = (props: PropType) => { const EditFormTable = forwardRef((props: PropType, ref) => {
const { initData } = props; const { initData, defaultColumns, setTableData } = props;
// console.log(initData); // console.log(initData);
const [dataSource, setDataSource] = useState<DataType[]>([]); const [dataSource, setDataSource] = useState<DataType[]>([]);
const [form] = Form.useForm(); const [form] = Form.useForm();
...@@ -108,48 +110,6 @@ const EditFormTable = (props: PropType) => { ...@@ -108,48 +110,6 @@ const EditFormTable = (props: PropType) => {
setDataSource(initData); setDataSource(initData);
}, [initData]) }, [initData])
const defaultColumns: (ColumnTypes[number] & { editable?: boolean; dataIndex: string })[] = [
{
title: '一级规格',
dataIndex: 'firstSpecValue',
},
{
title: '二级规格',
dataIndex: 'secondSpecValue',
},
{
title: '供货价',
dataIndex: 'supplyPrice',
editable: true,
},
{
title: '佣金费率',
dataIndex: 'commissionRate',
editable: true,
},
{
title: '市场价',
dataIndex: 'marketPrice',
editable: true,
},
{
title: '销售价',
dataIndex: 'salePrice',
editable: true,
},
{
title: '库存',
dataIndex: 'stock',
editable: true,
},
{
title: '库存预警阈值',
dataIndex: 'productStockWarning',
editable: true,
},
];
const handleAdd = async () => { const handleAdd = async () => {
try { try {
const { tableList } = await form.validateFields(); const { tableList } = await form.validateFields();
...@@ -168,8 +128,25 @@ const EditFormTable = (props: PropType) => { ...@@ -168,8 +128,25 @@ const EditFormTable = (props: PropType) => {
// ...item, // ...item,
// ...row, // ...row,
// }); // });
setDataSource([...row]); setTableData([...row]);
}; };
const onCheck = async () => {
try {
const values = await form.validateFields();
return values;
} catch (errorInfo) {
return null;
}
};
useImperativeHandle(ref, () => ({
onCheck,
form,
}));
// 根据这里做判断渲染表格 // 根据这里做判断渲染表格
const columns = defaultColumns.map((col, colIndex) => { const columns = defaultColumns.map((col, colIndex) => {
// if (!col.editable) { // if (!col.editable) {
...@@ -201,9 +178,6 @@ const EditFormTable = (props: PropType) => { ...@@ -201,9 +178,6 @@ const EditFormTable = (props: PropType) => {
return ( return (
<div> <div>
<Button onClick={handleAdd} type="primary" style={{ marginBottom: 16 }}>
Add a row
</Button>
<Form form={form} scrollToFirstError component={false}> <Form form={form} scrollToFirstError component={false}>
<EditableContext.Provider value={form}> <EditableContext.Provider value={form}>
<Table <Table
...@@ -221,9 +195,10 @@ const EditFormTable = (props: PropType) => { ...@@ -221,9 +195,10 @@ const EditFormTable = (props: PropType) => {
columns={columns as ColumnTypes} columns={columns as ColumnTypes}
/> />
</EditableContext.Provider> </EditableContext.Provider>
<Button onClick={handleAdd} type="primary" style={{ marginBottom: 16 }}>Add a row</Button>
</Form> </Form>
</div> </div>
); );
}; });
export default EditFormTable; export default EditFormTable;
\ No newline at end of file
...@@ -64,3 +64,36 @@ export const WeeksList = [ ...@@ -64,3 +64,36 @@ export const WeeksList = [
value: 7, value: 7,
}, },
]; ];
export const StaticColumns = [{
title: '供货价',
dataIndex: 'supplyPrice',
editable: true,
},
{
title: '佣金费率',
dataIndex: 'commissionRate',
editable: true,
role: [],
},
{
title: '市场价',
dataIndex: 'marketPrice',
editable: true,
},
{
title: '销售价',
dataIndex: 'salePrice',
editable: true,
},
{
title: '库存',
dataIndex: 'stock',
editable: true,
},
{
title: '库存预警阈值',
dataIndex: 'productStockWarning',
editable: true,
role: [],
}];
import React, { useState, useRef, useEffect } from 'react'; import React, { useState, useRef, useEffect } from 'react';
import { PageHeaderWrapper } from '@ant-design/pro-layout'; import { PageHeaderWrapper } from '@ant-design/pro-layout';
import { Spin, Button } from 'antd'; import { Spin, Button } from 'antd';
import { ConsoleSqlOutlined } from '@ant-design/icons';
// import { getAppChannelAllList } from '@/services/common';
import { Title, WrapperContainer } from './components/CommonTemplate'; import { Title, WrapperContainer } from './components/CommonTemplate';
import { TaskTypeSelect } from './components/TaskTypeSelect'; import { TaskTypeSelect } from './components/TaskTypeSelect';
import { Task } from './type'; import { Task } from './type';
...@@ -28,30 +25,19 @@ import { ServiceContext } from './context'; ...@@ -28,30 +25,19 @@ import { ServiceContext } from './context';
const ServiceGoods = options => { const ServiceGoods = options => {
const pageId = +options.match.params.id; // pageId: 0-新增,1-修改,2-查看 const pageId = +options.match.params.id; // pageId: 0-新增,1-修改,2-查看
const ref = useRef();
const basicRef = useRef(null); const basicRef = useRef(null);
const stockRef = useRef(null); const stockRef = useRef(null);
const settingRef = useRef(null); const settingRef = useRef(null);
const settleOtrRef = useRef(null); const settleOtrRef = useRef(null);
const [isEdit, setIsEdit] = useState(false); const [isEdit, setIsEdit] = useState(false); // 是否是编辑状态
const [productType, setProductType] = useState(1); const [productType, setProductType] = useState(1); // 商品状态
const [pageLoading, setPageLoading] = useState(false); const [pageLoading, setPageLoading] = useState(false); // 页面加载状态
const [categoryList, setCategoryList] = useState([]); // 获取三级类目 const [categoryList, setCategoryList] = useState([]); // 获取三级类目
const [brandList, setBrandList] = useState([]); // 获取商品牌 const [brandList, setBrandList] = useState([]); // 获取商品牌
const [specList, setSpecList] = useState([]); // 规格列表 const [specList, setSpecList] = useState([]); // 规格列表
const [examLoading, setExamLoading] = useState(false); const [editData, setEditData] = useState({}); // 编辑保存数据
const [channelAllList, setChannelAllList] = useState({});
const [editData, setEditData] = useState({});
// const refreshTable = () => {
// ref.current.reload();
// };
// useEffect(() => {
// getChannelAllList();
// }, []);
// const onReset = () => {};
const productChange = (task: Task): void => { const productChange = (task: Task): void => {
setProductType(task.type); setProductType(task.type);
}; };
...@@ -66,6 +52,7 @@ const ServiceGoods = options => { ...@@ -66,6 +52,7 @@ const ServiceGoods = options => {
console.log(resuslt); console.log(resuslt);
}; };
// 编辑回显详情数据
const getProductDetailResponse = async () => { const getProductDetailResponse = async () => {
try { try {
const res = await getProductDetail(pageId); const res = await getProductDetail(pageId);
...@@ -112,7 +99,7 @@ const ServiceGoods = options => { ...@@ -112,7 +99,7 @@ const ServiceGoods = options => {
<Spin tip="正在加载..." spinning={pageLoading} delay={100}> <Spin tip="正在加载..." spinning={pageLoading} delay={100}>
<PageHeaderWrapper className={commonStyle.header}> <PageHeaderWrapper className={commonStyle.header}>
<WrapperContainer> <WrapperContainer>
<ServiceContext.Provider value={{ pageId, isEdit }}> <ServiceContext.Provider value={{ pageId, isEdit, productType }}>
<Title title="商品类型" /> <Title title="商品类型" />
<TaskTypeSelect productType={productType} onChange={productChange} /> <TaskTypeSelect productType={productType} onChange={productChange} />
......
import { Button, Form, Input, Popconfirm, Table } from 'antd';
export interface Task { export interface Task {
name: string; name: string;
type: number; type: number;
desc: string; desc: string;
} }
interface EditableRowProps {
index: number;
}
export type EditableTableProps = Parameters<typeof Table>[0];
export type ColumnTypes = Exclude<EditableTableProps['columns'], undefined>;
...@@ -16,8 +16,6 @@ const createInitProduct = (skuItem, isCreate) => { ...@@ -16,8 +16,6 @@ const createInitProduct = (skuItem, isCreate) => {
return skuItem; return skuItem;
} }
return { return {
// firstSpecValue: '',
// secondSpecValue: '',
weight: null, weight: null,
productStockWarning: null, productStockWarning: null,
marketPrice: null, marketPrice: null,
...@@ -29,8 +27,6 @@ const createInitProduct = (skuItem, isCreate) => { ...@@ -29,8 +27,6 @@ const createInitProduct = (skuItem, isCreate) => {
}; };
const initData = { const initData = {
// firstSpecValue: '',
// secondSpecValue: '',
weight: null, weight: null,
productStockWarning: null, productStockWarning: null,
marketPrice: null, marketPrice: null,
...@@ -42,8 +38,6 @@ const initData = { ...@@ -42,8 +38,6 @@ const initData = {
const createSecondProduct = (secondSpecList, initItem, secondSpec, dataSource, callback) => { const createSecondProduct = (secondSpecList, initItem, secondSpec, dataSource, callback) => {
secondSpecList.forEach(secondItem => { secondSpecList.forEach(secondItem => {
console.log('============>', Object.keys(secondItem));
const specSecond = const specSecond =
Object.keys(secondItem).length < 2 Object.keys(secondItem).length < 2
? { ? {
...@@ -83,7 +77,6 @@ export const createProductData = ({ firstValues, secondValues, firstSpecId, seco ...@@ -83,7 +77,6 @@ export const createProductData = ({ firstValues, secondValues, firstSpecId, seco
dataSource.push(specFirst); dataSource.push(specFirst);
}); });
} else if (secondValues.length) { } else if (secondValues.length) {
// const childData = createInitProduct();
createSecondProduct(secondValues, initData, secondSpecId, dataSource); createSecondProduct(secondValues, initData, secondSpecId, dataSource);
} else { } else {
const specFirst = createInitProduct(); const specFirst = createInitProduct();
......
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