如何在Jquery datatable中选择一行 [英] How to select a row in Jquery datatable
问题描述
我在我的应用程序中使用了数据表。每当用户点击任何行时,我想突出显示它,并从选定的行中选择一些值。
I am using datatables in my application. Whenever user click on any row I want to highlight it and pick some values from selected row.
"oTableTools": {
"sRowSelect": "single",
"fnRowSelected": function ( node ) {
var s=$(node).children();
alert("Selected Row : " + $s[0]);
}
我尝试过 sRowSelect
和 fnRowSelected
但没有运气。该行不突出显示,并且 fnRowSelected
不被调用。即使没有控制台上的错误。
I tried sRowSelect
and fnRowSelected
but no luck. The row is not highlighted and neither fnRowSelected
is called. Even no error on console.
这是我的完整代码
var userTable = $('#users').dataTable({
"bPaginate": true,
"bScrollCollapse": true,
"iDisplayLength": 10,
"bFilter": false,
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"oLanguage": {
"sLengthMenu": "Display _MENU_ records per page",
"sZeroRecords": "Enter a string and click on search",
"sInfo": "Showing _START_ to _END_ of _TOTAL_ results",
"sInfoEmpty": "Showing 0 to 0 of 0 results",
"sInfoFiltered": "(filtered from _MAX_ total results)"
},
"aaSorting": [[ 0, "asc" ]],
"aoColumns": [/* Name */ null,
/*Institution*/null,
/*Email*/null],
"oTableTools": {
"sRowSelect": "single",
"fnRowSelected": function ( node ) {
alert("Clicked");
}
}
});
我没有任何东西吗?
编辑:
现在可以突出显示所选的row.Added class =display到HTML表。仍然想知道为什么我没有在datatable文档中找到这个。现在看看如何收集选定的值。
Now able to highlight selected row.Added class="display" to HTML table. Still wondering why I didn't find this in datatable docs. Now looking how to collect selected values.
推荐答案
这是我怎么做
只需添加此功能到你的页面(如果用户是你的表格id)
just add this function to your page (if users is your table id)
$("#users tbody").delegate("tr", "click", function() {
var iPos = userTable.fnGetPosition( this );
if(iPos!=null){
//couple of example on what can be done with the clicked row...
var aData = userTable.fnGetData( iPos );//get data of the clicked row
var iId = aData[1];//get column data of the row
userTable.fnDeleteRow(iPos);//delete row
}
这篇关于如何在Jquery datatable中选择一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!