Kaynağa Gözat

修复monitor是空的bug

skyfffire 2 ay önce
ebeveyn
işleme
e165101b08
1 değiştirilmiş dosya ile 54 ekleme ve 16 silme
  1. 54 16
      monitor.html

+ 54 - 16
monitor.html

@@ -143,20 +143,15 @@
             function renderHistoryDataTable(data) {
             function renderHistoryDataTable(data) {
                 const tableContainer = document.getElementById('history-table');
                 const tableContainer = document.getElementById('history-table');
     
     
-                // 始终销毁现有的DataTable(如果存在)
-                if (historyDataTable) {
-                    try {
-                        historyDataTable.destroy();
-                    } catch (e) {
-                        console.warn("销毁DataTable时出错:", e);
-                    }
-                    historyDataTable = null;
-                }
-                
-                // 清空容器
-                tableContainer.innerHTML = '';
-                
                 if (!data || data.length === 0) {
                 if (!data || data.length === 0) {
+                    if (historyDataTable) {
+                        try {
+                            historyDataTable.destroy();
+                        } catch (e) {
+                            console.warn("销毁DataTable时出错:", e);
+                        }
+                        historyDataTable = null;
+                    }
                     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>';
                     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;
                     historyCountBadge.textContent = 0;
                     return;
                     return;
@@ -178,7 +173,25 @@
 
 
                 historyCountBadge.textContent = data.length;
                 historyCountBadge.textContent = data.length;
                 
                 
-                // 延迟创建新的DataTable,确保DOM完全清理
+                // 安全地检查和销毁现有的 DataTable
+                if (historyDataTable) {
+                    try {
+                        // 更安全的数据比较
+                        const currentDataLength = historyDataTable.data && historyDataTable.data.data ? 
+                            historyDataTable.data.data.length : 0;
+                        
+                        // 总是重建,避免复杂的数据比较逻辑
+                        historyDataTable.destroy();
+                    } catch (e) {
+                        console.warn("处理现有DataTable时出错:", e);
+                    }
+                    historyDataTable = null;
+                }
+                
+                // 清空容器
+                tableContainer.innerHTML = '';
+                
+                // 创建新的 DataTable
                 setTimeout(() => {
                 setTimeout(() => {
                     try {
                     try {
                         historyDataTable = new simpleDatatables.DataTable(tableContainer, {
                         historyDataTable = new simpleDatatables.DataTable(tableContainer, {
@@ -198,9 +211,34 @@
                             historyDataTable.columns.sort(3, 'desc');
                             historyDataTable.columns.sort(3, 'desc');
                         });
                         });
                     } catch (e) {
                     } catch (e) {
-                        console.error("创建DataTable失败:", e);
+                        console.error("创建DataTable时出错:", e);
+                        // 如果创建失败,回退到简单的HTML表格
+                        tableContainer.innerHTML = `
+                            <table class="min-w-full text-sm">
+                                <thead class="bg-gray-50">
+                                    <tr>
+                                        <th class="px-3 py-2 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">交易对</th>
+                                        <th class="px-3 py-2 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">状态</th>
+                                        <th class="px-3 py-2 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">利润</th>
+                                        <th class="px-3 py-2 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">创建时间</th>
+                                        <th class="px-3 py-2 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">操作</th>
+                                    </tr>
+                                </thead>
+                                <tbody class="divide-y divide-gray-200">
+                                    ${tableData.map(row => `
+                                        <tr class="hover:bg-gray-50">
+                                            <td class="px-3 py-2 whitespace-nowrap">${row[0]}</td>
+                                            <td class="px-3 py-2 whitespace-nowrap">${row[1]}</td>
+                                            <td class="px-3 py-2 whitespace-nowrap">${row[2] !== null ? row[2] : 'N/A'}</td>
+                                            <td class="px-3 py-2 whitespace-nowrap">${row[3]}</td>
+                                            <td class="px-3 py-2 whitespace-nowrap">${row[4]}</td>
+                                        </tr>
+                                    `).join('')}
+                                </tbody>
+                            </table>
+                        `;
                     }
                     }
-                }, 100);
+                }, 100); // 增加延迟到100ms,确保DOM完全更新
             }
             }
 
 
             // --- Main Logic Functions ---
             // --- Main Logic Functions ---