【发布时间】:2021-03-19 17:08:38
【问题描述】:
这是我的index.html 文件:
<button id="getAllGroups" type="button" class="btn btn-primary">Groups</button>
<div class="container">
<h2 align="center">LESSONS</h2>
<table class="table table-dark" border="1" width="100%" cellpadding="5">
<thead>
<th>GROUP ID</th>
<th>GROUP NAME</th>
</thead>
<tbody id="tbody">
</tbody>
</table>
</div>
在我的js 文件下方:
GET: $(document).ready(
function () {
// GET REQUEST
$("#getAllGroups").click(function (event) {
event.preventDefault();
ajaxGet();
});
// DO GET
function ajaxGet() {
$.ajax({
type: "GET",
url: "checkGroups",
success: function (result) {
if (result.status == "success") {
var custList = "";
$.each(result.data,
function (i, group) {
var Html = "<tr>" +
"<td>" + group.groupId + "</td>" +
"<td>" + group.groupName + "</td>" +
"</tr>";
console.log("Group checking: ", group);
$('#tbody').append(Html);
});
console.log("Success: ", result);
} else {
console.log("Fail: ", result);
}
},
});
}
})
休息控制器:
@RestController
public class GroupController {
@Autowired
GroupService groupService;
@GetMapping("/checkGroups")
public ResponseEntity<Object> getAllGroups() {
ServiceResponse<List<Group>> response = new ServiceResponse<>("success", groupService.getAll());
return new ResponseEntity<Object>(response, HttpStatus.OK);
}
}
我的代码有效,但 th :组 ID 和组名称在页面上,即使我没有点击按钮 Groups,但我需要我的表格仅在点击按钮后显示。
如果我不点击按钮,表格应该是隐藏的。
提前感谢您的回复。
【问题讨论】: