Layui 行点击追加元素效果

本文介绍了一个基于JavaScript的表格行点击事件处理方法,详细解释了如何在点击表格行时显示特定数据,并通过动态生成HTML来展示化验详情及图片预览。此功能涉及到DOM操作、事件监听和数据绑定等技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
* 单击事件
* @param obj
*/
var dom;
var state;
WarningIllegalIntime.onRowClick=function(obj) {
console.log(obj.data.id,"6666666")
var data = obj.data;
console.log(data.id);
if (state != 1) {
if(state==0){
if(dom==null){//state 0
bbb(obj);
state=1;
}else{//state 0
bbb(obj)
state=1;
}
}else{
bbb(obj);
state=1;
}
}else{//state 1 打开状态
if(dom!=null){
if(data.id==dom){
var ids = document.getElementById(dom);
ids.remove();
state = 0;
}else if(data.id==null){
var ids = document.getElementById(dom);
ids.remove();
state = 0;
}else{
var ids = document.getElementById(dom);
ids.remove();
bbb(obj);
state = 1;
}
}else{
bbb(obj);
state = 1;
}
}
}

function bbb(obj){
var assayName=[];
var standardValue=[];
var assayValue=[];
var value=obj.data.assayDetailList;
for(var i=0;i<value.length;i++){
assayName[i]=value[i].assayName;
standardValue[i]=value[i].standardValue;
assayValue[i]=value[i].assayValue;
}
var data = obj.data;
var html = "<tr id=" + data.id + "><td colspan='18' align='center' valign='middle'>"
html+="<table>"
html+="<tr><td>指标项</td>"
for(a in assayName){
html+="<td style='width: 90px;text-align: center' >"+assayName[a]+"</td>"
}
html+="</tr>"
html+="<tr><td>标准值</td>"
for(b in standardValue){
html+="<td style='width: 90px;text-align: center'>"+standardValue[b]+"</td>"
}
html+="</tr>"
html+="<tr><td>化验值</td>"
for(c in assayValue){
html+="<td style='width: 90px;text-align: center'>"+assayValue[c]+"</td>"
}
html+="</tr>"
html+="</table>"
html+="<table><tr>"
html+='<td data-role="fieldcontain"><a class="aaa" id="zoom8" style="max-width:200px;display: block" href='+data.storageInImg.roughFront+' data-gallery=""><img onclick="ab(this)" class="img-thumbnail" src="'+data.storageInImg.roughFront+'" style="max-width:200px;height: 120px;"></a></td>'
html+='<td data-role="fieldcontain"><a class="aaa" id="zoom9" style="max-width:200px;display: block" href='+data.storageInImg.roughBack+' data-gallery=""><img onclick="ab(this)" class="img-thumbnail" src='+data.storageInImg.roughBack+' style="max-width:200px;height: 120px;"></a></td>'
html+='<td data-role="fieldcontain"><a class="aaa" id="zoom10" style="max-width:200px;display: block" href='+data.storageInImg.tareFront+' data-gallery=""><img onclick="ab(this)" class="img-thumbnail" src='+data.storageInImg.tareFront+' style="max-width:200px;height: 120px;"></a></td>'
html+='<td data-role="fieldcontain"><a class="aaa" id="zoom11" style="max-width:200px;display: block" href='+data.storageInImg.tareBack+' data-gallery=""><img onclick="ab(this)" class="img-thumbnail" src='+data.storageInImg.tareBack+' style="max-width:200px;height: 120px;"></a></td>'
html+='<td data-role="fieldcontain"><a class="aaa" id="zoom12" style="max-width:200px;display: block" href='+data.storageInImg.settlementPic+' data-gallery=""><img onclick="ab(this)" class="img-thumbnail" src='+data.storageInImg.settlementPic+' style="max-width:200px;height: 120px;"></a></td></tr><table>'
html+="</td></tr>"
dom =data.id;
obj.tr.after(html)
}

转载于:https://www.cnblogs.com/lixinzhen/p/10494417.html

### 实现 Layui 表格中点击按钮动态添加一数据 为了实现在 Layui 中通过点击按钮向表格中动态添加一数据,可以利用 JavaScriptLayui 提供的相关 API 来完成此操作。具体来说,在页面加载完成后初始化表格,并提供一个用于触发新插入的按钮。 当用户点击该按钮时,可以通过调用 `table.reload` 或者直接修改原始的数据源并刷新表格视图来达到目的。以下是具体的实现方式: #### HTML 部分 创建一个按钮以便于触发新增为以及展示表格结构[^1]: ```html <button id="addRowBtn">添加一</button> <table id="demo" lay-filter="test"></table> ``` #### 初始化表格配置 定义表格的基础配置项,包括表头信息等: ```javascript layui.use(['table'], function(){ var table = layui.table; // 渲染表格实例 table.render({ elem: '#demo' ,height: 312 ,cols: [[ {field:'id', title:'ID'} ,{field:'username', title:'用户名'} ,{fixed: 'right', align:'center', toolbar:'#barDemo'} //这里的toolbar是工具栏模板的选择器 ]] ,data: [ {"id": "0", "username":"初始用户"} ] }); }); ``` #### 动态添加数据逻辑 编写函数处理按钮点击事件并向现有数据集中追加新的记录条目: ```javascript document.getElementById('addRowBtn').addEventListener('click',function () { let newDataItem = {"id": new Date().getTime(), "username":"新用户"}; // 获取当前表格对象 const instance = layui.table.cache['demo']; // 更新本地存储的数据数组 instance.push(newDataItem); // 刷新表格显示最新的数据集 layui.table.reload('demo',{ data:instance, page:false // 关闭分页功能以确保即时更新 }) }) ``` 上述代码片段展示了如何监听外部按钮的点击动作,并据此执相应的业务流程——即往表格里增加一条全新的记录。注意这里采用了时间戳作为唯一标识符(id),实际应用可根据需求调整这一部分的设计思路。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值