jQuery 数据表无需点击即可获取行数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13826548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Datatables get row Data without clicking on it
提问by Silverwulf
I'm using DataTables to show a list of messages.
我正在使用 DataTables 来显示消息列表。
I need a way to get a specific row of data based off an id being passed to the page on the Query string.
我需要一种方法来根据传递给查询字符串页面的 id 来获取特定行的数据。
ex. www.webpage.com?id=2
前任。www.webpage.com?id=2
I already have the id in a jQuery variable. Now I just need to access the DataTable row associated with that id.
我已经在 jQuery 变量中拥有了 id。现在我只需要访问与该 ID 关联的 DataTable 行。
Basically, I need to reference the row, without having clicked on it.
基本上,我需要引用该行,而无需单击它。
Any suggestions?
有什么建议?
回答by Daniel
One way could be to use fnGetPosition
and fnGetData
一种方法可能是使用fnGetPosition
和fnGetData
var rowIndex = table.fnGetPosition( $("some selector").closest('tr')[0] );
//some selector = should select some hidden element inside a row that
//contains the relevant id
var aData = table.fnGetData( rowIndex );
alert(aData[0]);// will show first column data
Here a workingjsfiddle exampleof an external button that selects row with specific ID
这是选择具有特定 ID 的行的外部按钮的工作jsfiddle 示例
Another example that select the row with specific ID on page load(ready)jsfiddle example N#2
在页面加载(就绪)jsfiddle 示例 N#2上选择具有特定 ID 的行的另一个示例
Take a look at the function
看看功能
$("#wow").click(function() {
var rowIndex = table.fnGetPosition($("#example input[value=\'TridentVal\']").closest('tr')[0]);
alert(rowIndex);
var aData = table.fnGetData(rowIndex);
alert(aData[0]); // will show first column data
});?
This is the way to select an input with relevant data... :
这是选择具有相关数据的输入的方法...:
$("#example input[value=\'TridentVal\']")
example
is table id , replace TridentValwith the needed ID
example
是表 id ,用需要的 ID替换TridentVal
回答by Ricardo Alvaro Lohmann
First you have to get your table row using jQuery.
首先,您必须使用 jQuery 获取表格行。
var $rowNode = $('#myTable').find('tbody tr:eq(0)').get(0);
Then you use fnGetData
to get the row data.
然后您使用fnGetData
来获取行数据。
var data = table.fnGetData($rowNode);
fnGetData
acccepts a row node or an integer, so you can pass the row index as parameter too.
fnGetData
接受行节点或整数,因此您也可以将行索引作为参数传递。
回答by dnagirl
If you use the TableTools extension, then you can use fnSelect(). From the docs:
如果您使用 TableTools 扩展,那么您可以使用 fnSelect()。从文档:
$(document).ready( function () {
$('#example1').dataTable( {
"sDom": 'T<"clear">lfrtip',
"oTableTools": {
"sRowSelect": "single"
}
} );
// Select the first row in the table automatically
var oTT = TableTools.fnGetInstance( 'example1' );
oTT.fnSelect( $('#example tbody tr')[0] );
});
You'd of course want to modify the selector so it chooses the row that has your id.
您当然希望修改选择器,以便它选择具有您 id 的行。
回答by xeo
fnGetData is now obsolete. this documents the cell().data() access pattern in the current version https://datatables.net/reference/api/cell().data()
fnGetData 现在已过时。这记录了当前版本https://datatables.net/reference/api/cell().data() 中的 cell().data() 访问模式