baoshiwei
6 天以前 5bf14aed888cd0e258e325c65f14022dad02985b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue';
import { createDataReceiver } from '../utils/dataFetcher';
 
interface ForceData {
  timestamp: string;
  fx: number;
  fy: number;
  fz: number;
  mx: number;
  my: number;
  mz: number;
}
 
const tableData = ref<ForceData[]>([]);
const maxRows = 100; // 最多显示100条记录
 
// 更新表格数据
function updateTable(forceData: number[]) {
  const now = new Date();
  const timeStr = `${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}:${now.getSeconds().toString().padStart(2, '0')}`;
  
  const newData: ForceData = {
    timestamp: timeStr,
    fx: forceData[0],
    fy: forceData[1],
    fz: forceData[2],
    mx: forceData[3],
    my: forceData[4],
    mz: forceData[5]
  };
  
  tableData.value.unshift(newData);
  
  // 限制数据行数
  if (tableData.value.length > maxRows) {
    tableData.value.pop();
  }
}
 
 
 
let dataInterval: number | null = null;
 
onMounted(() => {
  const receiveData = createDataReceiver( (forceData) => {
    updateTable(forceData);
  });
  // 每500ms获取一次数据
  dataInterval = window.setInterval(receiveData, 500);
});
 
onUnmounted(() => {
  if (dataInterval !== null) {
    clearInterval(dataInterval);
  }
});
</script>
 
<template>
  <div class="table-container">
    <table class="force-table">
      <thead>
        <tr>
          <th>时间</th>
          <th>Fx (N)</th>
          <th>Fy (N)</th>
          <th>Fz (N)</th>
          <th>Mx (Nm)</th>
          <th>My (Nm)</th>
          <th>Mz (Nm)</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item, index) in tableData" :key="index">
          <td>{{ item.timestamp }}</td>
          <td>{{ item.fx.toFixed(2) }}</td>
          <td>{{ item.fy.toFixed(2) }}</td>
          <td>{{ item.fz.toFixed(2) }}</td>
          <td>{{ item.mx.toFixed(2) }}</td>
          <td>{{ item.my.toFixed(2) }}</td>
          <td>{{ item.mz.toFixed(2) }}</td>
        </tr>
        <tr v-if="tableData.length === 0">
          <td colspan="7" class="no-data">暂无数据</td>
        </tr>
      </tbody>
    </table>
  </div>
</template>
 
<style scoped>
.table-container {
  width: 98%;
  height: 97%;
  overflow-y: auto;
  margin: 10px;
}
 
.force-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}
 
.force-table th,
.force-table td {
  border: 1px solid #ddd;
  padding: 8px;
  text-align: center;
}
 
.force-table th {
  background-color: #f2f2f2;
  position: sticky;
  top: 0;
  z-index: 1;
}
 
.force-table tr:nth-child(even) {
  background-color: #f9f9f9;
}
 
.force-table tr:hover {
  background-color: #f0f0f0;
}
 
.no-data {
  text-align: center;
  padding: 20px;
  color: #999;
}
</style>