• 从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);
    });
  • 相关阅读:
    SQL的介绍及MySQL的安装
    git中级技能
    git基本用法
    git基本语法
    出租车数据分析
    使用Spark MLlib进行情感分析
    增量式编码器专题
    vue-loader的简单例子
    node爬虫(转)
    fs-extra 文件管理
  • 原文地址:https://www.cnblogs.com/syfwhu/p/7216056.html
Copyright © 2020-2023  润新知