从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);
});

 

posted @ 2017-07-21 09:32  默语  阅读(10246)  评论(0编辑  收藏  举报