我遇到了这种情况,我正试图从数据表中获取特定行的数据。我是dataTable的新手。
bellow是我的数据表的代码
$(document).ready(function() {
var table =$('#example').dataTable({
"processing": true,
"ajax": {
"url":'/myWeb/Controller/getUserList',
"dataSrc":""
},
"columns": [
{ "data": "userName"},
{ "data": "roles" },
{
data: "userName",
render: function ( data, type, row ) {
if ( type === 'display' ) {
return '<button type="button" id='+data+' class="btn btn-default btn-sm" data-toggle="modal" data-target="#editUser" data-whatever="@getbootstrap()" onclick="getUserName(this.id)" > <span class="glyphicon glyphicon-pencil"></span> Edit </button>';
}
//alert(data);
return data;
}
},
{
data : "userName",
if ( type === 'display' ) {
return '<button type="button" id='+data+' class="btn btn-default btn-sm" onclick="countUserRecord(this.id)"> <span class="glyphicon glyphicon-trash"></span> Delete </button>';
}
//alert(data);
return data;
}
}
],
"paging": false,
"info": false,
"filter": false,
"sort": false
});
} );
&#13;
在我传递userName的删除按钮上,我也想传递角色, 所以我试过这个
role = row.roles;
userName = row.userName;
status = row.status;
alert("username :"+userName+" role: "+role);
&#13;
但是当我这样做时,每个用户都会扮演不同的角色。它给了我错误的用户角色。
贝洛是我的Json结构,我从DB获得。
[
{"userName":"admin",
"password":null,
"roles":"Admin",
"status":"Active",
"email":"emailAdmin"},
{"userName":"varun",
"password":null,
"roles":"User",
"status":"Active",
"email":"emailVarun"},
{"userName":"bob",
"password":null,
"roles":"Admin",
"status":"Active",
"email":"undefined"}
]
&#13;
我想要做的是当我点击删除用户按钮时,它应该传递userName和角色,我该怎么做,
答案 0 :(得分:0)
这应该可以解决问题:
$('table').on('click','.addDeleteClassToDeleteBtns',function(){
var containerRow = $(this).parents('tr');
var userName = containerRow.find('td:first-child').html();
var role = containerRow.find('td:nth-child(2)').html();
alert(role+' '+userName);
});
或
function countUserRecord(deleteId){
var containerRow = $('#deleteId').parents('tr');
var userName = containerRow.find('td:first-child').html();
var role = containerRow.find('td:nth-child(2)').html();
alert(role+' '+userName);
}
您可以编辑选择器以更好地适合您的代码。