获取 Datatables table 中所有项目的 ID

Get ID of all items in Datatables table

我有以下HTML的table

<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>

在我的 backbone Initialize 函数中,我设置了对我的集合的引用,它调用 API 从数据库中获取数据。在此特定实例中,它正在获取工作人员 table.

中的所有行
this.staff = options.staff;

在我的 backbone render 函数中,我有以下代码用于填充 Datatables table:

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。任何帮助将非常感激。

createCourseTpl: function(e) {
   //get id of each item here?
}
$('#trainerListTable').each(function(index, domobj) {
    //your logic
});

说明:

您 select 您的 ID trainerListTable 并遍历子项,其中所述索引为您提供迭代次数以及来自父项的元素的索引,例如 domobj 是子项本身对于索引 0,thead 将是 dom 元素,依此类推,因此您可以编写逻辑来操作哪些元素,哪些不操作。