【发布时间】:2021-02-08 02:29:33
【问题描述】:
我创建了一个数据表,现在我需要编辑和删除表中的记录,所以我想在年份列旁边添加删除和编辑按钮。该列名称应作为操作。
index.html
<!doctype html>
<html class="no-js" lang="zxx">
<head>
<title>index</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="fontawesome-free-5.14.0-web\css\all.css">
</head>
<body>
<div class="adapt_area">
<div class="container">
<table id="newexample" class="table table-striped table-bordered table-light" style="width:100%">
<thead>
<tr>
<th>Name</th>
<th>Subject</th>
<th>Year</th>
</tr>
</thead>
</table>
</div>
</div>
</body>
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script src="https://cdn.datatables.net/1.10.22/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.22/js/dataTables.bootstrap4.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#newexample').dataTable({
"bProcessing": true,
"sAjaxSource": "fetchdata.php",
"aoColumns": [
{ mData: 'title' } ,
{ mData: 'name' },
{ mData: 'year_name' }
],
});
});
</script>
</html>
fetchdata.php
<?php
require 'db_connection.php';
$query = "SELECT notes.title,subject.name,year.year_name from notes
INNER JOIN subject ON notes.subject=subject.id
INNER JOIN year ON subject.year=year.id";
$result = $conn->query($query);
$data=array();
while($row = $result->fetch_array(MYSQLI_ASSOC)){
$data[] = $row;
}
$results = ["sEcho" => 1,
"iTotalRecords" => count($data),
"iTotalDisplayRecords" => count($data),
"aaData" => $data ];
echo json_encode($results);
?>
【问题讨论】:
-
您可以使用 DataTable createdRow 属性在空操作列中添加编辑/删除按钮(包括 columnDef 中的空列)。这样你可以在按钮中添加行 UID,这将更容易触发点击动作。
标签: javascript php html json datatable