Here is my index.html
file:
<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>
Below my js
file:
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:
@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);
}
}
My code works but th
: GROUP ID and GROUP NAME is on page even if I don't click on button Groups
but I need that my table shows only after click on button. If I don't click on button, the table should be hidden.
Thanks in advance for responding.
I need that my table shows only after click on button. If I don't click on button, the table should be hidden.
In this case, hide the table when the page loads using CSS and display it when the button is clicked using show()
:
.container table { display: none; }
// in the $.ajax success handler:
let html = result.data.map(g => `<tr><td>${g.groupId}</td><td>${g.groupName}</td></tr>`;
$('#tbody').append(html);
$('.container table').show();
Note the simplified and more performant use of map()
to build your HTML in the above sample.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.