如何通过点击按钮隐藏和显示表格?

wgxvkvu9  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(396)

这是我的 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);
                }
            },
                        });
    }
})

REST控制器:

@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 但我需要我的表显示后,才点击按钮。如果我不按一下按钮,表应该是隐藏的。
提前感谢您的回复。

ovfsdjhp

ovfsdjhp1#

我需要我的表显示后,才点击按钮。如果我不按一下按钮,表应该是隐藏的。
在这种情况下,在使用css加载页面时隐藏该表,在使用css单击按钮时显示该表 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();

请注意 map() 在上面的示例中构建html。

相关问题