用 antdv + vue3+ ts,写一个本地导入execl文件;到table列表中,并保存在本地缓存。可以编辑某一行的数据并保存在本地缓存;table列表字段有证券代码,证券名称,市值,浮动盈亏,浮动盈亏比,当日盈亏,当日盈亏率, 成本价,现价,股票数量,仓位,持股天数
时间: 2023-12-02 20:04:12 浏览: 105
vue3+ts+vuerouter+pinia+elementplus+ts环境模板搭建及vite打包优化
5星 · 资源好评率100%
好的,我可以为您提供一个基本的实现思路:
1. 创建一个包含所需字段的数据模型,例如:
```
interface StockData {
code: string;
name: string;
marketValue: number;
floatingProfitAndLoss: number;
floatingProfitAndLossRatio: number;
dailyProfitAndLoss: number;
dailyProfitAndLossRatio: number;
costPrice: number;
currentPrice: number;
quantity: number;
position: number;
holdingDays: number;
}
```
2. 创建一个上传 Excel 文件的组件,使用 `antdv` 的 `Upload` 组件,并设置 `beforeUpload` 钩子函数,将 Excel 文件转换为数据模型数组:
```
<template>
<a-upload
:beforeUpload="handleUpload"
:showUploadList="false"
>
<a-button>
<a-icon type="upload"></a-icon>
Upload Excel
</a-button>
</a-upload>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
import XLSX from 'xlsx';
import { StockData } from '@/models/StockData';
export default defineComponent({
methods: {
handleUpload(file: File) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = (e: ProgressEvent<FileReader>) => {
const data = new Uint8Array(e.target!.result as ArrayBuffer);
const workbook = XLSX.read(data, { type: 'array' });
const worksheet = workbook.Sheets[workbook.SheetNames[0]];
const jsonData = XLSX.utils.sheet_to_json<StockData>(worksheet);
// 将 jsonData 存入本地缓存
localStorage.setItem('stockData', JSON.stringify(jsonData));
resolve(null);
};
reader.readAsArrayBuffer(file);
});
},
},
});
</script>
```
3. 创建一个显示数据的表格组件,使用 `antdv` 的 `Table` 组件,并从本地缓存中读取数据:
```
<template>
<a-table :dataSource="dataSource" :columns="columns" />
</template>
<script lang="ts">
import { defineComponent } from 'vue';
import { StockData } from '@/models/StockData';
export default defineComponent({
data() {
return {
dataSource: [] as StockData[],
columns: [
{
title: 'Code',
dataIndex: 'code',
},
{
title: 'Name',
dataIndex: 'name',
},
{
title: 'Market Value',
dataIndex: 'marketValue',
},
// ... 其他列的配置
],
};
},
created() {
const stockData = localStorage.getItem('stockData');
if (stockData) {
this.dataSource = JSON.parse(stockData);
}
},
});
</script>
```
4. 创建一个编辑数据的组件,使用 `antdv` 的 `Modal` 和 `Form` 组件,并将修改后的数据更新到本地缓存:
```
<template>
<a-modal :visible="visible" :onOk="handleOk" :onCancel="handleCancel">
<a-form :form="form" :initialValues="initialValues">
<a-form-item label="Code" name="code">
<a-input disabled />
</a-form-item>
<a-form-item label="Name" name="name">
<a-input disabled />
</a-form-item>
<a-form-item label="Market Value" name="marketValue">
<a-input-number />
</a-form-item>
<!-- ... 其他表单项 -->
</a-form>
</a-modal>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
import { Modal, Form, Input, InputNumber } from 'ant-design-vue';
import { StockData } from '@/models/StockData';
export default defineComponent({
components: {
Modal,
Form,
AInput: Input,
AInputNumber: InputNumber,
},
props: {
visible: Boolean,
stockData: Object as () => StockData,
},
emits: ['update:visible'],
data() {
return {
form: {} as any,
initialValues: {} as StockData,
};
},
watch: {
stockData(newValue: StockData) {
this.initialValues = { ...newValue };
this.form.setFieldsValue({ ...newValue });
},
},
methods: {
handleOk() {
this.form.validateFields().then((values: StockData) => {
const dataSource = JSON.parse(localStorage.getItem('stockData') || '[]');
const index = dataSource.findIndex((item: StockData) => item.code === values.code);
dataSource.splice(index, 1, { ...values });
localStorage.setItem('stockData', JSON.stringify(dataSource));
this.$emit('update:visible', false);
});
},
handleCancel() {
this.$emit('update:visible', false);
},
},
});
</script>
```
最后,在需要使用的页面中,引入以上三个组件即可:
```
<template>
<div>
<stock-upload />
<stock-table />
<stock-edit
:visible="visible"
:stockData="editingData"
@update:visible="visible = $event"
/>
</div>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue';
import StockUpload from '@/components/StockUpload.vue';
import StockTable from '@/components/StockTable.vue';
import StockEdit from '@/components/StockEdit.vue';
import { StockData } from '@/models/StockData';
export default defineComponent({
components: {
StockUpload,
StockTable,
StockEdit,
},
setup() {
const visible = ref(false);
const editingData = ref({} as StockData);
return {
visible,
editingData,
};
},
});
</script>
```
阅读全文