• 从html页面中抽取table表格数据


    /**
     * [getDataFromTrElems 获取表格行元素数据]
     * @param  {[Object]} trElems [trs dom]
     * @param  {[String]} type    [type]
     * @return {[Array]}         
     */
    function getDataFromTrElems(trElems, type) {
    
        if (!trElems || !trElems.length) {
            return [];
        }
    
        var data = [];
        var cell = type == 'head' ? 'th' : 'td';
    
        for (var i = 0, n = trElems.length; i < n; i++) {
    
            var trElem = $(trElems[i]);
            var cellElems = trElem.children(cell);
            var trData = [];
    
            for (var j = 0, m = cellElems.length; j < m; j++) {
    
                cellElem = $(cellElems[j]);
                colspan = +cellElem.attr('colspan');
                if (!(colspan > 0)) {
                    colspan = 1;
                }
    
                rowspan = +cellElem.attr('rowspan');
                if (!(rowspan > 0)) {
                    rowspan = 1;
                }
    
                text = $.trim(cellElem.text());
                trData.push({
                    colspan: colspan,
                    rowspan: rowspan,
                    text: text
                });
            }
    
            data.push(trData);
        }
    
        return data;
    }
    /**
     * [getDataFromHead 获取表格head数据]
     * @param  {[Object]} elem [head dom]
     * @return {[Array]} 
     */
    function getDataFromHead(elem) {
    
        if (!elem || !elem.length) {
            return [];
        }
    
        var trElems = elem.children('tr');
        return getDataFromTrElems(trElems, 'head');
    }
    /**
     * [getDataFromBody 获取表格body数据]
     * @param  {[Object]} elem [body dom]
     * @return {[Array]}      
     */
    function getDataFromBody(elem) {
    
        if (!elem || !elem.length) {
            return [];
        }
    
        var trElems = elem.children('tr');
        return getDataFromTrElems(trElems, 'body');
    }
    /**
     * [getDataFromTbaleElem 获取表格元素数据]
     * @param  {[Object]} elem [table dom]
     * @return {[Array]}         
     */
    function getDataFromTbaleElem(elem) {
    
        if (!elem || !elem.length) {
            return [];
        }
    
        var headElem = elem.children('thead');
        var bodyElem = elem.children('tbody');
        var headData = getDataFromHead(headElem);
        var bodyData = getDataFromBody(bodyElem);
        var rawData = headData.concat(bodyData);
    
        return formRawData(rawData);
    }
    /**
     * [formRawData 格式化表格初始数据]
     * @param  {[Array]} rawData [raw data]
     * @return {[Array]}         
     */
    function formRawData(rawData) {
    
        var data = [];
    
        rawData.forEach(function(trData, i) {
    
            data[i] = data[i] && data[i].length ? data[i] : [];
            var colIndex = -1;
    
            trData.forEach(function(cellData, j) {
    
                var text = cellData.text;
    
                for (var c = 0; c < cellData.colspan; c++) {
                    ++colIndex;
    
                    if (typeof data[i][colIndex] != 'undefined') {
                        ++colIndex;
                    }
    
                    data[i][colIndex] = text;
    
                    for (var r = 2; r <= cellData.rowspan; r++) {
                        var rowIndex = i + r - 1;
                        rawData[rowIndex].splice(colIndex, 0, {
                            text: text,
                            colspan: 1,
                            rowspan: 1
                        });
                    }
                }
            });
        });
    
        return data;
    }
    
    $(document).ready(function() {
        var start = new Date();
        var tableElems = $('table');
        var results = {};
        for (var index = 0, len = tableElems.length; index < len; index++) {
    
            var tableElem = $(tableElems[index]);
            results[index] = getDataFromTbaleElem(tableElem);
        }
        var end = new Date();
        console.log(results, end - start);
    });
  • 相关阅读:
    Java中Runnable和Thread的区别
    JAVA Swing 事件监听
    java 监听机制模拟(JButton按钮监听机制)
    java事件处理机制(自定义事件)
    oracle之检查点(Checkpoint)
    linux内核值shmmax问题
    如何在VMware虚拟机间建立共享磁盘?
    Mysql 不同版本 说明
    mysql 概念和逻辑架构
    mysql 在大型应用中的架构演变
  • 原文地址:https://www.cnblogs.com/syfwhu/p/7216056.html
Copyright © 2020-2023  润新知