2015-01-22 1 views
0

특정 클래스가있는 행에서 jquery 데이터 테이블의 데이터를 가져 오려고합니다.jquery 데이터 테이블의 모든 열을 반복하고 특정 클래스가있는 행에서 데이터를 가져옵니다.

내 테이블은 다음과 같습니다. 데이터 테이블에도 페이지 매김이 있습니다. 위의 코드에서

$('body').on('change', 'input.checked-box', function() { 

    if ($(this).prop('checked') == true) { 

     $(this).closest('tr').addClass('selectedRow'); 
    } 
    else { 
     $(this).closest('tr').removeClass('selectedRow'); 
    } 

    if (table.rows('.selectedRow').data().length > 0) { 

     $('#downloadDoc').removeClass('inactive'); 
     $('#downloadDoc').addClass('active'); 
    } 
    else { 
     $('#downloadDoc').removeClass('active'); 
     $('#downloadDoc').addClass('inactive'); 
    } 
}); 

:

ID | DocumentID | FileName |   | 
------------------------------------------ 
1 |   201| abc.cc | check box| 

제가 아래의 코드를 사용하여 일부 행에 클래스 추가 input.checked-box은 체크 박스 <input type='checkbox' class='checked-box' id='check0'>이며, 체크 박스는 특정 ID와 각 행에 추가 . 체크 박스를 클릭하면 해당 행에 class='selectedRow'이 표시됩니다. 따라서 위 코드는 해당 확인란을 선택하면 각 행에 클래스를 추가합니다. 지금까지 잘 작동합니다. 이제

, 나는 버튼을 클릭했을 때 class='selectedRow'

내 HTML (<button id='downloadDoc' class='active'> 말할)이있는 행을 (위의 표에서와 같이) "DocumentID"컬럼의 데이터를 얻을 수있는 방법 :

<div class="table-responsive"> 
    <button id='downloadDoc' class='inactive'> 

    <table id="attachmentList" class="display"> 
    <thead> 
     <tr> 
      <th></th> 
      <th>Attachment Detail Id</th> 
      <th>File Name</th> 
      <th></th> 
     </tr> 
    </thead> 
    <tbody></tbody> 
    </table> 
</div> 
+0

무엇 귀하의 마크 업에서 해당 버튼의 위치는 무엇입니까 ?? –

+0

@RajaprabhuAravindasamy와 동일한 'div'에있는 표가 있습니다. – Riki

답변

0

테이블 행을 반복하고 특정 클래스로 선택하십시오.

은이어서, 제 2 셀의 텍스트를 얻고 각 행의 제 2 셀을 대상 eq() 기능을 사용할 수

어레이 (arr)에 저장한다.

참고 : 구걸에서 알 수없는 경우, Attachment Detail Id 열의 인덱스, 당신은이 작업을 수행 할 수 있습니다 :

가 특정 컬럼에 ID를 추가하여 :

<tr> 
    <th></th> 
    <th id="att_id">Attachment Detail Id</th> 
    <th>File Name</th> 
    <th></th> 
</tr> 

찾기 특정 컬럼의 인덱스, avariable (i)에 저장하고 eq() 함수에 전달합니다

var arr = []; 
$('#attachmentList').find('tr').each(function() { 
    var that = $(this); 
    var i = $('#attachmentList').find('#att_id').index(); 
    var class = that.attr('class'); 
    if (class == 'selectedRow') { 
     arr.push(that.find('td').eq(i).text()); 
    } 
}); 
console.log(arr); 
+0

하지만 여전히 다음 페이지에서 데이터를 가져올 수 없습니다. – Riki

관련 문제