HTML:表格与JSON

本文介绍了一种改进的方法,将HTML表格数据转换为JSON格式,以便更好地支持数据定制功能。这种方法不仅提高了数据的可访问性,还减少了数据大小。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I am updating a tool that allows users to customize a large data table to their needs. It's sort of like a progressive insurance for seminaries in that it helps students compare information on about 30 US seminaries so they can make an informed decision about where to go.

My initial approach when I built it 4-5 years ago was to embed JSON data in the HTML page and then use JavaScript to convert that JSON data into a table based on the user's choices.

But there are a few problems with this approach.

    The data is not accessible to screen readers.
    The data is not accessible to those without JavaScript
    Embedding a lot of JSON data in an HTML page isn't a very good idea. It should be a separate cached .js file, but I can't do that since the data often changes and it still wouldn't address #1 and #2

So I've decided in the new version to start with a large <table> and then turn that table into JSON data using the <thead> region as the JSON labels. Then I can take that data and customize it based on the user's choices. Interestingly the overall data size is a bit smaller since writing a <td> and </td> for each peice of information is actually smaller than writing the header name and quotes over and over.

Here's the simple table to JSON code. It assumes the first row is has header names and removes spaces and lower cases it.

function tableToJson(table) {
    var data = [];
    
    // first row needs to be headers
    var headers = [];
    for (var i=0; i<table.rows[0].cells.length; i++) {
        headers[i] = table.rows[0].cells[i].innerHTML.toLowerCase().replace(/ /gi,'');
    }
    
    // go through cells
    for (var i=1; i<table.rows.length; i++) {
        
        var tableRow = table.rows[i];
        var rowData = {};
        
        for (var j=0; j<tableRow.cells.length; j++) {
            
            rowData[ headers[j] ] = tableRow.cells[j].innerHTML;
        
        }
        
        data.push(rowData);
    }        
    
    return data;
}
http://www.zznews.cn/jk/bjhhyy/yygk/5983620492.html
http://www.zznews.cn/jk/bjhhyy/yygk/5983168860.html
http://www.zznews.cn/jk/bjhhyy/yygk/5983587831.html
http://www.zznews.cn/jk/bjhhyy/zqzz/5982786689.html
http://www.zznews.cn/jk/bjhhyy/zqzz/5982595838.html
http://www.zznews.cn/jk/bjhhyy/zqzz/5982557166.html
http://www.zznews.cn/jk/bjhhyy/zqzz/5982802458.html
http://www.zznews.cn/jk/bjhhyy/zlff/5982890179.html
http://www.zznews.cn/jk/bjhhyy/zlff/5982880536.html
http://www.zznews.cn/jk/bjhhyy/zlff/5982876194.html
http://www.zznews.cn/jk/bjhhyy/zlff/5982865981.html
http://www.zznews.cn/jk/bjhhyy/nzym/5982923191.html
http://www.zznews.cn/jk/bjhhyy/nzym/5982929592.html
http://www.zznews.cn/jk/bjhhyy/nzym/5982937900.html
http://www.zznews.cn/jk/bjhhyy/zzbyy/5982953795.html
http://www.zznews.cn/jk/bjhhyy/zzbyy/5982961689.html
http://www.zznews.cn/jk/bjhhyy/zzbyy/5982967439.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值