【Vue.JS】 Vue.js 制作数据走势记录表

效果图

在这里插入图片描述

在线预览

GitHub链接

代码

<!DOCTYPE html>

<head>
    <!-- <meta http-equiv="Refresh" content="5" /> -->
    <title></title>
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.11/vue.js"></script>
    <style>
        [v-cloak] {
            display: none;
        }

        * {
            margin: 0;
            padding: 0;
        }

        html,
        body {
            height: 100%;
        }

        #app {
            width: 100%;
            height: 100%;
        }

        table {
            table-layout: fixed;
            width: 100%;
            border-collapse: collapse;
            margin: 0 auto;
            text-align: center;
            font-size: 6px;
        }

        table caption {
            font-size: 18px;
            font-weight: 600;
            padding-bottom: 10px;
        }

        table td,
        table th {
            border: 1px solid #cad9ea;
            color: #666;
			height:13px;
            max-height: 30px;
            width: 30px;
        }

        table thead th {
            background-color: #CCE8EB;
        }

        table tr:nth-child(odd) {
            background: #fff;
        }

        table tr:nth-child(even) {
            background: #F5FAFA;
        }

        table .thead-col {
            max-width: 65px;
        }

        table td img{
            width: 63px;
            height: 34px;
        }

        p {
            font-size: 4px;
        }
    </style>

<body>
    <div id="app" v-cloak>
        <table style="height: 53%;">
            <caption v-text="title"></caption>
            <thead>
                <tr>
                    <td rowspan="2" class="thead-col">
                        <img src="./src/th1.png" alt="">
                    </td>
                    <td colspan="2" v-for="day in getAllDays()">{{day | longDateToDay}}</td>
                </tr>
                <tr>
                    <td v-for="i in getAllDays().length * 2" v-text="i & 1 == 1 ?'A班': 'B班'"></td>
                </tr>
            </thead>
            <tbody>
                <tr v-for="t in tem_datas">
                    <td class="thead-col" v-text="t.flag + ''"></td>
                    <td v-for="i in getAllDays().length * 2"
                        v-text="t.flag == tem_text_flag(Math.round(t.tem_data[date_list[i]])) ? t.tem_data[date_list[i]] : ''"></td>
                </tr>
                <tr>
                    <td class="thead-col">测试数据℃</td>
                    <td v-for="i in getAllDays().length * 2"></td>
                </tr>
                <tr>
                    <td class="thead-col">天气状况</td>
                    <td v-for="i in getAllDays().length * 2"></td>
                </tr>
            </tbody>
        </table>
        <table style="height: 33%;">
            <thead>
                <tr>
                    <td rowspan="2" class="thead-col">
                        <img src="./src/th2.png" alt="">
                    </td>
                    <td colspan="2" v-for="day in getAllDays()">{{day | longDateToDay}}</td>
                </tr>
                <tr>
                    <td v-for="i in getAllDays().length * 2" v-text="i & 1 == 1 ?'A班': 'B班'"></td>
                </tr>
            </thead>
            <tbody>
                <tr v-for="(t, i) in hum_datas">
                    <td rowspan="2" class="thead-col" v-if="i % 2 == 0"  v-text="t.flag + '% RH'"></td>
                    <td v-for="n in getAllDays().length * 2" v-if="i % 2 == 0"
                        v-text="t.flag == hum_text_flag(t.hum_data[date_list[n]]) ? t.hum_data[date_list[n]] : ''"></td>
					<td v-for="n in getAllDays().length * 2" v-if="i % 2 == 1"
                        v-text="t.flag == hum_text_flag(t.hum_data[date_list[n]]) ? '' : ''"></td>
                </tr>
                <tr>
                    <td rowspan="2" class="thead-col">测试数据(% RH)</td>
                    <td v-for="i in getAllDays().length * 2"></td>
                </tr>
                <tr>
                    <td v-for="i in getAllDays().length * 2"></td>
                </tr>
            </tbody>
        </table>

        <div class="desc" style="float: left; margin-top:14px;">
            <p> 说明: 1. 如相对湿度 &gt; 75% 采取相应措施(开风扇、除去物料 PE 膜、加大物料摆放间距 </p>
            <p style="text-indent:10em">2. 仓库参照《仓库相对湿度点检表》作此表记录 (AM4:00/AM9:00), 相对湿度管制范围 &lt; 85%</p>
            <p style="text-indent:10em">3. 异常时记录人员反馈给当班线组长</p>
        </div>

        <div class="desc" style="float: right; height: 8%;">
            <div style="margin-top: 30px; width: 750px;">
                <span style="margin-right: 190px;">核准: </span>
                <span style="margin-right: 190px;">审核:</span>
                <span style="margin-right: 190px;">填表:</span></div>
        </div>
    </div>

    <script>
        var fDate = function (value) {
            return value < 10 ? '0' + value : value;
        }
        Date.prototype.format = function () {
            return this.getFullYear() + '-' + fDate(this.getMonth() + 1) + '-' + fDate(this.getDate());
        };
        var app = new Vue({
            el: "#app",
            data: {
                title: '',//表格标题
                start_date: '',//表头的开始日期
                end_date: '',//表头结束日期
                tem_data: {},//温度数据
                hum_data: {},//湿度数据
                min_tem: 0,//最小温度(左侧表头) 如为空,则取 tem_data 中值得最小值
                max_tem: 0,//最大温度(左侧表头) 如为空,则取 tem_data 中值的最大值
                flag_tems: [],//温度数组(左侧表头) 如不为空,则优先使用数组内容作为表头,如为空,则根据 min_tem 和 max tem 和 flag_tem_step 自动计算填充
                flag_tem_step: 1,//温度步长,当 flag_tems 为空时,根据最大最小值计算填充表头数组时的间隔
                min_hum: 0,// 最小湿度
                max_hum: 0,// 最大湿度
                flag_hums: [],//湿度数组
                flag_hum_step: 5//湿度步长
            },
            methods: {
				//温度数据展示规则,可以根据需求自定义。
			    tem_text_flag(n){
					return this.flag_tems.find(i => Math.abs(n - i) < this.flag_tem_step);
				},
				//湿度数据展示规则,可以根据需求自定义
				hum_text_flag(n){
					return this.flag_hums.find(i => n <= i);
				},
                //根据日期区间获取所有日期,返回数组
                getAllDays() {
                    var allDays = [];
                    var a = (this.start_date || '').split('-');
                    var b = (this.end_date || '').split('-');
                    var uDb = new Date().setUTCFullYear(a[0], a[1] - 1, a[2]);
                    var uDe = new Date().setUTCFullYear(b[0], b[1] - 1, b[2]);
                    for (var i = uDb; i <= uDe; i = i + 24 * 60 * 60 * 1000) {
                        allDays.push(new Date(parseInt(i)).format());
                    }
                    return allDays;
                },
                //温度数组为空时,自动根据其他设定填充温度数组
                fill_flag_tem() {
                    if (this.flag_tems.length == 0) {
                        if (!this.max_tem)
                            this.max_tem = Math.max(...Object.values(this.tem_data))
                        if (!this.min_tem)
                            this.min_tem = Math.min(...Object.values(this.tem_data))
						//降序
                        while (this.min_tem <= this.max_tem) {
							this.max_tem = parseFloat(this.max_tem)
							this.min_tem = parseFloat(this.min_tem)
                            this.flag_tems.push(this.max_tem)
                            this.max_tem = (this.max_tem - this.flag_tem_step).toFixed(1)
                        }
                    }
                },
                //湿度数组为空时,自动根据其他设定填充温度数组
                fill_flag_hum() {
                    if (this.flag_hums.length == 0) {
                        if (!this.max_hum)
                            this.max_hum = Math.max(...Object.values(this.hum_data))
                        if (!this.min_hum)
                            this.min_hum = Math.min(...Object.values(this.hum_data))
						//升序
                        while (this.min_hum <= this.max_hum) {
							this.min_hum = parseFloat(this.min_hum)
							this.max_hum = parseFloat(this.max_hum)
                            this.flag_hums.push(this.min_hum)
                            this.flag_hums.push(this.min_hum)
                            this.min_hum = (this.min_hum += this.flag_hum_step).toFixed(1)
                        }
                    }
                }
            },
            filters: {
                //日期过滤(dd)
                longDateToDay: function (longDate) {
                    return fDate(new Date(Date.parse(longDate)).getDate());
                },
                //日期过滤(mm)
                longDateToMonth: function (longDate) {
                    return fDate(new Date(Date.parse(longDate)).getMonth() + 1);
                }
            },
            computed: {
                //日期数组,后台返回的所有键,温湿度去重合并,用于表头取值
                date_list() {
                    return Array.from(new Set(Object.keys(this.tem_data).concat(Object.keys(this.hum_data))))
                },
                //温度数组组合
                tem_datas() {
                    var t = []
                    this.flag_tems.map((flag) => {
                        t.push({
                            flag, tem_data: this.tem_data
                        })
                    })
                    return t
                },
                //湿度数组组合
                hum_datas() {
                    var t = []
                    this.flag_hums.map((flag) => {
                        t.push({
                            flag, hum_data: this.hum_data
                        })
                    })
                    return t
                }
            },
            mounted() {
                this.fill_flag_tem()
                this.fill_flag_hum()
            },
            created() {
                //以下为示例数据,通常从 ajax 获取
                this.title = "仓库温湿度走势记录表"
                this.start_date = "2020-06-01"
                this.end_date = "2020-06-30"

                this.flag_tems = []
                this.max_tem = ''
                this.min_tem = ''
                this.flag_tem_step = 2

                this.flag_hums = []
                this.max_hum = 90
                this.min_hum = 55
                this.flag_hum_step = 5

                this.tem_data = {
                    "2020-06-01-A": (Math.random()*20+10).toFixed(1),
                    "2020-06-01-B": (Math.random()*20+10).toFixed(1),
                    "2020-06-02-A": (Math.random()*20+10).toFixed(1),
                    "2020-06-02-B": (Math.random()*20+10).toFixed(1),
                    "2020-06-03-A": (Math.random()*20+10).toFixed(1),
                    "2020-06-03-B": (Math.random()*20+10).toFixed(1),
                    "2020-06-04-A": (Math.random()*20+10).toFixed(1),
                    "2020-06-04-B": (Math.random()*20+10).toFixed(1),
                    "2020-06-05-A": (Math.random()*20+10).toFixed(1),
                    "2020-06-05-B": (Math.random()*20+10).toFixed(1),
                    "2020-06-06-A": (Math.random()*20+10).toFixed(1),
                    "2020-06-06-B": (Math.random()*20+10).toFixed(1),
                    "2020-06-07-A": (Math.random()*20+10).toFixed(1),
                    "2020-06-07-B": (Math.random()*20+10).toFixed(1),
                    "2020-06-08-A": (Math.random()*20+10).toFixed(1),
                    "2020-06-08-B": (Math.random()*20+10).toFixed(1),
                    "2020-06-09-A": (Math.random()*20+10).toFixed(1),
                    "2020-06-09-B": (Math.random()*20+10).toFixed(1),
                    "2020-06-10-A": (Math.random()*20+10).toFixed(1),
                    "2020-06-10-B": (Math.random()*20+10).toFixed(1),
                    "2020-06-11-A": (Math.random()*20+10).toFixed(1),
                    "2020-06-11-B": (Math.random()*20+10).toFixed(1),
                    "2020-06-12-A": (Math.random()*20+10).toFixed(1),
                    "2020-06-12-B": (Math.random()*20+10).toFixed(1),
                    "2020-06-13-A": (Math.random()*20+10).toFixed(1),
                    "2020-06-13-B": (Math.random()*20+10).toFixed(1),
                    "2020-06-14-A": (Math.random()*20+10).toFixed(1),
                    "2020-06-14-B": (Math.random()*20+10).toFixed(1),
                    "2020-06-15-A": (Math.random()*20+10).toFixed(1),
                    "2020-06-15-B": (Math.random()*20+10).toFixed(1),
                    "2020-06-16-A": (Math.random()*20+10).toFixed(1),
                    "2020-06-16-B": (Math.random()*20+10).toFixed(1),
                    "2020-06-17-A": (Math.random()*20+10).toFixed(1),
                    "2020-06-17-B": (Math.random()*20+10).toFixed(1),
                }
                this.hum_data = {
                    "2020-06-01-A": (Math.random()*40+50).toFixed(1),
                    "2020-06-01-B": (Math.random()*40+50).toFixed(1),
                    "2020-06-02-A": (Math.random()*40+50).toFixed(1),
                    "2020-06-02-B": (Math.random()*40+50).toFixed(1),
                    "2020-06-03-A": (Math.random()*40+50).toFixed(1),
                    "2020-06-03-B": (Math.random()*40+50).toFixed(1),
                    "2020-06-04-A": (Math.random()*40+50).toFixed(1),
                    "2020-06-04-B": (Math.random()*40+50).toFixed(1),
                    "2020-06-05-A": (Math.random()*40+50).toFixed(1),
                    "2020-06-05-B": (Math.random()*40+50).toFixed(1),
                    "2020-06-06-A": (Math.random()*40+50).toFixed(1),
                    "2020-06-06-B": (Math.random()*40+50).toFixed(1),
                    "2020-06-07-A": (Math.random()*40+50).toFixed(1),
                    "2020-06-07-B": (Math.random()*40+50).toFixed(1),
                    "2020-06-08-A": (Math.random()*40+50).toFixed(1),
                    "2020-06-08-B": (Math.random()*40+50).toFixed(1),
                    "2020-06-09-A": (Math.random()*40+50).toFixed(1),
                    "2020-06-09-B": (Math.random()*40+50).toFixed(1),
                    "2020-06-10-A": (Math.random()*40+50).toFixed(1),
                    "2020-06-10-B": (Math.random()*40+50).toFixed(1),
                    "2020-06-11-A": (Math.random()*40+50).toFixed(1),
                    "2020-06-11-B": (Math.random()*40+50).toFixed(1),
                    "2020-06-12-A": (Math.random()*40+50).toFixed(1),
                    "2020-06-12-B": (Math.random()*40+50).toFixed(1),
                    "2020-06-13-A": (Math.random()*40+50).toFixed(1),
                    "2020-06-13-B": (Math.random()*40+50).toFixed(1),
                    "2020-06-14-A": (Math.random()*40+50).toFixed(1),
                    "2020-06-14-B": (Math.random()*40+50).toFixed(1),
                    "2020-06-15-A": (Math.random()*40+50).toFixed(1),
                    "2020-06-15-B": (Math.random()*40+50).toFixed(1),
                    "2020-06-16-A": (Math.random()*40+50).toFixed(1),
                    "2020-06-16-B": (Math.random()*40+50).toFixed(1),
                    "2020-06-17-A": (Math.random()*40+50).toFixed(1),
                    "2020-06-17-B": (Math.random()*40+50).toFixed(1),
                }
            }
        });
    </script>
</body>
</head>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HolaSecurity

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值