thymeleaf中使用layui(公用部分提取)

layui已经提供了一整套的UI布局及相关组件,并且有相当完善的api文档,本文重点在于thymeleaf中如何使用layui,包括公用部分的提取,而非简单的layui的用法。

先上两个效果图

thymeleaf中使用layui(公用部分提取)_第1张图片
列表.png

thymeleaf中使用layui(公用部分提取)_第2张图片
表单.png

一、思路

1.像传统的一样建文件头和尾(但是又和传统的写法不同,见下面代码)
2.定义公用js--common.js
3.定义公用css--用来重写一些样式或者自定义样式
4.编写列表页
5.编写表单页

二、文件头和尾

头部引入css,尾部引入js,定义两个模板(一个也可以,里面的碎片分开写,我喜欢写两个分别引入),注意th:fragment,fragment即碎片,可以在模板任何位置引入,此处为分别定义名为header和js_footer的碎片,以供后面引用

样式文件 link.html


js文件 script.html


三、公用js

该文件包含几个方法:
1.initTable 初始化带分页的table,注意其中的request属性,可以配置和你后台Page对象对应的属性,具体可以参考layui官方文档
2.searchTable table的搜索功能,调用layui table模块的的重载功能,参数组建思路:获取序列化的表单,组装成json对象
3.upload 文件上传
4.openFrame 打开弹出层,基于最顶层弹出

var Common = function () {
    var initTable = function (ele, url, cols, table, doneCallBack) {
        return table.render({
            elem: ele
            , url: url
            , method: 'POST'
            , cellMinWidth: 80 //全局定义常规单元格的最小宽度,layui 2.2.1 新增
            , cols: cols
            , page: {
                limits: [10, 20, 50, 100]
            },
            request: {
                pageName: 'current',
                limitName: 'size'
            },
            done: function (res, curr, count) {
                if (typeof(doneCallBack) === "function") {
                    doneCallBack(res);
                }
            }
        });
    };

    var searchTable = function (formId, tableIns) {
        var queryParams = getParams(formId);
        tableIns.reload({
            where: {condition: queryParams},
            page: {
                curr: 1 //重新从第 1 页开始
            }
        });
    };

    var getParams = function (formId) {
        var $ = layui.jquery;
        var _params = {};
        $.each($('#' + formId).serializeArray(), function (i, field) {
            if (null != field.value && "" != field.value) {
                _params[field.name] = field.value;
            }
        });
        return _params;
    };

    var upload = function (eleId, layUpload, done, error, accept, exts) {
        layUpload.render({
            elem: eleId //绑定元素
            , url: '/upload/' //上传接口
            , accept: accept === undefined ? 'file' : accept
            , exts: exts === undefined ? 'jpg|png|gif|bmp|jpeg' : exts
            , done: function (res) {
                //上传完毕回调
                if (typeof (done) === 'function') {
                    done(res)
                }
            }
            , error: function () {
                //请求异常回调
                if (typeof (error) === 'function') {
                    error()
                }
            }
        });
    };

    var openFrame = function (url, title, width, height) {
        width = width === undefined ? '900px' : width;
        height = height === undefined ? '500px' : height;
        return top.layer.open({
            area: [width, height],
            type: 2,
            title: title,
            content: url //这里content是一个URL,如果你不想让iframe出现滚动条,你还可以content: ['http://sentsin.com', 'no']
        });
    };
    return {
        initTable: function (ele, url, cols, table, doneCallBack) {
            return initTable(ele, url, cols, table, doneCallBack);
        },
        searchTable: function (formId, table) {
            searchTable(formId, table);
        },
        uploadFile: function (eleId, layUpload, done, error, accept, exts) {
            upload(eleId, layUpload, done, error, accept, exts);
        },
        openFrame: function (url, title, width, height) {
            return openFrame(url, title, width, height);
        }
    }
}();

四、公用css

这个可以根据个人喜好或者项目实际情况自己定义

.form-body{
    margin-top: 4%
}
.layui-input-inline{
    width: 350px !important;
}

五、列表页

整个html除了头部的引入,整体分为三部分:
1.上面form为搜索内容部分
2.中间一个table为主窗口
3.注意一个id="toolBars"的js,为工具条,用于追加在每一列的后面
4.可以看到下面页面初始化等都调用了Common.js中的对应方法




    Title
    
条件搜索

六、表单页

列表出来之后表单就更简单了,只需要去layui官网找个表单,根据自己的实际情况布局即可,特别说明的是校验,即verify.js,用于自己对验证方法的扩展
form.html




    Title
    
*6-12个字符
*包含a_z、A_Z、1-9中的两种,且长度6-20
*手机号
*不多于30个字符

verify.js

layui.use('form', function () {
    var form = layui.form;
    //自定义验证规则
    form.verify({
        username: function (value) {
            if (value.length < 6 || value.length > 12) {
                return '请输入6到12位的用户名';
            }
        }, password: function (value) {
            if (value.length < 4) {
                return '内容请输入至少4个字符';
            }
        }
        , phone: [/^1[3|4|5|7|8]\d{9}$/, '手机必须11位,只能是数字!']
        , email: [/^[a-z0-9._%-]+@([a-z0-9-]+\.)+[a-z]{2,4}$|^1[3|4|5|7|8]\d{9}$/, '邮箱格式不对']
    });
})

其他小技巧:
idea使用thymeleaf模板时,页面取值总提示无法解析变量,但是又不影响实际应用,据说idea2017.3已经解决了,但是我现在用的2017.2,没有更新,但是同样可以解决。错误提示如图


img_87a1b4ef5f15caec3d40c70a97b54f61.png
波浪线提示.png

解决方法也很简单:File--settings--Editor--Inspections--Thymeleaf,去掉第一个校验即可。


thymeleaf中使用layui(公用部分提取)_第3张图片
去掉波浪线提示.png

你可能感兴趣的:(thymeleaf中使用layui(公用部分提取))