Browse Source

修复monitor的bug

skyfffire 2 months ago
parent
commit
63f246a829
1 changed files with 34 additions and 36 deletions
  1. 34 36
      monitor.html

+ 34 - 36
monitor.html

@@ -143,11 +143,20 @@
             function renderHistoryDataTable(data) {
                 const tableContainer = document.getElementById('history-table');
     
-                if (!data || data.length === 0) {
-                    if (historyDataTable) {
+                // 始终销毁现有的DataTable(如果存在)
+                if (historyDataTable) {
+                    try {
                         historyDataTable.destroy();
-                        historyDataTable = null;
+                    } catch (e) {
+                        console.warn("销毁DataTable时出错:", e);
                     }
+                    historyDataTable = null;
+                }
+                
+                // 清空容器
+                tableContainer.innerHTML = '';
+                
+                if (!data || data.length === 0) {
                     tableContainer.innerHTML = '<thead><tr><th>交易对</th><th>状态</th><th>利润</th><th>创建时间</th><th>操作</th></tr></thead><tbody><tr><td colspan="5" class="text-center p-4 text-gray-500">没有历史记录。</td></tr></tbody>';
                     historyCountBadge.textContent = 0;
                     return;
@@ -169,40 +178,29 @@
 
                 historyCountBadge.textContent = data.length;
                 
-                // 如果 DataTable 已存在,先检查数据是否有变化
-                if (historyDataTable) {
-                    // 简单的数据比较(可以根据需要优化)
-                    const currentDataLength = historyDataTable.data.data.length;
-                    if (currentDataLength !== tableData.length) {
-                        // 数据量发生变化,需要重建
-                        historyDataTable.destroy();
-                        historyDataTable = null;
-                        tableContainer.innerHTML = '';
-                    } else {
-                        // 数据量相同,可能不需要重建(这里可以做更细致的比较)
-                        return;
-                    }
-                }
-                
-                // 创建新的 DataTable
+                // 延迟创建新的DataTable,确保DOM完全清理
                 setTimeout(() => {
-                    historyDataTable = new simpleDatatables.DataTable(tableContainer, {
-                        data: { headings, data: tableData },
-                        paging: false,
-                        perPageSelect: false,
-                        searchable: true,
-                        labels: { placeholder: "搜索...", noRows: "未找到记录" },
-                        columns: [ 
-                            { select: 2, type: 'number' },
-                            { select: 3, type: 'date', format: "YYYY-MM-DD HH:mm:ss.SSS" },
-                            { select: 4, sortable: false }
-                        ]
-                    });
-                    
-                    historyDataTable.on('datatable.init', () => {
-                        historyDataTable.columns.sort(3, 'desc');
-                    });
-                }, 50);
+                    try {
+                        historyDataTable = new simpleDatatables.DataTable(tableContainer, {
+                            data: { headings, data: tableData },
+                            paging: false,
+                            perPageSelect: false,
+                            searchable: true,
+                            labels: { placeholder: "搜索...", noRows: "未找到记录" },
+                            columns: [ 
+                                { select: 2, type: 'number' },
+                                { select: 3, type: 'date', format: "YYYY-MM-DD HH:mm:ss.SSS" },
+                                { select: 4, sortable: false }
+                            ]
+                        });
+                        
+                        historyDataTable.on('datatable.init', () => {
+                            historyDataTable.columns.sort(3, 'desc');
+                        });
+                    } catch (e) {
+                        console.error("创建DataTable失败:", e);
+                    }
+                }, 100);
             }
 
             // --- Main Logic Functions ---