{% block js %} <script> //Demo layui.use('form', function () { var form = layui.form; //监听提交 form.on('submit(formDemo)', function (data) { // data.field 当前容器的全部表单字段,名值对形式:{name: value} layer.msg(JSON.stringify(data.field)); }); }); </script> <script> layui.use(['laypage', 'layer'], function () { var laypage = layui.laypage , layer = layui.layer; //自定义样式 laypage.render({ elem: 'demo2' , limit: 20 , count: 1000 , theme: '#385EOF' , curr: {{ page|safe }} , jump: function (obj, first) { //obj包含了当前分页的所有参数,比如: console.log(obj.curr); //得到当前页,以便向服务端请求对应页的数据。 console.log(obj.limit); //得到每页显示的条数 //首次不执行 if (!first) { //do something location.href = "/lists/?page=" + obj.curr; } } }); });
时间: 2023-06-26 21:09:53 浏览: 127
这是一个使用了 layui 框架的分页功能的 JavaScript 代码。其中使用了两个模块:`form` 和 `laypage`。`form` 模块用于监听表单提交事件,`laypage` 模块用于渲染分页组件。
具体来说,该代码在页面加载完成后,使用 `form.on()` 方法监听名为 `formDemo` 的表单提交事件,并在提交时将表单字段以 JSON 格式输出。
同时,使用 `laypage.render()` 方法渲染一个 ID 为 `demo2` 的分页组件,该组件包含了总页数、每页显示条数、当前页等参数。在用户点击分页按钮时,通过 `location.href` 跳转到对应的页面。
相关问题
编写一个layui form表单监听多个按钮的demo
下面是一个监听多个按钮的layui form表单的demo:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Layui Form监听多个按钮</title>
<link rel="stylesheet" href="https://www.layuicdn.com/layui/css/layui.css">
</head>
<body>
<div class="layui-container">
<form class="layui-form">
<div class="layui-form-item">
<label class="layui-form-label">用户名</label>
<div class="layui-input-block">
<input type="text" name="username" required lay-verify="required" placeholder="请输入用户名" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">密码</label>
<div class="layui-input-block">
<input type="password" name="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<div class="layui-input-block">
<button class="layui-btn" lay-submit lay-filter="login">登录</button>
<button class="layui-btn layui-btn-primary" type="reset">重置</button>
</div>
</div>
</form>
</div>
<script src="https://www.layuicdn.com/layui/layui.js"></script>
<script>
layui.use(['form', 'layer'], function(){
var form = layui.form;
var layer = layui.layer;
//监听表单提交
form.on('submit(login)', function(data){
//模拟登录请求
if(data.field.username === 'admin' && data.field.password === '123456'){
layer.msg('登录成功');
}else{
layer.msg('用户名或密码错误');
}
return false;
});
//监听表单重置
form.on('reset', function(){
//重置表单时清空验证信息
form.render();
});
});
</script>
</body>
</html>
```
在这个demo中,我们使用了layui的form模块来实现表单的提交和重置操作。我们给登录按钮设置了`lay-filter`属性,值为`login`,这样就可以在表单提交时监听这个按钮的点击事件。同时,我们也监听了表单的重置事件。在表单提交时,我们可以通过`data.field`获取到表单中的所有数据,然后进行相应的处理。
编写1个程序,使用文件流的相关方法顺序完成以下操作: //读取文件Data.txt中的所有数据; //将读取得到的数据以二进制的形式存放到程序所在目录下的文件Res.dat中; //再次读取Res.dat中的所有数据; //并将这些数据以文本文件的形式存放到程序所在目录下的Res.txt文件中,存放时每行2个数据, //每个数据占10列,右对齐,左边补'C'字符。
1. 在pom.xml文件中添加mybatis-plus和mysql驱动的依赖
```xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
```
2. 配置数据源和mybatis-plus
在application.properties文件中添加以下配置:
```properties
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
# mybatis-plus配置
# 扫描mapper.xml文件的包路径
mybatis-plus.mapper-locations=classpath*:mapper/*.xml
# 实体类包路径
mybatis-plus.typeAliasesPackage=com.example.demo.entity
# 开启驼峰命名规则
mybatis-plus.configuration.map-underscore-to-camel-case=true
```
3. 创建Employee实体类
```java
@Data
public class Employee {
private Long id;
private String name;
private String password;
private Integer age;
private String gender;
private Date entryDate;
private String department;
}
```
4. 创建EmployeeMapper接口
```java
@Mapper
public interface EmployeeMapper extends BaseMapper<Employee> {
}
```
5. 创建EmployeeService接口和实现类
```java
public interface EmployeeService extends IService<Employee> {
}
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
}
```
6. 创建EmployeeController
```java
@RestController
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@PostMapping("/register")
public Result register(@RequestBody Employee employee) {
boolean success = employeeService.save(employee);
return success ? Result.success() : Result.failure("注册失败");
}
}
```
7. 创建前端页面(使用layui和ajax)
```html
<!DOCTYPE html>
<html>
<head>
<title>员工注册</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.staticfile.org/layui/2.5.7/css/layui.min.css">
</head>
<body>
<div class="layui-container" style="margin-top: 50px;">
<div class="layui-row">
<div class="layui-col-md-offset3 layui-col-md6">
<form class="layui-form" id="register-form">
<div class="layui-form-item">
<label class="layui-form-label">姓名</label>
<div class="layui-input-block">
<input type="text" name="name" lay-verify="required" placeholder="请输入姓名" autocomplete="off"
class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">密码</label>
<div class="layui-input-block">
<input type="password" name="password" lay-verify="required" placeholder="请输入密码"
autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">年龄</label>
<div class="layui-input-block">
<input type="text" name="age" lay-verify="required|number" placeholder="请输入年龄" autocomplete="off"
class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">性别</label>
<div class="layui-input-block">
<input type="radio" name="gender" value="男" title="男" checked>
<input type="radio" name="gender" value="女" title="女">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">入职时间</label>
<div class="layui-input-block">
<input type="text" name="entryDate" lay-verify="required" placeholder="yyyy-MM-dd" autocomplete="off"
class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">部门</label>
<div class="layui-input-block">
<input type="text" name="department" lay-verify="required" placeholder="请输入部门" autocomplete="off"
class="layui-input">
</div>
</div>
<div class="layui-form-item">
<div class="layui-input-block">
<button class="layui-btn" lay-submit lay-filter="register">注册</button>
<button type="reset" class="layui-btn layui-btn-primary">重置</button>
</div>
</div>
</form>
</div>
</div>
</div>
<script src="https://cdn.staticfile.org/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/layui/2.5.7/layui.min.js"></script>
<script>
layui.use(['form', 'layer'], function () {
var form = layui.form;
var layer = layui.layer;
form.on('submit(register)', function (data) {
$.ajax({
type: "POST",
url: "/employee/register",
contentType: "application/json",
data: JSON.stringify(data.field),
success: function (res) {
if (res.code === 0) {
layer.msg("注册成功");
} else {
layer.msg(res.msg);
}
},
error: function () {
layer.msg("网络错误");
}
});
return false;
});
});
</script>
</body>
</html>
```
以上就是使用mybatis-plus、springboot、layui和ajax实现员工注册的完整代码。
阅读全文