如何为HTML表格添加日志记录?有哪些实现方法?

星降
发布: 2025-07-03 17:17:02
原创
701人浏览过
如何为HTML表格添加日志记录?有哪些实现方法?如何为HTML表格添加日志记录?有哪些实现方法?如何为HTML表格添加日志记录?有哪些实现方法?如何为HTML表格添加日志记录?有哪些实现方法?
{
  "timestamp": "2023-10-27T10:30:00Z",
  "userId": "user123",
  "tableId": "product_list",
  "actionType": "edit_cell",
  "rowIndex": 5,
  "columnId": "price",
  "oldValue": "19.99",
  "newValue": "24.50"
}
登录后复制
{
  "timestamp": "2023-10-27T14:55:30.789Z",
  "userId": "john.doe",
  "tableId": "order_details",
  "actionType": "cell_edit",
  "rowIndex": 3,
  "rowId": "order_abc123",
  "columnName": "quantity",
  "oldValue": 5,
  "newValue": 7,
  "details": {
    "ipAddress": "192.168.1.100",
    "browser": "Chrome 118"
  }
}
登录后复制
// 简单的前端日志发送示例
function sendLogToServer(logData) {
    fetch('/api/table-logs', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            // 'Authorization': 'Bearer YOUR_TOKEN' // 如果需要认证
        },
        body: JSON.stringify(logData)
    })
    .then(response => {
        if (!response.ok) {
            console.error('Failed to send log:', response.statusText);
            // 可以在这里实现重试机制或客户端Fallback
        }
    })
    .catch(error => {
        console.error('Error sending log:', error);
        // 网络错误等,同样可以考虑重试或Fallback
    });
}

// 假设有一个可编辑的表格
document.getElementById('myDataTable').addEventListener('blur', function(event) {
    const target = event.target;
    // 确保是可编辑的单元格或其内的input
    if (target.matches('td[contenteditable="true"]') || target.matches('td input, td textarea')) {
        const row = target.closest('tr');
        const table = target.closest('table');

        // 假设我们能获取旧值(可能在focus时保存)
        const oldValue = target.dataset.oldValue || ''; 
        const newValue = target.innerText || target.value;

        if (oldValue !== newValue) { // 只有值发生变化才记录
            const logEntry = {
                timestamp: new Date().toISOString(),
                userId: 'someUser123', // 从用户会话中获取
                tableId: table ? table.id : 'unknown_table',
                actionType: 'cell_edit',
                rowIndex: row ? row.rowIndex : -1,
                columnName: target.dataset.columnName || 'unknown_column', // 使用data-属性标识列
                oldValue: oldValue,
                newValue: newValue
            };
            sendLogToServer(logEntry);
        }
        // 清除旧值,或在focus时设置新的oldValue
        target.dataset.oldValue = newValue; 
    }
}, true); // 使用捕获阶段,确保事件能被表格容器捕获

// 在单元格获得焦点时保存旧值
document.getElementById('myDataTable').addEventListener('focus', function(event) {
    const target = event.target;
    if (target.matches('td[contenteditable="true"]') || target.matches('td input, td textarea')) {
        target.dataset.oldValue = target.innerText || target.value;
    }
}, true);
登录后复制

以上就是如何为HTML表格添加日志记录?有哪些实现方法?的详细内容,更多请关注php中文网其它相关文章!

HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号