侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Layui弹框中数据表格中可双击选择一条数据的实现

前端  /  管理员 发布于 6年前   342

Layui提供的功能如下(预览)

可自行查看:layui官网此模块的链接
着急看双击选中 直接看标黄色部分

假设这是个弹窗里的表格和数据点击圆圈,圆圈变绿则为选中,选中后点击上方查看数据按钮(实际中是确认按钮,实际中点击确认按钮后会关闭弹窗并把json串带到原本页面中)

Layui提供的代码如下(查看代码)

<body><!-- 表格空架子 --><table class="layui-hide" id="test" lay-filter="test"></table><!-- 确认(查看数据按钮) --><script type="text/html" id="toolbarDemo"> <div class="layui-btn-container">  <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button> </div></script> <script src="https://res.layui.com/layui/dist/layui.js" charset="utf-8"></script><!-- 注意:如果你直接复制所有代码到本地,上述js路径需要改成你本地的 --> <script>layui.use('table', function(){ var table = layui.table; table.render({  elem: '#test'  ,url:'/demo/table/user/'  ,toolbar: '#toolbarDemo'  ,cols: [[   {type:'radio'}   ,{field:'id', width:80, title: 'ID', sort: true}   ,{field:'username', width:80, title: '用户名'}   ,{field:'sex', width:80, title: '性别', sort: true}   ,{field:'city', width:80, title: '城市'}   ,{field:'sign', title: '签名', minWidth: 100}   ,{field:'experience', width:80, title: '积分', sort: true}   ,{field:'score', width:80, title: '评分', sort: true}   ,{field:'classify', width:80, title: '职业'}   ,{field:'wealth', width:135, title: '财富', sort: true}  ]]  ,page: true });  //头工具栏事件 table.on('toolbar(test)', function(obj){  var checkStatus = table.checkStatus(obj.config.id); //获取选中行状态  switch(obj.event){   case 'getCheckData':    var data = checkStatus.data; //获取选中行数据    layer.alert(JSON.stringify(data));   break;  }; });});</script></body>

实际需求实例

  • 点击 【选择】 按钮,出现弹框
  • 弹框里有数据表格
  • 点击圆圈为选中当前条数据
  • 点击弹框中【确认】把选中条数据带到主页面

实际代码实例

主页面代码(底,都为自动带出的输入框)

静态部分

<div><div><span>客户姓名:</span><form:input path="customerName" readonly="true"/><span onclick="onclick()" title="选择"><input id="selectCustomer"class="btn" type="button" value="选择" /></span></div><div><span>客户性别:</span><form:input path="customerSex" readonly="true"/></div><div><span>客户年龄:</span><form:input path="customerYears" readonly="true"/></div></div>

【选择】按钮的弹窗事件

function onclick(){ var width = window.screen.availWidth*0.8; var height = window.screen.availHeight*0.65; var iTop=(window.screen.availHeight-30-height)/2; var iLeft=(window.screen.availWidth-30-width)/2; var url="${xxx}/vvvv/rrrrr/getCustomerList"; //后端代码就不介绍了 window.open(url,'客户信息','height='+height+',width='+width+',top='+iTop+',left='+iLeft+', toolbar=no,menubar=no,scrollbars=yes, resizable=yes,location=no, status=no')} 

弹窗页面代码

弹窗页面中C静态部分

<table id="contentTable" class="layui-table"> <thead> <tr> <th></th> <th>客户姓名</th> <th>客户性别</th> <th>客户年龄</th> <th>...</th> </tr> </thead> <tbody> <c:forEach items="${page.list}" var="customerMain"> <tr> <td align="center"><input name="customerId" type="radio" value="${customerMain.id}"></td> <td>${customerMain.customerName}</td> <td>${customerMain.customerSex}</td> <td>${customerMain.customerYears}</td> <td class="hide" >${customerMain.....}</td> </tr> </c:forEach> </tbody></table>

弹框页面上-- == 单击单选圆圈的事件+双击行选中 ==

$(document).ready(function() { //双击行 即可执行函数(行数据被选中:radio为checked)  $("table tbody tr").dblclick(function(i) {  $(this).find("input[type='radio']").prop("checked", true)  });  //圆圈改变状态即可执行函数  $("#contentTable tbody tr input").change(function () {    var ischecked = $(this).prop("checked");    var index=$(this).parent().parent().index()    var tr=$("#contentTable tbody tr")    if(ischecked){      for(var i=0;i<tr.length;i++){        if(i!=index){          $("#contentTable tbody tr:eq("+i+") input").prop("checked",!ischecked);        }      }    }  })});

弹框页面上C选择好数据后带回主页面的函数

<script>//给弹框中【确认】按钮绑定事件function toSubmit(){//此方法在下方var data=getRowData();if(data==null){layer.alert("请先选择一位客户")return ;}window.opener.getCustomerData(data);//调用主页面上的方法,给主页面赋值,最下方有具体方法过程window.close();//关闭弹窗}//给弹框中【返回】按钮绑定事件function closed(){window.close();}//获取行对象function getRowData(){var row = null;//锁定行(循环遍历找到被选中的行)$("table tbody tr").each(function(){var radio = $(this).find("td").eq(0).find("input[type='radio']:checked").val();if(radio){row = $(this) ;}});//如果此行有数据则拼接if(row){var customerId = row.find("td").eq(0).find("input[type='radio']:checked").val();var customerName = row.find("td").eq(1).text();var customerSex = row.find("td").eq(2).text();var customerYears = row.find("td").eq(3).text();//拼接模板 $.trim() jQuery.trim()函数用于去除字符串两端的空白字符。该函数可以去除字符串开始和末尾两端的空白字符(直到遇到第一个非空白字符串为止)。它会清除包括换行符、空格、制表符等常见的空白字符。var data = "[{\"customerId\":\""+$.trim(customerId)  +"\",\"customerName\":\""+$.trim(customerName)    +"\",\"customerSex\":\""+$.trim(customerSex)    +"\",\"customerYears\":\""+$.trim(customerYears)  +"\"}]";}return data ;}</script>

调用主页面上的给主页面赋值的方法

<script>function getCustomerData(data){var json = JSON.parse(data);$("#customerId").val(json[0].customerId);  $("#customerName").val(json[0].customerName);  $("#customerSex").val(json[0].customerSex);  $("#customerYears").val(json[0].customerYears);  ....}</script>

到此这篇关于Layui弹框中数据表格中可双击选择一条数据的实现的文章就介绍到这了,更多相关Layui弹框双击数据内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!


  • 上一条:
    angular组件间传值测试的方法详解
    下一条:
    Vue SSR 即时编译技术的实现
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 使用 Alpine.js 排序插件对元素进行排序(0个评论)
    • 在js中使用jszip + file-saver实现批量下载OSS文件功能示例(0个评论)
    • 在vue中实现父页面按钮显示子组件中的el-dialog效果(0个评论)
    • 使用mock-server实现模拟接口对接流程步骤(0个评论)
    • vue项目打包程序实现把项目打包成一个exe可执行程序(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-03
    • 2018-04
    • 2018-05
    • 2018-06
    • 2018-09
    • 2018-11
    • 2018-12
    • 2019-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2021-04
    • 2021-05
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客