admin管理员组

文章数量:1402299

I have multiple tables in a dOM tree and I want to calculate the count of cells in every table.

code:

function tree() {
    var table = document.getElementsByTagName('table');
    for (var p = 0, tr; tr= table[p]; p++) {
      for (var i =0, row; row = tr.rows[i]; i++) {
         console.log(row.cells.length)
      }
    }
}

console.log(tree())

html:

<table>
   <tr>
            <td>First</td>
            <td>row</td>
        </tr>
        <tr>
            <td>and</td>
            <td>second</td>
        </tr>
</table>
<table>
   <tr>
            <td>First</td>
            <td>row</td>
        </tr>
</table>

In the first there are 4 cells and second there are 2 cells I want to print out the cells with largest count in a table. Hence the o/p will be:

4

my above js code, returns the total length of all the cells in a table. How can I traverse through the tables in a tree and find the largest count of cells?

thx!

I have multiple tables in a dOM tree and I want to calculate the count of cells in every table.

code:

function tree() {
    var table = document.getElementsByTagName('table');
    for (var p = 0, tr; tr= table[p]; p++) {
      for (var i =0, row; row = tr.rows[i]; i++) {
         console.log(row.cells.length)
      }
    }
}

console.log(tree())

html:

<table>
   <tr>
            <td>First</td>
            <td>row</td>
        </tr>
        <tr>
            <td>and</td>
            <td>second</td>
        </tr>
</table>
<table>
   <tr>
            <td>First</td>
            <td>row</td>
        </tr>
</table>

In the first there are 4 cells and second there are 2 cells I want to print out the cells with largest count in a table. Hence the o/p will be:

4

my above js code, returns the total length of all the cells in a table. How can I traverse through the tables in a tree and find the largest count of cells?

thx!

Share Improve this question asked May 7, 2019 at 17:01 user1234user1234 3,1596 gold badges57 silver badges117 bronze badges 2
  • Are any tables nested within another table? Do you want the count - in this case 4 - or a reference to the <table> that contains the largest number of <td> elements? – David Thomas Commented May 7, 2019 at 17:05
  • I just want the count of tds in every tbale and no there wont be nested tables – user1234 Commented May 7, 2019 at 17:07
Add a ment  | 

2 Answers 2

Reset to default 7

Use querySelectorAll() to simplify this.

The below code will output 4 as in your OP. It's a simple modification to get the table with the most cells, which I suspect is what you meant (it's unclear).

// Get your list of all tables in the document.
const tables = document.querySelectorAll('table');

// Map list of tables to list of number of <td> elements.
const lengths = 
  Array.from(tables)
    .map(table => table.querySelectorAll('td').length)

// Get the max of the list of lengths and log to console.
const max = Math.max(...lengths);
console.log(max);
<table>
  <tr>
    <td>First</td>
    <td>row</td>
   </tr>
   <tr>
     <td>and</td>
     <td>second</td>
  </tr>
</table>
<table>
  <tr>
    <td>First</td>
    <td>row</td>
  </tr>
</table>

You can try this to if you want :

const maxTdTable = ()=>{
  var object;
  var maxCol = 0;
  $('table').each(function(element){
    $(element).each(function(elem){
      max = $(elem).childNodes.length;
      maxCol = max>=maxCol ? max : maxCol ; 
    });
  });
  return {element : object,max : maxCol};
}

本文标签: javascriptHow to find the largest count of lttdgt39s in a tree having multiple tablesJSStack Overflow