baoshiwei
5 天以前 2ad852ee08e21ee681950f1d6058499248baf88e
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
<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();
  }
}
 
onMounted(() => {
 createDataReceiver((forceData) => {
    updateTable(forceData);
  });
});
 
onUnmounted(() => {
 
});
</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>