2016-08-05 2 views
0

모든 항목의 ID를 가져옵니다 내 백본 Initialize 함수에서 테이블Datatables 테이블

<div role="tabpanel" class="tab-pane fade col-sm-12" id="trainerList" style="max-width: 1000px;"> 
    <table id="trainerListTable" class="table table-condensed table-striped" style="width: 100%;"> 
     <thead> 
      <tr> 
       <th></th> 
       <th></th> 
       <th>Staff ID</th> 
       <th>Trainer</th> 
       <th>Team</th> 
       <th>Department</th> 
      </tr> 
     </thead> 
     <tbody id="listTemplateTbody"></tbody> 
    </table> 
</div> 

다음과 같은 HTML을 나는 데이터베이스에서 데이터를 얻을 수있는 API를 호출 내 컬렉션에 대한 참조를 설정합니다. 이 특정 인스턴스에서는 스태프 테이블의 모든 행을 가져옵니다.

this.ui.listTemplateTbody.empty(); 

this.staff.each(function(template) { 
    var profileImg = template.get('PictureUri') || "/images/placeholderUser.png"; 
    self.ui.listTemplateTbody.append(self.listTabletpl({ 
     Index: template.get('Index'), 
     StaffId: template.get('Id'), 
     Forename: template.get('forename'), 
     Surname: template.get('surname'), 
     Team: template.get('Team') || "No Team", 
     Department: template.get('DepartmentId') || "No Department", 
     Image: '<img class="createImageTableIcon" src="' + profileImg + '" />' 
    })); 
}); 

this.staffTable = $('#trainerListTable').DataTable({ 
    "aLengthMenu": [ 
     [5, 10, 25, 50, -1], 
     [5, 10, 25, 50, "all"] 
    ], 
    "iDisplayLength": 10, 
    "order": [ 
     [2, "DESC"] 
    ], 
    "columnDefs": [{ 
     "orderable": false, 
     "targets": [0, 1] 
    }, { 
     "width": "5%", 
     "targets": 0 
    }, { 
     "width": "5%", 
     "targets": 1 
    }, { 
     "width": "10%", 
     "targets": 2 
    }, { 
     "width": "40%", 
     "targets": 3 
    }, { 
     "width": "20%", 
     "targets": 4 
    }, { 
     "width": "20%", 
     "targets": 5 
    }] 
}); 

내가 listTemplateTbody에서 각 항목의 Index를 얻을 수 있기를 원하는 새로운 기능에서 내 백본 render 기능에

this.staff = options.staff; 

은 내가 Datatables 테이블을 채우기위한 다음과 같은 코드가 있습니다. 어떤 도움이라도 대단히 감사 할 것입니다.

createCourseTpl: function(e) { 
    //get id of each item here? 
} 

답변

2
$('#trainerListTable').each(function(index, domobj) { 
    //your logic 
}); 

설명 :

당신은 당신의 ID trainerListTable을 선택하고 명시된 인덱스로도 부모와 domobj에서 요소의 인덱스를 당신에게 반복의 수를 제공 어디서 아이들을 반복 아이 자체가 예를 들어 인덱스 0에 대해 thead는 dom 요소가 될 것이고 그래서 당신은 당신의 논리를 조작 할 요소와 작성할 요소를 작성할 수 있습니다.

+0

이 코드 단편은 질문을 해결할 수 있지만 [// 설명 포함] (// meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) 정말 게시물의 품질을 향상시키는 데 도움이됩니다. 앞으로 독자의 질문에 답하고 있으며 코드 제안의 이유를 알지 못할 수도 있습니다. 코드와 설명의 가독성을 떨어 뜨리기 때문에 주석을 설명하는 코드를 사용하지 마십시오! – FrankerZ

+0

설명이 추가되었습니다. – SiddP

관련 문제