async getPorts () { const outputArr = [] const promises = [] for (let i = 1; i <= 7; i++) { promises.push( electricQuery({ electricId: ELECTRIC_ID, commandKey: 15, queryMode: 1, week: i }).then(({ data }) => { if (data[0]) { outputArr.push(data[0].timers) } }) ) } await Promise.all(promises) console.log({ data: outputArr }) return { data: outputArr } }, 每次调用接口得到周一到周日其中一天的数据 数据形态是[0 : {startTime: "12:0:0", stopTime: "22:0:0"} 1 : {startTime: "0:0:0", stopTime: "0:0:0"} 2 : {startTime: "0:0:0", stopTime: "0:0:0"} 3 : {startTime: "0:0:0", stopTime: "0:0:0"}] 现在需要把一周七天的数据渲染 使用elementUI 的grid布局 代码怎么更改
时间: 2023-11-27 08:53:36 浏览: 77
首先,您需要将获取到的数据处理成适合 Element UI Grid 布局的形式。具体地,您需要将每个星期的数据转换为一行(即一周七天),每一列对应一个时间段(例如,上午 8 点到下午 6 点),并在每个单元格中显示该时间段是否被设置为开启状态。
下面是一种可能的解决方案:
```
<template>
<el-row>
<el-col :span="4">时间段</el-col>
<el-col v-for="day in days" :key="day" :span="3">{{ day }}</el-col>
</el-row>
<el-row v-for="(time, index) in times" :key="index">
<el-col :span="4">{{ time }}</el-col>
<el-col v-for="day in days" :key="day" :span="3">
<el-tag v-if="data[day][index].startTime !== '0:0:0'">{{ data[day][index].startTime }} - {{ data[day][index].stopTime }}</el-tag>
<el-tag v-else type="danger">未设置</el-tag>
</el-col>
</el-row>
</template>
<script>
export default {
data() {
return {
days: ["周一", "周二", "周三", "周四", "周五", "周六", "周日"],
times: ["00:00-01:00", "01:00-02:00", "02:00-03:00", "03:00-04:00", "04:00-05:00", "05:00-06:00", "06:00-07:00", "07:00-08:00", "08:00-09:00", "09:00-10:00", "10:00-11:00", "11:00-12:00", "12:00-13:00", "13:00-14:00", "14:00-15:00", "15:00-16:00", "16:00-17:00", "17:00-18:00", "18:00-19:00", "19:00-20:00", "20:00-21:00", "21:00-22:00", "22:00-23:00", "23:00-24:00"],
data: {}
}
},
async created() {
const result = await this.getPorts()
this.data = this.processData(result.data)
},
methods: {
async getPorts() {
const outputArr = []
const promises = []
for (let i = 1; i <= 7; i++) {
promises.push(
electricQuery({
electricId: ELECTRIC_ID,
commandKey: 15,
queryMode: 1,
week: i
}).then(({ data }) => {
if (data[0]) {
outputArr.push(data[0].timers)
}
})
)
}
await Promise.all(promises)
return { data: outputArr }
},
processData(data) {
const processed = {}
for (let i = 0; i < this.days.length; i++) {
processed[this.days[i]] = data[i] || []
}
return processed
}
}
}
</script>
```
这个代码片段中,我们使用了 Element UI 的 Grid 布局,并在其中渲染了一周的数据。具体来说,我们将时间段作为行,将每天作为列,并在每个单元格中显示该时间段是否被设置为开启状态。如果该时间段未被设置,则会显示一个红色的“未设置”标签。我们还使用了 `processData` 方法将获取到的数据转换为适合处理的形式。
阅读全文