javascript 如何从javascript中的数据表获取特定列值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17521144/
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
How to get specific column value from Datatables in javascript
提问by roger_that
I have a datatable populated with aaData
. There is this last column which is action
that again is populated with an array.
我有一个填充了aaData
. 最后一列action
再次填充了一个数组。
What i need to do is, by clicking an action link, I should pick up the column array value for that specific row where i clicked the action.
我需要做的是,通过单击操作链接,我应该为我单击操作的特定行选择列数组值。
The action column is populated with below json format
操作列填充了以下 json 格式
[
{
"displayValue":"Browse",
"link":"svnpath/BacklogApp",
"displayIcon" : "browselogo"
},
{
"displayValue":"Source Code",
"link":"svnpath/BackLog/trunk/webapp-project/",
"displayIcon" : "svnlogo"
},
{
"displayValue":"Contributors: Vaibhav",
"link":"#contributors",
"displayIcon" : "people"
}
]
This is my action column which has three links - browse, source code and contributor. When i click the contributor icon, i should be able to get "Contributors: Vaibhav"
this string.
这是我的操作栏,它有三个链接——浏览、源代码和贡献者。当我点击贡献者图标时,我应该能够得到"Contributors: Vaibhav"
这个字符串。
Below is the JS code
下面是JS代码
$(document).on('click', '.contributor', function(e)
var aPos = tableAADataForContributors.fnGetPosition(this);
alert(aPos);
//if aPos returns an array in console, use first val at pos zero to get row data
var aData = tableAADataForContributors.fnGetData(aPos[0]);
alert(aData);
But on alert, my aPosis coming to be null whereas tableAADataForContributorsis not null.
但是提醒一下,我的aPos将变为空,而tableAADataForContributors不为空。
Also, this is how I am populating the datatable
另外,这就是我填充数据表的方式
$.ajax({
type: "GET",
url: url,
dataType: "json",
cache: false,
contentType: "application/json",
success: function(tablePropJSONData) {
var oTable = $('#genericTable').dataTable( {
"bProcessing": true,
"aaData": tableObj,
"sPaginationType" : "full_numbers",
"bJQueryUI" : true,
"bRetrieve" : true,
"bPaginate" : true,
"bSort" : true,
"aaSorting" : [[ 2, "desc" ]],
"iDisplayLength" : 50,
"bAutoWidth" : false,
"bStateSave" : false,
"aoColumns" : tablePropJSONData.aoColumns,
"fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
var lastColumnIndex = aData.length-1;
var actionColumnContent = renderActionColumn(aData[lastColumnIndex]);
$('td:eq('+lastColumnIndex+')', nRow).html(actionColumnContent);
}
});
tableAADataForContributors = oTable; // initializing for picking up contributors from datatable.
searchDataTable(oTable, searchTerm);
},
And likewise initializing a global variable tableAADataForContributorsto be able to use it in onclick event later.
同样初始化一个全局变量tableAADataForContributors以便稍后在 onclick 事件中使用它。
I am not able to do so. Please someone suggest a javascript code.
我不能这样做。请有人建议一个javascript代码。
采纳答案by roger_that
Thankfully, I was able to solve it myself. Just a little bit of here and there was needed. Below code does the requirement
谢天谢地,我能够自己解决它。只需要一点点。下面的代码满足要求
$(document).on('click', '.contributor', function(e){
var aPos = tableAADataForContributors.fnGetPosition( $(this).closest('tr')[0]);
//if aPos returns an array in console, use first val at pos zero to get row data
var aData = tableAADataForContributors.fnGetData(aPos);
var actionColumnData = aData[aData.length-1];
$.each(actionColumnData, function(i, value){
alert(value.displayValue)
});