2 changed files with 72 additions and 61 deletions
@ -1,67 +1,63 @@ |
|||||
<template> |
<template> |
||||
<a-menu |
|
||||
v-model:openKeys="openKeys" |
|
||||
v-model:selectedKeys="selectedKeys" |
|
||||
style="width: 256px" |
|
||||
mode="inline" |
|
||||
:items="items" |
|
||||
@click="handleClick" |
|
||||
></a-menu> |
|
||||
|
<div> |
||||
|
<ul> |
||||
|
<li v-for="item in processedData" :key="item.date"> |
||||
|
{{ item.date }}: {{ item.total_count }} |
||||
|
</li> |
||||
|
</ul> |
||||
|
</div> |
||||
</template> |
</template> |
||||
<script lang="ts" setup> |
|
||||
import { reactive, ref, watch, VueElement, h } from 'vue'; |
|
||||
import { MailOutlined, AppstoreOutlined, SettingOutlined } from '@ant-design/icons-vue'; |
|
||||
import type { MenuProps, ItemType } from 'ant-design-vue'; |
|
||||
|
|
||||
const selectedKeys = ref<string[]>(['1']); |
|
||||
const openKeys = ref<string[]>(['sub1']); |
|
||||
|
<script setup lang="ts"> |
||||
|
import { ref, onMounted } from 'vue'; |
||||
|
|
||||
function getItem( |
|
||||
label: VueElement | string, |
|
||||
key: string, |
|
||||
icon?: any, |
|
||||
children?: ItemType[], |
|
||||
type?: 'group', |
|
||||
): ItemType { |
|
||||
return { |
|
||||
key, |
|
||||
icon, |
|
||||
children, |
|
||||
label, |
|
||||
type, |
|
||||
} as ItemType; |
|
||||
|
const generateDateSequence = (days: number) => { |
||||
|
const dates = []; |
||||
|
const today = new Date(); |
||||
|
for (let i = 0; i < days; i++) { |
||||
|
const date = new Date(today); |
||||
|
date.setDate(today.getDate() - i); |
||||
|
dates.push(date.toISOString().split('T')[0]); // 格式化为 YYYY-MM-DD |
||||
} |
} |
||||
|
return dates.reverse(); // 反转顺序,从最早的日期到今天 |
||||
|
}; |
||||
|
|
||||
const items: ItemType[] = reactive([ |
|
||||
getItem('Navigation One', 'sub1', () => h(MailOutlined), [ |
|
||||
getItem('Item 1', 'g1', null, [getItem('Option 1', '1'), getItem('Option 2', '2')], 'group'), |
|
||||
getItem('Item 2', 'g2', null, [getItem('Option 3', '3'), getItem('Option 4', '4')], 'group'), |
|
||||
]), |
|
||||
|
const processQueryResults = (dateSequence: string[], queryResults: any[]) => { |
||||
|
const resultMap = new Map(); |
||||
|
queryResults.forEach((item: any) => { |
||||
|
resultMap.set(item.date, item.total_count); |
||||
|
}); |
||||
|
|
||||
getItem('Navigation Two', 'sub2', () => h(AppstoreOutlined), [ |
|
||||
getItem('Option 5', '5'), |
|
||||
getItem('Option 6', '6'), |
|
||||
getItem('Submenu', 'sub3', null, [getItem('Option 7', '7'), getItem('Option 8', '8')]), |
|
||||
]), |
|
||||
|
return dateSequence.map(date => ({ |
||||
|
date, |
||||
|
total_count: resultMap.get(date) || 0 |
||||
|
})); |
||||
|
}; |
||||
|
|
||||
{ type: 'divider' }, |
|
||||
|
const rawData = ref<any[]>([]); // 从API获取的原始数据 |
||||
|
const processedData = ref<any[]>([]); // 处理后的数据 |
||||
|
|
||||
getItem('Navigation Three', 'sub4', () => h(SettingOutlined), [ |
|
||||
getItem('Option 9', '9'), |
|
||||
getItem('Option 10', '10'), |
|
||||
getItem('Option 11', '11'), |
|
||||
getItem('Option 12', '12'), |
|
||||
]), |
|
||||
|
const fetchData = () => { |
||||
|
// 模拟从API获取数据 |
||||
|
const apiData = [ |
||||
|
{ date: '2024-07-12', total_count: 3 }, |
||||
|
{ date: '2024-07-08', total_count: 1 }, |
||||
|
{ date: '2024-07-05', total_count: 18 }, |
||||
|
{ date: '2024-07-04', total_count: 1 } |
||||
|
]; |
||||
|
|
||||
getItem('Group', 'grp', null, [getItem('Option 13', '13'), getItem('Option 14', '14')], 'group'), |
|
||||
]); |
|
||||
|
rawData.value = apiData; |
||||
|
|
||||
const handleClick: MenuProps['onClick'] = e => { |
|
||||
console.log('click', e); |
|
||||
|
const dateSequence = generateDateSequence(60); |
||||
|
processedData.value = processQueryResults(dateSequence, rawData.value); |
||||
|
console.log(`output->`,processedData.value) |
||||
}; |
}; |
||||
|
|
||||
watch(openKeys, val => { |
|
||||
console.log('openKeys', val); |
|
||||
|
onMounted(() => { |
||||
|
fetchData(); |
||||
}); |
}); |
||||
</script> |
</script> |
||||
|
|
||||
|
<style> |
||||
|
/* 在这里添加你的样式 */ |
||||
|
</style> |
Write
Preview
Loading…
Cancel
Save
Reference in new issue