当前位置: 代码网 > it编程>编程语言>Javascript > Avue实现动态查询与数据展示的示例代码

Avue实现动态查询与数据展示的示例代码

2024年09月05日 Javascript 我要评论
下面是一个前端查询页面的总结,包括demo示例和注释1. 基本知识数据展示配置 (optiontotal, datatotal, totalpage)<avue-crud :option="o

下面是一个前端查询页面的总结,包括demo示例和注释

1. 基本知识

数据展示配置 (optiontotal, datatotal, totalpage)

<avue-crud
  :option="optiontotal"
  :data="datatotal"
  :page="totalpage"
  @on-load="loadpage">
  <!-- 插槽模板 -->
  <template slot-scope="scope" slot="menu">
    <el-button style="margin-left:10px;" size="small" type="text" @click.native="showdetail(scope.row)">查看</el-button>
  </template>
  <template slot-scope="scope" slot="remaincapacity" class="el-progress-bar__innertext">
    <el-progress :text-inside="true" :stroke-width="24" :color="percentagestyle(scope.row.remaincapacity)" :percentage="scope.row.remaincapacity"></el-progress>
  </template>
</avue-crud>
  • :option="optiontotal":配置avue-crud组件的显示选项,如表格列的定义、是否显示删除、编辑、添加按钮等
  • :data="datatotal": 绑定要显示的数据,通常是从api获取的结果数组
  • :page="totalpage":配置分页信息,包括当前页码、每页大小、总记录数等
  • @on-load="loadpage": 当数据需要加载时调用loadpage方法,通常用于处理分页和数据加载

基本的注意事项如下:

  • 分页参数:
    确保分页参数(currentpage和pagesize)正确传递,并与后端api的分页要求一致
    在分页变化时(如页码变动),需要重新加载数据以反映当前页的数据

  • 数据绑定:
    :data="datatotal"绑定的数据应确保格式正确,并与表格列定义中的prop属性一致
    数据中每一项的字段名称应与optiontotal中定义的列字段一致,以确保数据能正确显示

  • 插槽使用:
    slot="menu"用于自定义行操作按钮,如“查看”按钮
    slot="remaincapacity"用于自定义进度条显示,动态设置颜色和百分比,提供直观的设备状态反馈

2. demo

以下为充电桩的实时动态数据,通过plc实时传输到数据库中,对应这个页面动态查询数据即可

整体界面逻辑如下:

  1. 组件初始化:在组件挂载时,启动定时器每30秒自动刷新数据
  2. 查询功能
    -搜索:根据输入的条件(如车辆名称)查询数据并更新展示
    -重置:重置表单字段和查询条件,重新加载数据
  3. 数据显示:通过avue-crud组件展示车辆信息,包括车辆名称、状态、充电枪状态、电池温度、剩余电量和更新时间等
  4. 详情查看:点击“查看”按钮时,跳转到设备详情页面,并将设备名称作为查询参数传递
  5. 设备详情对话框:显示设备详细信息的对话框(在此例中为空)
<template>
  <div>
    <basic-container>
      <!-- 查询表单 -->
      <el-form :inline="true" ref="forminline" :model="forminline" label-width="150px">
        <el-form-item label="车辆名称">
          <el-input v-model="forminline.devicename" placeholder="请输入车辆名称"></el-input>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" size="small" @click="onsearch">查询</el-button>
          <el-button size="small" @click="resetform('forminline')">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 数据展示卡片 -->
      <el-card class="box-card">
        <div class="clearfix">
          <span>总数为:{{totalpage.total}}辆</span>
          <avue-crud
            :option="optiontotal"
            :data="datatotal"
            :page="totalpage"
            @on-load="loadpage">
            <!-- 查看按钮模板 -->
            <template slot-scope="scope" slot="menu">
              <el-button style="margin-left:10px;" size="small" type="text" @click.native="showdetail(scope.row)">查看</el-button>
            </template>
            <!-- 剩余电量进度条模板 -->
            <template slot-scope="scope" slot="remaincapacity" class="el-progress-bar__innertext">
              <el-progress :text-inside="true" :stroke-width="24" :color="percentagestyle(scope.row.remaincapacity)" :percentage="scope.row.remaincapacity"></el-progress>
            </template>
          </avue-crud>
        </div>
      </el-card>
    </basic-container>

    <!-- 设备详情对话框 -->
    <el-dialog title="设备详情" :append-to-body='true' :visible="detailvisible" @close="closedetialdialog"></el-dialog>
  </div>
</template>

<script>
import { getdevicereallist } from "@/api/equipment/chargingschedule/devicereal";

export default {
  data() {
    return {
      timer: null,
      detailvisible: false,
      query: {},
      totalpage: {
        pagesize: 20,
        currentpage: 1,
        total: 0
      },
      forminline: {
        devicename: ''
      },
      datatotal: [],
      optiontotal: {
        height: 'auto',
        calcheight: 95,
        fit: true,
        border: true,
        delbtn: false,
        editbtn: false,
        addbtn: false,
        menuwidth: 100,
        highlightcurrentrow: true,
        column: [
          {
            label: '车辆名称',
            prop: 'devicename'
          },
          {
            label: '车辆状态',
            prop: 'vehiclestatus',
            dicdata: [
              { label: '启动', value: '01' },
              { label: '熄火', value: '02' },
              { label: '充电', value: '03' },
              { label: '离线', value: '99' }
            ]
          },
          {
            label: '充电枪状态',
            prop: 'chargegun',
            dicdata: [
              { label: '未连接', value: '00' },
              { label: '已连接', value: '11' }
            ]
          },
          {
            label: '电池系统温度(℃)',
            prop: 'batterytemp'
          },
          {
            label: '剩余电量(%)',
            prop: 'remaincapacity',
            slot: true
          },
          {
            label: '更新时间',
            prop: 'infoupdatetime',
            width: '150'
          },
          {
            label: '时间差值(天)',
            prop: 'timedifferenceday',
            width: '70',
            formatter: (row) => this.calculatetimedifference(row.infoupdatetime)
          }
        ]
      }
    };
  },
  computed: {
    currenttime() {
      return new date();
    }
  },
  mounted() {
    // 定时刷新页面
    this.timer = setinterval(() => {
      settimeout(() => this.loadpage(this.totalpage, this.query), 0);
    }, 1000 * 30);
  },
  methods: {
    calculatetimedifference(updatetime) {
      const updatetimeobj = new date(updatetime);
      const timedifference = this.currenttime - updatetimeobj; // 时间差值,单位为毫秒
      const daydifference = math.floor(timedifference / (1000 * 60 * 60 * 24));
      return daydifference;
    },
    onsearch() {
      let searchinfo = {
        devicename: this.forminline.devicename === '' ? null : this.forminline.devicename
      };
      this.totalpage.currentpage = 1;
      this.loadpage(this.totalpage, searchinfo);
    },
    resetform(formname) {
      this.$refs[formname].resetfields();
      this.forminline.devicename = null;
      this.onsearch();
    },
    showdetail(row) {
      this.$router.push({ path: '/equipment/chargingschedule/deviceinfovisual', query: { devicename: row.devicename } });
    },
    loadpage(page, params = {}) {
      getdevicereallist(page.currentpage, page.pagesize, object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.totalpage.total = data.total;
        this.datatotal = data.records;
      });
    },
    percentagestyle(percentage) {
      if (percentage <= 20) return '#cd0000';
      if (percentage > 20 && percentage <= 40) return '#ff0000';
      if (percentage > 80) return '#32cd32';
      if (percentage > 60 && percentage <= 80) return '#eeee00';
      if (percentage <= 60 && percentage > 40) return '#eec900';
    },
    closedetialdialog() {
      this.detailvisible = false;
    }
  }
}
</script>

<style>
.el-progress-bar__innertext {
  color: #0b0a0a;
  font-size: 12px;
  margin: 0px 5px;
}
</style>

最终界面如下所示:

到此这篇关于avue实现动态查询与数据展示的示例代码的文章就介绍到这了,更多相关avue动态查询与数据展示内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com