使用layui的数据表格完成的作业有分页和检索
<%--
Created by IntelliJ IDEA.
User: 29782
Date: 2024/5/4
Time: 15:32
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>table 组件综合演示 - Layui</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="layui/css/layui.css" rel="stylesheet">
<style>
.layui-hide {
overflow: hidden !important; /* 去掉滚动条 */
}
</style>
</head>
<body>
<!--
本「综合演示」包含:自定义头部工具栏、获取表格数据、表格重载、自定义模板、单双行显示、单元格编辑、自定义底部分页栏、表格相关事件与操作、与其他组件的结合等相对常用的功能,以便快速掌握 table 组件的使用。
-->
<div class="header">
</div><img height="110%" width="110%">
<form method="post" action="d.jsp" id="myform">
<div class="layui-col-sm3">
<input type="text" name="name" placeholder="政策名称" class="layui-input" id="name" >
</div>
<div class="layui-col-sm3">
<div class="layui-input-wrap">
<input type="text" name="number" placeholder="政策文号" lay-affix="clear" class="layui-input" id="number">
</div>
</div>
<div class="layui-col-sm3">
<input type="text" name="document" placeholder="发文机构" class="layui-input" id="document">
</div>
<div class="layui-col-sm3">
<input type="text" name="all" placeholder="全文检索" class="layui-input" id="text">
</div>
<button type="submit" class="layui-btn" id="btn">搜索</button>
</form>
<div style="padding: 16px;">
<table class="layui-hide" id="test" ></table>
</div>
<script type="text/html" id="toolbarDemo">
<div class="layui-btn-container">
<button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
<button class="layui-btn layui-btn-sm" lay-event="getData">获取当前页数据</button>
<button class="layui-btn layui-btn-sm" id="dropdownButton">
下拉按钮
<i class="layui-icon layui-icon-down layui-font-12"></i>
</button>
<button class="layui-btn layui-btn-sm layui-bg-blue" id="reloadTest">
重载测试
<i class="layui-icon layui-icon-down layui-font-12"></i>
</button>
<button class="layui-btn layui-btn-sm layui-btn-primary" id="rowMode">
<span>{{= d.lineStyle ? '多行' : '单行' }}模式</span>
<i class="layui-icon layui-icon-down layui-font-12"></i>
</button>
</div>
</script>
<script type="text/html" id="barDemo">
<div class="layui-clear-space">
<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
<a class="layui-btn layui-btn-xs" lay-event="more">
更多
<i class="layui-icon layui-icon-down"></i>
</a>
</div>
</script>
<script src="layui/layui.js"></script>
<script src="jQuery/jquery-1.7.2.js"></script>
<script>
layui.use(['table', 'dropdown'], function(){
var table = layui.table;
var dropdown = layui.dropdown;
// 创建渲染实例
table.render({
elem: '#test',
url: 'getdata1?name=&document=&number=&text=', // 此处为静态模拟数据,实际使用时需换成真实接口
toolbar: '#toolbarDemo',
defaultToolbar: ['filter', 'exports', 'print', {
title: '提示',
layEvent: 'LAYTABLE_TIPS',
icon: 'layui-icon-tips'
}],
height: 'full-35', // 最大高度减去其他容器已占有的高度差
css: [ // 重设当前表格样式
'.layui-table-tool-temp{padding-right: 145px;}'
].join(''),
cellMinWidth: 80,
totalRow: true, // 开启合计行
page: true,
limits: [5, 10, 15, 20, 25],
limit:10,
cols: [[
{type: 'checkbox', fixed: 'left'},
// {field:'id', fixed: 'left', width:80, title: 'ID', sort: true, totalRowText: '合计:'},
{field: 'name', title: '政策名称', width: '20%', sort: true},
{field: 'document', title: '发文机构', width: '20%'},
{field: 'starttime', title: '发布时间', minWidth: '20%'},
{field: 'type', title: '政策类型', width: '20%'},
// {field: 'city', title: '按钮', width: 100},
{fixed: 'right', title:'操作', width: 134, minWidth: 125, toolbar: '#barDemo'}
]],
done: function(){
var id = this.id;
// 下拉按钮测试
dropdown.render({
elem: '#dropdownButton', // 可绑定在任意元素中,此处以上述按钮为例
data: [{
id: 'add',
title: '添加'
},{
id: 'update',
title: '编辑'
},{
id: 'delete',
title: '删除'
}],
// 菜单被点击的事件
click: function(obj){
var checkStatus = table.checkStatus(id)
var data = checkStatus.data; // 获取选中的数据
switch(obj.id){
case 'add':
layer.open({
title: '添加',
type: 1,
area: ['80%','80%'],
content: '<div style="padding: 16px;">自定义表单元素</div>'
});
break;
case 'update':
if(data.length !== 1) return layer.msg('请选择一行');
layer.open({
title: '编辑',
type: 1,
area: ['80%','80%'],
content: '<div style="padding: 16px;">自定义表单元素</div>'
});
break;
case 'delete':
if(data.length === 0){
return layer.msg('请选择一行');
}
layer.msg('delete event');
break;
}
}
});
// 重载测试
dropdown.render({
elem: '#reloadTest', // 可绑定在任意元素中,此处以上述按钮为例
data: [{
id: 'reload',
title: '重载'
},{
id: 'reload-deep',
title: '重载 - 参数叠加'
},{
id: 'reloadData',
title: '仅重载数据'
},{
id: 'reloadData-deep',
title: '仅重载数据 - 参数叠加'
}],
// 菜单被点击的事件
click: function(obj){
switch(obj.id){
case 'reload':
// 重载 - 默认(参数重置)
table.reload('test', {
where: {
abc: '123456',
//test: '新的 test2',
//token: '新的 token2'
},
/*
cols: [[ // 重置表头
{type: 'checkbox', fixed: 'left'},
{field:'id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true, totalRowText: '合计:'},
{field:'sex', title:'性别', width:80, edit: 'text', sort: true},
{field:'experience', title:'积分', width:80, sort: true, totalRow: true, templet: '<div>{{= d.experience }} 分</div>'},
{field:'logins', title:'登入次数', width:100, sort: true, totalRow: true},
{field:'joinTime', title:'加入时间', width:120}
]]
*/
});
break;
case 'reload-deep':
// 重载 - 深度(参数叠加)
table.reload('test', {
where: {
abc: 123,
test: '新的 test1'
},
//defaultToolbar: ['print'], // 重载头部工具栏右侧图标
//cols: ins1.config.cols
}, true);
break;
case 'reloadData':
// 数据重载 - 参数重置
table.reloadData('test', {
where: {
abc: '123456',
//test: '新的 test2',
//token: '新的 token2'
},
scrollPos: 'fixed', // 保持滚动条位置不变 - v2.7.3 新增
height: 2000, // 测试无效参数(即与数据无关的参数设置无效,此处以 height 设置无效为例)
//url: '404',
//page: {curr: 1, limit: 30} // 重新指向分页
});
break;
case 'reloadData-deep':
// 数据重载 - 参数叠加
table.reloadData('test', {
where: {
abc: 123,
test: '新的 test1'
}
}, true);
break;
}
layer.msg('可观察 Network 请求参数的变化');
}
});
// 行模式
dropdown.render({
elem: '#rowMode',
data: [{
id: 'default-row',
title: '单行模式(默认)'
},{
id: 'multi-row',
title: '多行模式'
}],
// 菜单被点击的事件
click: function(obj){
var checkStatus = table.checkStatus(id)
var data = checkStatus.data; // 获取选中的数据
switch(obj.id){
case 'default-row':
table.reload('test', {
lineStyle: null // 恢复单行
});
layer.msg('已设为单行');
break;
case 'multi-row':
table.reload('test', {
// 设置行样式,此处以设置多行高度为例。若为单行,则没必要设置改参数 - 注:v2.7.0 新增
lineStyle: 'height: 95px;'
});
layer.msg('即通过设置 lineStyle 参数可开启多行');
break;
}
}
});
},
error: function(res, msg){
console.log(res, msg)
}
});
// 工具栏事件
table.on('toolbar(test)', function(obj){
var id = obj.config.id;
var checkStatus = table.checkStatus(id);
var othis = lay(this);
switch(obj.event){
case 'getCheckData':
var data = checkStatus.data;
layer.alert(layui.util.escape(JSON.stringify(data)));
break;
case 'getData':
var getData = table.getData(id);
console.log(getData);
layer.alert(layui.util.escape(JSON.stringify(getData)));
break;
case 'LAYTABLE_TIPS':
layer.alert('自定义工具栏图标按钮');
break;
};
});
// 表头自定义元素工具事件 --- 2.8.8+
table.on('colTool(test)', function(obj){
var event = obj.event;
console.log(obj);
if(event === 'email-tips'){
layer.alert(layui.util.escape(JSON.stringify(obj.col)), {
title: '当前列属性配置项'
});
}
});
// 触发单元格工具事件
table.on('tool(test)', function(obj){ // 双击 toolDouble
var data = obj.data; // 获得当前行数据
// console.log(obj)
if(obj.event === 'edit'){
layer.open({
title: '编辑 - id:'+ data.name,
type: 1,
area: ['80%','80%'],
content: '<div style="padding: 16px;"></div>'
});
} else if(obj.event === 'more'){
// 更多 - 下拉菜单
dropdown.render({
elem: this, // 触发事件的 DOM 对象
show: true, // 外部事件触发即显示
data: [{
title: '查看',
id: 'detail'
},{
title: '删除',
id: 'del'
}],
time :200000,
click: function(menudata){
if(menudata.id === 'detail'){
layer.open({
title: '编辑 - id:'+ data.name+data.text,
type: 1,
area: ['80%','80%'],
content: data.text
});
// layer.msg('查看操作,当前行 ID:'+ data.name+data.text);
} else if(menudata.id === 'del'){
layer.confirm('真的删除行 [id: '+ data.name +'] 么', function(index){
obj.del(); // 删除对应行(tr)的DOM结构
$.ajax({
url:'del?id='+data.name,
type:'post',
contentType:'application/json;charset=utf-8',
dataType:'text',
success:function (res){
layer.msg('删除成功');
},
error:function (res){
layer.msg('删除失败');
}
})
layer.close(index);
// 向服务端发送删除指令
});
}
},
align: 'right', // 右对齐弹出
style: 'box-shadow: 1px 1px 10px rgb(1 0 0/ 12%);' // 设置额外样式
})
}
});
// 触发表格复选框选择
table.on('checkbox(test)', function(obj){
console.log(obj)
});
// 触发表格单选框选择
table.on('radio(test)', function(obj){
console.log(obj)
});
// 行单击事件
table.on('row(test)', function(obj){
//console.log(obj);
//layer.closeAll('tips');
});
// 行双击事件
table.on('rowDouble(test)', function(obj){
console.log(obj);
});
// 单元格编辑事件
table.on('edit(test)', function(obj){
var field = obj.field; // 得到字段
var value = obj.value; // 得到修改后的值
var data = obj.data; // 得到所在行所有键值
// 值的校验
if(field === 'email'){
if(!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(obj.value)){
layer.tips('输入的邮箱格式不正确,请重新编辑', this, {tips: 1});
return obj.reedit(); // 重新编辑 -- v2.8.0 新增
}
}
// 编辑后续操作,如提交更新请求,以完成真实的数据更新
// …
layer.msg('编辑成功', {icon: 1});
// 其他更新操作
var update = {};
update[field] = value;
obj.update(update);
});
});
</script>
</body>
</html>
//import com.alibaba.fastjson.JSONObject;
//import com.google.gson.Gson;
//import com.google.gson.JsonObject;
import com.google.gson.Gson;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import javax.swing.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/getdata1")
public class getdata1 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
Dao dao = new Dao();
try {
String page=request.getParameter("page");
String limit=request.getParameter("limit");
String name=request.getParameter("name");
String document=request.getParameter("document");
String number=request.getParameter("number");
String text=request.getParameter("text");
System.out.println(name+"name");
List<policy> list = new ArrayList<>();
List<policy> list1 = new ArrayList<>();
// list = dao.get();
list=dao.getlimit(name,document,number,text,page,limit);
//list1=dao.get2(name);
Vo vo = new Vo();
vo.setCode(0);
vo.setMsg("success");
vo.setCount(dao.get1(name,document,number,text));
vo.setData(list);
Gson gson = new Gson();
String a = gson.toJson(vo);
resp.getWriter().write(a);
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doGet1(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
Dao dao=new Dao();
try{
List<policy> list=new ArrayList<>();
list=dao.get();
Vo vo=new Vo();
vo.setCode(0);
vo.setMsg("success");
vo.setCount(list.size()-300);
vo.setData(list);
Gson gson=new Gson();
String a =gson.toJson(vo);
resp.getWriter().write(a);
}catch (Exception e){
e.printStackTrace();
}
}
}
//import lombok.Data;
//import lombok.Getter;
import java.util.List;
//@Data
public class Vo {
public int code;
public String msg;
public int count;
public List<policy> data;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public List<policy> getData() {
return data;
}
public void setData(List<policy> data) {
this.data = data;
}
}
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/setdata")
public class setdata extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
String name=request.getParameter("name");
String number=request.getParameter("number");
String document=request.getParameter("document");
String all=request.getParameter("all");
System.out.println(name+"name11");
//request.setAttribute("name",name);
HttpSession session = request.getSession();
session.setAttribute("name", name);
session.setAttribute("number", number);
session.setAttribute("document", document);
session.setAttribute("all", all);
// request.setAttribute("name","Get1");
//resp.sendRedirect("getdata?action=Get1");
request.getRequestDispatcher("/getdata1").forward(request,resp);
}
}