2016-08-05 48 views
0

我有表獲得所有項目的ID在數據表表

<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在我的骨幹Initialize功能我設置爲我的集合的引用,其調用API從數據庫中獲取數據。在這個特定的實例中,它獲取了staff表中的所有行。

this.staff = options.staff; 

在我的骨幹render功能我有用於填充數據表的表下面的代碼:

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 
    }] 
}); 

在一個新的功能,我希望能夠得到每個項目的IndexlistTemplateTbody。任何幫助將非常感激。

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

相關問題