[英]Get row id of a table row after selecting a row and clicking the button in datatables
我想要實現的是顯示一個表,在選擇一行后,可以編輯或刪除一行(進行適當的查詢),所以我需要一個我不想顯示的主鍵。 我的桌子看起來像這樣:
<table id='documents_d' class='display'>
<thead>
<tr><th>title 1</th><th>title 2</th><th>title 3</th>
</thead>
<tbody>
<tr data-id='1'>
<td>content</td>
<td>content</td>
<td>content</td>
<tr data-id='2'>
<td>content</td>
<td>content</td>
<td>content</td></tr>
</tbody>
</table>
我使用以下腳本來運行數據表:
<script>
$(document).ready(function () {
var documents_d = $('#documents_d').DataTable({
paging: true,
dom: 'Blfrtip',
colReorder: true,
select: {style: 'single'},
rowId: 'Writer',
buttons: [
'colvis',
'excelHtml5',
{
text: 'Edit',
action: function () {
alert(documents_d.row('.selected').data());
}
}
]
});
});
</script>
我想運行像 editDocument($docID) 這樣的函數而不是警報。 因此,首先我想選擇一行(這工作正常),然后單擊“編輯”后,我想獲取“數據 ID”並運行一個函數。 我試過:
documents_d.row('.selected').data("data-id")
但這會給我一個錯誤:
DataTables 警告:table id=documents_d - 為第 1 行第 7 列請求未知參數“7”。有關此錯誤的更多信息,請參閱http://datatables.net/tn/4
你可能想要這個:
$(documents_d.row('.selected').node()).data('id');
$()
包裝)然而,還有另一種方式:
https://datatables.net/reference/option/rowId
在 DataTable 中的每個
tr
元素上都有一個id
屬性用於行選擇和數據源標識通常很有用,尤其是在使用事件時。
然后你可以使用:
documents_d.row('.selected').id();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.