jQuery:计算表中的行数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1149958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:46:25  来源:igfitidea点击:

jQuery: count number of rows in a table

jquerycountrow

提问by tvanfosson

How do I count the number of tr elements within a table using jQuery?

如何使用 jQuery 计算表中 tr 元素的数量?

I know there is a similar question, but I just want the total rows.

我知道有一个类似的问题,但我只想要总行数。

回答by tvanfosson

Use a selector that will select all the rows and take the length.

使用将选择所有行并取长度的选择器。

var rowCount = $('#myTable tr').length;

Note: this approach also counts all trs of every nested table!

注意:这种方法还计算每个嵌套表的所有 trs!

回答by James Moberg

If you use <tbody>or <tfoot>in your table, you'll have to use the following syntax or you'll get a incorrect value:

如果在表中使用<tbody><tfoot>,则必须使用以下语法,否则会得到不正确的值:

var rowCount = $('#myTable >tbody >tr').length;

回答by DevlshOne

Alternatively...

或者...

var rowCount = $('table#myTable tr:last').index() + 1;

jsFiddle DEMO

jsFiddle 演示

This will ensure that any nested table-rows are not also counted.

这将确保任何嵌套的表行也不会被计算在内。

回答by jjroman

Well, I get the attr rows from the table and get the length for that collection:

好吧,我从表中获取 attr 行并获取该集合的长度:

$("#myTable").attr('rows').length;

I think that jQuery works less.

我认为 jQuery 工作较少。

回答by Ricky G

Here's my take on it:

这是我的看法:

//Helper function that gets a count of all the rows <TR> in a table body <TBODY>
$.fn.rowCount = function() {
    return $('tr', $(this).find('tbody')).length;
};

USAGE:

用法:

var rowCount = $('#productTypesTable').rowCount();

回答by chifliiiii

I got the following:

我得到以下信息:

jQuery('#tableId').find('tr').index();

回答by dennismonsewicz

I needed a way to do this in an AJAX return, so I wrote this piece:

我需要一种在 AJAX 返回中做到这一点的方法,所以我写了这篇文章:

<p id="num_results">Number of results: <span></span></p>

<div id="results"></div>

<script type="text/javascript">
$(function(){
    ajax();
})

//Function that makes Ajax call out to receive search results
var ajax = function() {
    //Setup Ajax
    $.ajax({
        url: '/path/to/url', //URL to load
        type: 'GET', //Type of Ajax call
        dataType: 'html', //Type of data to be expected on return
        success: function(data) { //Function that manipulates the returned AJAX'ed data
            $('#results').html(data); //Load the data into a HTML holder
            var $el = $('#results'); //jQuery Object that is holding the results
            setTimeout(function(){ //Custom callback function to count the number of results
                callBack($el);
            });
        }
    });
}

//Custom Callback function to return the number of results
var callBack = function(el) {
    var length = $('tr', $(el)).not('tr:first').length; //Count all TR DOM elements, except the first row (which contains the header information)
    $('#num_results span').text(length); //Write the counted results to the DOM
}
</script>

Obviously this is a quick example, but it may be helpful.

显然,这是一个简单的例子,但它可能会有所帮助。

回答by BornToCode

try this one if there is tbody

如果有 tbody 试试这个

Without Header

没有标题

$("#myTable > tbody").children.length

If there is header then

如果有标题那么

$("#myTable > tbody").children.length -1

Enjoy!!!

享受!!!

回答by Zoe

I found this to work really well if you want to count rows without counting the th and any rows from tables inside of tables:

如果您想计算行数而不计算表内表中的第 th 行和任何行,我发现这非常有效:

var rowCount = $("#tableData > tbody").children().length;

回答by Cybernetic

row_count =  $('#my_table').find('tr').length;
column_count =  $('#my_table').find('td').length / row_count;