网站首页 > 博客文章 正文
需求目的:
双击web单元格编辑后,保存数据到mongodb数据库。就像Excel那样更新数据。
1. 首先Layui 标识列可单击修改
添加 edit:text标识,标识列可修改
{field: 'Id', title: '编号', width: 80, sort: true, fixed: 'left'}
,{field: 'username', title: '用户名' , edit: 'text'}
,{field: 'gender', title: '性别', width: 80 , edit: 'text'}
,{field: 'password', title: '密码', width: 80 , edit: 'text'}
,{field: 'address', title: '地址', width: 80 , edit: 'text'}
,{field: 'email', title: '邮箱', width: 80 , edit: 'text'}
,{field: 'phone', title: '电话', width: 80 , edit: 'text'}
此时点击数据表,可看到数据表进入编辑模式
2. 获取编辑表格事件,添加列修改后监听:
//监听单元格编辑
table.on('edit(test_table)',
function (obj) {
var value = obj.value //得到修改后的值
,
data = obj.data //得到所在行所有键值
,
field = obj.field; //得到字段
layer.msg('[ID: ' + data.Id + '] ' + field + ' 字段更改为:' + value);
console.log(data.Id);
var jsonResult = dataServer.updateUserInfo(obj.data);
if(jsonResult==1){
layer.alert("更新成功", {icon: 6},function () {
x_admin_father_reload();
});
}else{
layer.msg("更新成功!");
}
});
3. 后台添加对应的编辑修改接口:
/**
修改用户列表
*/
func (m UserC) Update(g *gin.Context) {
rsp := new(Rsp)
var info models.User
err := g.BindJSON(&info)
if err != nil {
rsp.Msg = "json faild"
rsp.Code = 201
g.JSON(http.StatusOK, rsp)
return
}
if info.Id.String() == "" {
rsp.Msg = "id is empty!"
rsp.Code = 201
g.JSON(http.StatusOK, rsp)
return
}
filter := bson.D{{"_id", info.Id}}
//selector := bson.M{"_id": updateId}
//updateInfo,_ :=bson.Marshal(&info)
result := m.Mgo.Collection(db.User).FindOneAndReplace(context.Background(), filter, info)
if result != nil {
if result.Err() != nil {
fmt.Println(result.Err())
}
rsp.Msg = "success"
rsp.Code = 200
rsp.Data = 1
g.JSON(http.StatusOK, rsp)
return
}
rsp.Msg = "err"
rsp.Code = 201
rsp.Data = 0
g.JSON(http.StatusOK, rsp)
return
}
4. 查看完成效果:
单击表格并编辑成功!
获取源代码请转发后回复:1
猜你喜欢
- 2025-03-30 Go操作etcd(csgo操作)
- 2025-03-30 用Go语言编写实用的以太坊钱包程序
- 2025-03-30 Etcd服务注册与发现封装实现--golang
- 2025-03-30 使用Go实现一个数据库连接池(使用go实现一个数据库连接池的方法)
- 2025-03-30 Go中如何优雅关闭HttpServer?(关闭go module)
- 2025-03-30 OpenTelemetry 实战:从零实现应用指标监控
- 2025-03-30 如何使用Go语言是操作Docker(go语言sdk)
- 2025-03-30 源码分析 kubelet gc 垃圾回收的实现原理
- 2025-03-30 Java程序员学Go语言指南(下篇)(java程序员需要学的东西)
- 2025-03-30 golang 限流——time/rate(golang限流器)
你 发表评论:
欢迎- 374℃手把手教程「JavaWeb」优雅的SpringMvc+Mybatis整合之路
- 369℃用AI Agent治理微服务的复杂性问题|QCon
- 360℃初次使用IntelliJ IDEA新建Maven项目
- 353℃Maven技术方案最全手册(mavena)
- 351℃安利Touch Bar 专属应用,让闲置的Touch Bar活跃起来!
- 348℃InfoQ 2024 年趋势报告:架构篇(infoq+2024+年趋势报告:架构篇分析)
- 348℃IntelliJ IDEA 2018版本和2022版本创建 Maven 项目对比
- 344℃从头搭建 IntelliJ IDEA 环境(intellij idea建包)
- 最近发表
- 标签列表
-
- powershellfor (55)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- vue数组concat (56)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)