9

我想从 html 表中提取数据,例如

<table>
    <tr>
        <th> Header1 </th>
        <th> Header2 </th>
        <th> Header3 </th>
    </tr>
    <tr>
        <td> Value 1,1 </td>
        <td> Value 2,1 </td>
        <td> Value 3,1 </td>
    </tr>

    ... rows ...

</table>

并获取数组:

  • 标题的数组
  • 列值的二维数组(或每列的数组)

    如何使用 jQuery 做到这一点?

    我不关心序列化它,或者将它放入 JSON 对象,因为我想用它来呈现图表。


    相关的一般设计问题:

    目前我有类似的东西

    1. ajax query returns html table
    2. use jQuery to get values from html table
    3. render chart
    

    从 ajax 查询中返回一个 JSON 对象,然后从那里渲染一个表格和图表是否更有意义?

  • 4

    9 回答 9

    16

    demo updated http://jsfiddle.net/ish1301/cnsnk/

    var header = Array();
    
    $("table tr th").each(function(i, v){
            header[i] = $(this).text();
    })
    
    alert(header);
    
    var data = Array();
    
    $("table tr").each(function(i, v){
        data[i] = Array();
        $(this).children('td').each(function(ii, vv){
            data[i][ii] = $(this).text();
        }); 
    })
    
    alert(data);
    
    于 2010-12-03T21:34:29.407 回答
    15

    Something like this?

    $(function() {
    
      var headers = $("span",$("#tblVersions")).map(function() { 
        return this.innerHTML;
      }).get();
    
      var rows = $("tbody tr",$("#tblVersions")).map(function() { 
        return [$("td:eq(0) input:checkbox:checked",this).map(function() { 
          return this.innerHTML;     
        }).get()];
      }).get();
    
      alert(rows);
    });
    
    于 2010-12-03T21:31:27.470 回答
    3

    yet another way of doing it

    var headers = jQuery('th').map(function(i,e) { return e.innerHTML;}).get();
    var datas = []
    jQuery.each(jQuery('tr:gt(0)'), function(i,e ) {
       datas.push(jQuery('td', e).map(function(i,e) {
                                         return e.innerHTML; 
                                      }).get()
                 );
    });
    
    于 2010-12-03T21:55:07.500 回答
    1

    类似于以下内容:

    var thArray = new Array();
    var contentArray = new Array();
    
    $('th').each(function(index) {
      thArray[index] =    $(this).html();
    })
    
    
    $('tr').each(function(indexParent) {
      contentArray['row'+indexParent] = new Array();
        $(this).children().each(function(indexChild) {
          contentArray['row'+indexParent]['col'+indexChild] = $(this).html();
        });
    });
    

    This gives you two arrays, thArray which is an array of your headings and contentArray which is a 2d array containing rows and columns: contentArray['row1']['col0'] returns " Value 1,1"

    Actually, contentArray contains the th's as well... referenced 'row0'

    于 2010-12-03T21:30:18.673 回答
    1

    does it make more sense to throw a JSON object back from the ajax query and then render a table and a chart from there?

    Yes, absolutely. Return JSON in response to your AJAX request, then you can render the table using something like jQuery Templates and use the same underlying data to generate your chart as well.

    于 2010-12-03T21:41:05.533 回答
    1

    Here's a modification of Jerome Wagner's answer that uses recursive maps instead of a map inside an 'each':

    http://jsbin.com/oveva3/383/edit

      var headers = $("th",$("#meme")).map(function() { 
        return this.innerHTML;
      }).get();
    
      var rows = $("tbody tr",$("#meme")).map(function() { 
        return [$("td",this).map(function() { 
          return this.innerHTML;     
        }).get()];
      }).get();
    
    于 2013-01-22T01:29:11.520 回答
    1

    I'm tinkering with the same thing over here, but I prefer iterating through all tables and writing the header and body arrays into properties of each table, so here's my modification to the original answer:

    $(function() {
    $("table").each(function(){
      var $table = $(this),
          $headerCells = $("thead th", $(this)),
          $rows = $("tbody tr", $(this));
      var headers = [],
          rows = [];
    
    
    $headerCells.each(function(k,v) {
       headers[headers.length] = $(this).text();
      $table.prop("headAry", headers);
    });
    
    $rows.each(function(row,v) {
      $(this).find("td").each(function(cell,v) {
        if (typeof rows[cell] === 'undefined') rows[cell] = [];
        rows[cell][row] = $(this).text();
        $table.prop("bodAry", rows);
      });
    });
    console.log($(this).prop('headAry'));
    console.log($(this).prop('bodAry'));  
    });
    });
    

    JSbin

    于 2013-03-19T23:00:26.130 回答
    0

    我认为从 ajax 调用中获取一个 json 数组并从中生成表格/图表会更有意义。使用 jquery 模板,这一点都不难。

    于 2010-12-03T21:24:00.870 回答
    0

    Use this line of code:

    var arrays = [];
    $('table').eq(0).find('tr').each((r,row) => arrays.push($(row).find('td,th').map((c,cell) => $(cell).text()).toArray()))
    
    于 2020-01-08T12:54:33.243 回答