layui table实现多条件搜索

版权声明:请勿用于任何商业用途,转载请说明出处!

百度了好多文章,效果都不是很满意,最终还是需要自己动手!经过不断的测试,终于做出来了!

2021.06.30:之前的思路无法解决前端分页问题,代码做了下修改!

先展示下效果:

1.页面无条件初始化

2.搜索条件查询结果

源码:

1.前端页面代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head lang="en" th:replace="include/common/common :: commonHeader">
    <footer th:include="include/common/common :: layUiResource"></footer>
    <!--    <footer th:include="include/common/common :: bootstrapResource"></footer>-->
    <footer th:include="include/common/common :: blueCSSResource"></footer>

    <script>
        layui.config({
            base: '/staticResource/layuiadmin/' //静态资源所在路径
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index' , 'table'] , function () {
            var table = layui.table
                ,admin = layui.admin
                ,element = layui.element;

            let entityName = 'config';

            $(document).on('click' , '#' + entityName + 'SearchBtn' ,function () {
                createTable(this);
            });


            function createTable(e){
                let formId = $(e).attr("blue-form-id")
                    ,formData = $("#" + formId).serialize()
                    ,formUrl = $("#" + formId).attr("action");
                table.render({
                    elem: '#test-table-operate'
                    ,url:formUrl + '?config = ' + formData
                    // [[${pageSize}]]:thymeleaf模板引擎语法,由后端传入
                    ,limit: [[${pageSize}]]
                    ,page: true
                    ,parseData: function(res) { //res 即为原始返回的数据
                        return {
                            "code": 0, //解析接口状态
                            "msg": '', //解析提示文本
                            "count": res.total, //解析数据长度
                            "data": res.records //解析数据列表
                        };
                    }
                    ,cols:[
                        [
                            {type:'checkbox', fixed: 'left'}
                            ,{field:'id', title: 'ID', sort: true, hide:true, fixed: 'left'}
                            ,{field:'userName', sort:true , title: '用户名'}
                            ,{field:'loginCode', sort:true , title: '登录名'}
                            ,{title:'操作' , align:'center', fixed: 'right', toolbar: '#test-table-operate-barDemo'}
                        ]
                    ]
                });
            };

            $(document).ready(function () {
                let searchBtn = '#' + entityName + 'SearchBtn';
                createTable(searchBtn);
            });


            //监听工具条
            let openFormActive = {
                tabAdd:function (userId) {
                    layui.router();
                    let i = "/a/user/form?id=" + userId , t = "用户表单";
                    let l=parent===self?layui:top.layui;l.index.openTabsPage(i,t);
                }
            };

            //todo:以下测试,未完成
            //监听表格复选框选择
            table.on('checkbox(test-table-operate)', function(obj){
                console.log(obj)
            });

            table.on('tool(test-table-operate)', function(obj){
                var data = obj.data;
                if(obj.event === 'detail'){
                    layer.alert('查看:<br>'+ JSON.stringify(data))
                    // layer.msg('ID:'+ data.id + ' 的查看操作');
                } else if(obj.event === 'del'){
                    layer.confirm('真的删除行么', function(index){
                        obj.del();
                        layer.close(index);
                    });
                } else if(obj.event === 'edit'){
                    openFormActive['tabAdd'].call(this , data.id);

                }
            });

            var active = {
                getCheckData: function(){ //获取选中数据
                    var checkStatus = table.checkStatus('test-table-operate')
                        ,data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                }
                ,getCheckLength: function(){ //获取选中数目
                    var checkStatus = table.checkStatus('test-table-operate')
                        ,data = checkStatus.data;
                    layer.msg('选中了:'+ data.length + ' 个');
                }
                ,add: function(){
                    layui.router();
                    let i = "/a/" + entityName + "/form?isNewRecord=true" , t = "参数信息";
                    let l=parent===self?layui:top.layui;l.index.openTabsPage(i,t);
                }
            };

            $('.test-table-operate-btn .layui-btn').on('click', function(){
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
        });
    </script>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">用户信息</div>
                <div class="layui-card-body">
                        <div class="layui-row layui-col-space10 layui-form-item">
                            <form class="form-horizontal" id="userSearchForm" th:object="${user}" method="post" action="/a/user/findPage">
                                <div class="layui-col-lg8">
                                    <div class="layui-col-lg6">
                                        <label class="layui-form-label">登录名:</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="loginCode" id="searchLoginCode" placeholder="登录名" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>

                                    <div class="layui-col-lg6">
                                        <label class="layui-form-label">用户名:</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="userName" id="searchUserName" placeholder="登录名" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                </div>

                                <div class="layui-col-lg4">
                                    <button type="button" class="layui-btn" id="userSearchBtn" onsubmit="return false" blue-form-id="userSearchForm">搜索</button>
                                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                                </div>
                            </form>
                        </div>

                </div>
            </div>
            <div class="layui-card">
                <div class="layui-card-body" >
                    <div class="layui-btn-group test-table-operate-btn" id="tableBtn" style="margin-bottom: 10px;">
                        <button class="layui-btn layui-btn-sm" data-type="add">验证是否全选</button>
                        <button class="layui-btn layui-btn-sm" data-type="getCheckData">获取选中行数据</button>
                        <button class="layui-btn layui-btn-sm" data-type="getCheckLength">获取选中数目</button>
                    </div>
                    <table class="layui-hide" id="test-table-operate" lay-filter="test-table-operate"></table>

                    <script type="text/html" id="test-table-operate-barDemo">
                        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
                        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                    </script>
                </div>

            </div>
        </div>
    </div>
</div>
</body>
</html>

2.服务端

2.1 controller、service层代码:

// controller层findPage
@RequiresPermissions("user:view")
    @RequestMapping("/findPage")
    @ResponseBody
    public IPage<User> findPage(@RsaSecurityParameter User user){
        return userService.findPage(user);
    }


//service层findPage
public IPage<User> findPage( User entity, String... args) {
        Map<String , Object> search = null;
        if (EmptyUtil.isNoEmpty(entity.getUserName())
                || EmptyUtil.isNoEmpty(entity.getLoginCode())
        ){
            search = new HashedMap();
            if (EmptyUtil.isNoEmpty(entity.getUserName())){
                search.put("USER_NAME" , entity.getUserName());
            }
            if (EmptyUtil.isNoEmpty(entity.getLoginCode())){
                search.put("LOGIN_CODE" , entity.getLoginCode());
            }
        }else {
            long limit = entity.getLimit();
            long page = entity.getPage();
            entity = new User();
            entity.setLimit(limit);
            entity.setPage(page);
        }
        return super.findPage(entity , search , args);
    }

//service父类中的findPage
  public IPage<T> findPage(T entity , Map<String , Object> search , String... args){
        long current = 1L;
        long size = 10L;
        current =  EmptyUtil.isNoEmpty(ReflexUtil.getFieldValue(entity , "page"))?(long) ReflexUtil.getFieldValue(entity , "page") :current;
        size = EmptyUtil.isNoEmpty(ReflexUtil.getFieldValue(entity , "limit"))? (long) ReflexUtil.getFieldValue(entity , "limit") :size;
        QueryWrapper<T> queryWrapper;
        if (EmptyUtil.isNoEmpty(search)){
            queryWrapper = new QueryWrapper<>();
            for (Map.Entry<String , Object> entry:search.entrySet()
                 ) {
                queryWrapper.like(entry.getKey() , entry.getValue());
            }
        }else {
            queryWrapper = new QueryWrapper<>(entity);
        }
        queryWrapper.orderByAsc(args);
        return super.page(new Page<T>(current , size) , queryWrapper);
    }

2.2 实体类需要limit、size属性用于接收前端传入的分页信息

public class User extends PageEntity<User>  //User实体类继承一个自定义的page父类



// 自定义page父类
@ToString
@NoArgsConstructor
public class PageEntity<T> extends BaseEntity<T> {
    private static final long serialVersionUID = -8596775877393206795L;

    @TableField(exist = false)
    @Setter
    @Getter
    protected long limit;
    @TableField(exist = false)
    @Setter
    @Getter
    protected long page;


}

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 实现 springboot 和 layui 表格分页的步骤如下: 1. 前端页面:在使用 layui 表格的页面引入 layui.js 和 layui.css 文件。 2. 后端代码:使用 springboot 构建后端服务,通过 HTTP 请求向前端发送数据。 3. 分页参数:在前端页面,使用 layui table 控件定义分页参数,例如:每页显示数量、当前页码等。 4. 分页请求:使用 layui table 控件发送请求到后端,后端通过分页参数从数据库中查询相应的数据并返回给前端。 5. 表格渲染:前端接收到后端返回的数据,使用 layui table 控件将数据渲染到表格中,实现表格分页功能。 6. 翻页事件:使用 layui table 控件监听翻页事件,在事件触发时重新发送请求到后端,实现下一页或上一页的数据加载。 希望以上步骤能帮助您实现 springboot 和 layui 表格分页。 ### 回答2: 在SpringBoot和Layui实现表格分页的过程中,可以通过以下步骤实现: 1. 在后端代码中,首先引入Layui的DataTable插件,使用Layui提供的数据表格组件实现分页效果。 2. 在后端代码中,通过SpringBoot的控制器(Controller)接收前端传递的分页参数,例如当前页码(page)和每页显示数量(limit)。 3. 在后端代码中,根据接收到的分页参数,利用数据库查询语句查询数据表中的数据,并根据传递的分页参数进行分页操作。 4. 将查询到的分页数据返回给前端。 5. 在前端代码中,使用Layuitable模块生成表格,并设置分页参数。 6. 通过JavaScript在前端代码中调用Layuitable模块提供的分页方法,根据后端返回的分页数据动态生成分页组件。 7. 在前端代码中,利用JavaScript将查询到的数据填充到表格的相应位置。 总结来说,SpringBoot和Layui实现表格分页的核心是后端对分页参数的接收和数据的分页操作,以及前端对后端返回的分页数据的处理和分页组件的生成。通过这样的操作,可以实现便捷的表格分页功能。 ### 回答3: 在Spring Boot和Layui结合使用时,实现表格分页可以通过以下步骤来实现: 1. 在后端使用Spring Boot开发接口,查询数据库获取分页数据。可以使用Spring Data JPA或MyBatis等持久层框架进行数据库操作。 2. 定义一个Java类来表示表格数据的结果集,包含总记录数和当前页数据列表。 3. 在后端接口中,根据前端传递的页码和每页显示的记录数,通过查询条件进行筛选,并使用分页查询语句获取对应页码的数据。 4. 将查询结果封装到步骤2中定义的实体类中,并返回给前端。 5. 在前端的HTML页面中引入Layui框架的相关CSS和JS文件。 6. 在HTML页面中使用Layui提供的表格组件,结合异步加载数据的方式进行数据展示和分页操作。 7. 在Layui的表格组件中,配置分页参数,包括每页显示的记录数、总记录数以及分页样式等。 8. 在Layui的表格组件的回调事件中,监听分页操作,并通过Ajax请求后端接口获取指定页的数据,并将返回的数据展示到表格中。 通过以上步骤,可以实现Spring Boot和Layui结合使用的表格分页功能。需要注意的是,在后端接口中,除了分页查询外,还需要进行条件查询、排序等操作,以满足不同的业务需求。同时,前端页面中还可以添加筛选条件搜索功能等,以提高用户体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值