문제

jQuery를 사용하여 테이블 내의 TR 요소 수를 어떻게 계산합니까?

나는 있다는 것을 알고있다 비슷한 질문, 그러나 나는 단지 총 행을 원합니다.

도움이 되었습니까?

해결책

모든 행을 선택하고 길이를 선택하는 선택기를 사용하십시오.

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

참고 :이 접근법은 또한 모든 중첩 테이블의 모든 TR을 계산합니다!

다른 팁

사용하는 경우 <tbody> 또는 <tfoot> 테이블에서 다음 구문을 사용해야하거나 잘못된 값을 얻을 수 있습니다.

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

또는 ...

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

JSFIDDLE 데모

이렇게하면 중첩 된 테이블 줄도 계산되지 않습니다.

글쎄, 나는 테이블에서 아트 행을 얻고 그 컬렉션의 길이를 얻습니다.

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

jQuery가 덜 작동한다고 생각합니다.

여기에 내 테이크가 있습니다.

//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;
};

용법:

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

나는 다음을 얻었다 :

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

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>

분명히 이것은 빠른 예이지만 도움이 될 수 있습니다.

테이블 내부 테이블에서 th를 세지 않고 행을 세고 싶다면 이것이 정말로 잘 작동한다는 것을 알았습니다.

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

tbody가 있다면 이것을 시도하십시오

헤더없이

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

헤더가 있다면

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

즐기다!!!

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

var trLength = jQuery('#tablebodyID >tr').length;

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top