tp5+bootstrap+ajax进行基本的CRUD

这里写图片描述

界面是这样的,很简单,但是我做了好多天,因为什么都不会,所有的地方都要想办法解决。

需要解决的问题如下:

1.表格如何根据数据库动态显示?
2.添加修改部门的弹窗怎么显示?
3.批量删除如何识别checkbox是否进行了选择?
4.如何用ajax异步进行更新?

1.表格动态显示数据库数据。

使用volist进行动态处理,数据传入方式: $this->assign('departs', $departs);
volist动态显示方式(使用html即可)

{volist name="departs" id="item"}
    <tr class="change">
                    <td style="text-align:left;"><input type="checkbox" class="check" value={$i}></td>
                    <td style="text-align:left;">{$depart}</td>
                    <td style="text-align:left;">
                        <button type="button" class="btn btn-warning" onclick='editSingle( <?php echo $i; ?>,<?php echo json_encode($departs); ?>)'>修改</button>
                        <button type="button" class="btn btn-danger" onclick='delSingle(<?php echo $i; ?>,<?php echo json_encode($departs); ?>)'>删除</button>
                    </td>
                </tr>
{/volist}

2.显示添加修改的弹窗

其实就是bootstrap的弹窗了。
js调用方式:
$(‘#myModal’).modal();句用于显示

 $("#myModalLabel").text('修改部门');
 $('#myModal').modal();
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">新增</h4>
            </div>
            <div class="modal-body">

                <div class="form-group">
                    <label for="txt_departmentname">部门名称</label>
                    <input type="text" name="txt_departmentname" class="form-control" id="txt_departmentname"
                           placeholder="部门名称">
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal"><span
                        class="glyphicon glyphicon-remove" aria-hidden="true"></span>关闭
                </button>
                <button type="button" id="btn_submit" class="btn btn-primary" data-dismiss="modal" onclick="save()">
                    <span class="glyphicon glyphicon-floppy-disk" aria-hidden="true"></span>保存
                </button>
            </div>
        </div>
    </div>
</div>

3.获取批量删除的选项

checkbox需要设置value:

<td style="text-align:left;"><input type="checkbox" class="check" value={$i}></td>

js获取选中的id

function getSelected() {
    var ids = [];
    //意思是选择被选中的checkbox
    $.each($('input:checkbox.check:checked'), function () {
        ids.push($(this).val());
    });
    return ids;
}

4.ajax进行异步更新

这里需要用到jquery

$.ajax({
        type: 'POST',
        dataType:'json',
        data:{param:JSON.stringify(delIds)},
        url : '/index/index/deldepart',
        success:function(data){
            var obj = eval('('+data+')');
            if(obj.code == 1){
                alert(obj.msg);
                window.location.reload(true);
            }else{
                alert(obj.msg);
            }
        }
    });

php:

obj['code']=1;
obj['msg']="ajax成功";
return json_encode(obj);
Thymeleaf是一种Java模板引擎,而Bootstrap是一种前端框架,可以使Web开发更加高效。要使用Thymeleaf和Bootstrap5发送Ajax请求,我们可以使用jQuery的Ajax函数来实现。 首先,我们需要在HTML文件中引入jQuery库和Bootstrap样式库。然后,在需要发送Ajax请求的按钮或链接上添加一个ID或特定的类,并使用JavaScript代码将其与Ajax事件绑定。在Ajax事件中,我们可以指定请求类型、URL、数据等,还可以定义成功或失败时要执行的代码。 对于Thymeleaf,我们可以使用其内置的表达式语言将数据绑定到HTML元素中,例如将表格中的数据使用循环结构进行填充,或将表单的数据传递到控制器中。 具体而言,我们可以使用如下代码来实现Thymeleaf Bootstrap5发送Ajax请求: HTML代码: ```html <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Thymeleaf Bootstrap5 Ajax Request Demo</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.1.1/css/bootstrap.min.css"> </head> <body> <div class="container"> <h2>Thymeleaf Bootstrap5 Ajax Request Demo</h2> <table class="table table-striped table-bordered table-hover"> <thead> <tr> <th>ID</th> <th>Name</th> <th>Age</th> </tr> </thead> <tbody> <tr th:each="user: ${users}"> <td th:text="${user.id}"></td> <td th:text="${user.name}"></td> <td th:text="${user.age}"></td> <td> <a href="javascript:void(0)" class="btn btn-info btn-xs edit-btn" th:data-id="${user.id}">Edit</a> <a href="javascript:void(0)" class="btn btn-danger btn-xs delete-btn" th:data-id="${user.id}">Delete</a> </td> </tr> </tbody> </table> </div> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.1.1/js/bootstrap.bundle.min.js"></script> <script> $(document).ready(function () { // Edit button click event $('.edit-btn').click(function () { var id = $(this).data('id'); $.ajax({ type: "GET", url: "/user/" + id, success: function (data) { $('#id').val(data.id); $('#name').val(data.name); $('#age').val(data.age); $('#editModal').modal('show'); }, error: function (jqXHR, textStatus, errorThrown) { alert('Error getting user by ID'); } }); }); // Delete button click event $('.delete-btn').click(function () { var id = $(this).data('id'); if (confirm('Are you sure you want to delete this user?')) { $.ajax({ type: "DELETE", url: "/user/" + id, success: function (data) { window.location.reload(); }, error: function (jqXHR, textStatus, errorThrown) { alert('Error deleting user'); } }); } }); }); </script> </body> </html> ``` 这里我们为Edit按钮和Delete按钮分别添加了edit-btn和delete-btn类,并为其设置了数据属性th:data-id,用于记录需要编辑或删除的用户ID。在JavaScript代码中,我们使用jQuery的Ajax函数分别为这两个按钮绑定了点击事件,并在事件中分别发送了GET和DELETE类型的Ajax请求,获取或删除对应的用户数据。 需要注意的是,我们使用了Thymeleaf的内置表达式语言将数据渲染到了HTML中,并在用户单击Edit按钮时使用了Bootstrap的Modal组件来显示编辑面板。此外,我们还为删除按钮添加了一个确认框,以防止误操作。 总的来说,使用Thymeleaf Bootstrap5发送Ajax请求非常方便,能够大大提高Web开发效率和用户体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值