2013-03-22 4 views
1

나는 datatables.net을 가지고 놀고 있었고 이제 테이블은 json 객체 배열에서 데이터를로드합니다. 이제는 누군가가 라인을 클릭하면 대화 상자를 열 수 있도록 이러한 객체 중 하나의 ID (단지 속성)를 가져옵니다.클릭하면 DataTables 행 선택

fnRowCallback을 사용하여 ID를 TR 요소의 특성으로로드하는 방법을 찾았지만 행이 정렬 될 때 중단됩니다.

var tableData = [ 
      { id: 196402, name: "Joe Bloggs", age: 25, gender: "Male"}, 
      { id: 257820, name: "Jane Bloggs", age: 22, gender: "Female"}, 
      { id: 33025, name: "Sam Smith", age: 27, gender: "Female"} 
     ]; 


oTable = $('#MyTable').dataTable({ "aaData": tableData, 
      "aoColumns": 
       [ 
        {"mData": "name"}, 
        {"mData": "age"}, 
        {"mData": "gender"} 
       ], 
      "fnRowCallback": function(nRow, aData, iDisplayIndex, iDisplayIndexFull) { 
       $(nRow).attr('id', aData.id); // this breaks if the grid is sorted! 
      } 
      }); 

      $("#MyTable tbody tr").click(function(e) {    
       alert($(this).attr('id')); 
      }); 

더 좋은 방법이 있습니까?

답변

2

에서 : http://www.datatables.net/api

$('#example tbody td').click(function() { 
    // Get the position of the current data from the node 
    var aPos = oTable.fnGetPosition(this); 

    // Get the data array for this row 
    var aData = oTable.fnGetData(aPos[0]); 

    // Update the data array and return the value 
    aData[ aPos[1] ] = 'clicked'; 
    this.innerHTML = 'clicked'; 
    }); 

내가 datatables를 사랑하지만 그들을 학습 동안 지출 정말 가치가 ... 나는 아직도 도움이 TBH ;-)

희망을 갈 길이 멀다있어 .

+0

정확히 내가 필요한 것 - 감사합니다. – Liath