前端数据传到后端
js
form.on('submit(sreach)', function (res) {
let data = res.field;
//console.log(data);
table.render({
elem: '#table'
, url: '/user/all' // 此处为静态模拟数据,实际使用时需换成真实接口
,where:data
, height: 'full-200' // 最大高度减去其他容器已占有的高度差
, cellMinWidth: 80
, page: true //开启分页
, cols: [[
{field: 'id', fixed: 'left', width: 60, title: 'ID', sort: true}
, {field: 'name', width: 100, title: '用户名'}
, {
field: 'email',
title: '邮箱 <i class="layui-icon layui-icon-email"></i>',
hide: 0,
width: 120,
edit: 'text'
}
, {field: 'sex', width: 70, title: '性别', sort: true}
, {field: 'phone', width: 120, title: '手机号'}
, {field: 'birthday', width: 120, title: '生日'}
, {field: 'address', width: 200, title: '地址'}
, {
field: 'enable', width: 120, title: '启用停用', templet: function (d) {
return d.enable === 1 ? '启用' : '停用'
}
}
, {fixed: 'right', title: '操作', toolbar: '#barDemo'}
]]
})
return false;
})
后端接收数据
mapper中的SQL语句
<select id="findAll" resultMap="userMap">
select id,
name,
phone,
email,
sex,
address,
birthday,
enable
from user
<where>
<if test="name!='' ">
and name like concat('%',#{name},'%')
</if>
<if test="email!='' ">
and email like concat('%',#{email},'%')
</if>
<if test="sex!='' ">
and sex = #{sex}
</if>
<if test="startBirthday!='' and endBirthday==''">
and birthday >=#{startBirthday}
</if>
<if test="startBirthday=='' and endBirthday!=''">
and birthday <=#{endBirthday}
</if>
<if test="startBirthday!='' and endBirthday!=''">
and birthday between #{startBirthday} and #{endBirthday}
</if>
<if test="enable==1 or enable == 0">
and enable=#{enable}
</if>
</where>
order by id desc
<bind name="page" value="(page - 1) * limit"/>
limit #{page},#{limit}
</select>
判断前端传到后端的值,当相应字段有数据时才能选中,用if语句对值进行判断。
名字部分进行模糊查询
<if test="name!='' ">
and name like concat('%',#{name},'%')
</if>