通过单击动态创建的按钮访问 jQuery 中的数据表内容
Access Datatable content in jQuery with dynamically created button click
我是 jQuery 的新手。当我按下擦除按钮时,我需要访问数据表内容。我尝试了很多方法,但它 returns 未定义。
$(document).ready(function() {
loadData()
});
function loadData() {
var table = $('#example').DataTable({
"ajax" : {
"method" : 'POST',
"crossDomain" : true,
"dataType" : 'json',
"contentType" : 'application/x-www-form-urlencoded; charset=UTF-8',
"dataSrc" : "",
"url" : "http://localhost:8081/Lakshmi_Service/admin/full"
},
"columns" : [ {
"data" : "adminName"
}, {
"data" : "address"
}, {
"data" : "emailId"
}, {
"data" : "otp"
}, {
"data" : "expiryDate"
}, {
"data" : "mobileNo"
}, {
"targets" : -1,
"data" : null,
"defaultContent" : '<button>Erase</button>'
} ],
"iDisplayLength" : 5,
"bAutoWidth" : true,
"bSort" : false,
"aLengthMenu" : [ [ 10, 25, 50, -1 ], [ 10, 25, 50, "All" ] ],
"bDestroy" : true,
"bFilter" : false,
"bLengthChange" : false
});
$('#example tbody').on('click', 'button', function(event) {
var aData = table.fnGetPosition(this);
var oTableData = table.fnGetData(aData[0]);
var ids = oTableData[aData].adminName;
alert(ids);
});
}
当我点击擦除按钮调用另一个服务时,我需要这个 adminName
值。我的页面看起来像:
我的 html 页面是:
<!DOCTYPE html>
<html>
<head>
<script src="jquery-1.11.3.js"></script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script type="text/javascript" language="javascript"
src="https://cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" language="javascript"
src="https://cdn.datatables.net/1.10.9/js/dataTables.bootstrap.min.js"></script>
<script src="jquery-ui.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css"
href="https://cdn.datatables.net/1.10.9/css/jquery.dataTables.min.css">
<link rel="stylesheet" type="text/css"
href="https://cdn.datatables.net/1.10.9/css/dataTables.bootstrap.min.css">
<title>Guru Statistics</title>
</head>
<body>
<table id="example" class="table table-striped table-bordered"
cellspacing="0" width="60%">
<thead>
<tr title="Name">
<th>Name</th>
<th>address</th>
<th>mailId</th>
<th>otp</th>
<th>Date</th>
<th>Mobile No</th>
<th>Erase</th>
</tr>
</thead>
</table>
<script type="text/javascript" src="home.js"></script>
</body>
</html>
您可以将 类 添加到 "columns"(我使用 aoColumns)dataTables 对象中的单元格(adminName、address...),然后使用 jQuery 在你的 click
事件中
$(this).parents("tr").find(".adminName").text();
这样你应该能够得到你点击的行的adminName的值
如果您不想添加 类(您应该添加!),我想您仍然可以通过这种方式获取数据:
$(this).parents("tr").find("td").first().text();
但我不鼓励你这样做,就好像有一天你决定在 adminName 之前添加一个新列(比如 "Id")你的代码将检索 Id
而不是adminName
,这可能会破坏您的代码。
我是 jQuery 的新手。当我按下擦除按钮时,我需要访问数据表内容。我尝试了很多方法,但它 returns 未定义。
$(document).ready(function() {
loadData()
});
function loadData() {
var table = $('#example').DataTable({
"ajax" : {
"method" : 'POST',
"crossDomain" : true,
"dataType" : 'json',
"contentType" : 'application/x-www-form-urlencoded; charset=UTF-8',
"dataSrc" : "",
"url" : "http://localhost:8081/Lakshmi_Service/admin/full"
},
"columns" : [ {
"data" : "adminName"
}, {
"data" : "address"
}, {
"data" : "emailId"
}, {
"data" : "otp"
}, {
"data" : "expiryDate"
}, {
"data" : "mobileNo"
}, {
"targets" : -1,
"data" : null,
"defaultContent" : '<button>Erase</button>'
} ],
"iDisplayLength" : 5,
"bAutoWidth" : true,
"bSort" : false,
"aLengthMenu" : [ [ 10, 25, 50, -1 ], [ 10, 25, 50, "All" ] ],
"bDestroy" : true,
"bFilter" : false,
"bLengthChange" : false
});
$('#example tbody').on('click', 'button', function(event) {
var aData = table.fnGetPosition(this);
var oTableData = table.fnGetData(aData[0]);
var ids = oTableData[aData].adminName;
alert(ids);
});
}
当我点击擦除按钮调用另一个服务时,我需要这个 adminName
值。我的页面看起来像:
我的 html 页面是:
<!DOCTYPE html>
<html>
<head>
<script src="jquery-1.11.3.js"></script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script type="text/javascript" language="javascript"
src="https://cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" language="javascript"
src="https://cdn.datatables.net/1.10.9/js/dataTables.bootstrap.min.js"></script>
<script src="jquery-ui.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css"
href="https://cdn.datatables.net/1.10.9/css/jquery.dataTables.min.css">
<link rel="stylesheet" type="text/css"
href="https://cdn.datatables.net/1.10.9/css/dataTables.bootstrap.min.css">
<title>Guru Statistics</title>
</head>
<body>
<table id="example" class="table table-striped table-bordered"
cellspacing="0" width="60%">
<thead>
<tr title="Name">
<th>Name</th>
<th>address</th>
<th>mailId</th>
<th>otp</th>
<th>Date</th>
<th>Mobile No</th>
<th>Erase</th>
</tr>
</thead>
</table>
<script type="text/javascript" src="home.js"></script>
</body>
</html>
您可以将 类 添加到 "columns"(我使用 aoColumns)dataTables 对象中的单元格(adminName、address...),然后使用 jQuery 在你的 click
事件中
$(this).parents("tr").find(".adminName").text();
这样你应该能够得到你点击的行的adminName的值
如果您不想添加 类(您应该添加!),我想您仍然可以通过这种方式获取数据:
$(this).parents("tr").find("td").first().text();
但我不鼓励你这样做,就好像有一天你决定在 adminName 之前添加一个新列(比如 "Id")你的代码将检索 Id
而不是adminName
,这可能会破坏您的代码。