layui和vue父子级页面及操作

最近在老项目里面添加一些页面,项目太老只能在原有的项目基础和插件上添加代码

html

html 复制代码
//表格        
<table id="dataTable">
   <thead>
      <tr><th>序号</th><th>名称</th><th></th></tr>
   </thead>
   <tbody></tbody>
</table>
<div id="pagination"></div>//layui分页

js

javascript 复制代码
<script>
    function toggleChildRow(rowId) {
        console.log(rowId,'rowId')
        var childRow = document.querySelector('.child-row[data-parent-id="'+rowId+'"]');
        var btn = document.querySelector('.toggle-btn[data-row="'+rowId+'"]');
        if(childRow.style.display === 'none') {
            childRow.style.display = 'table-row';
            btn.textContent = '▶';//展开
        } else {
            childRow.style.display = 'none';
            btn.textContent = '▼';//折叠
        }
    }
   function viewChildRow(row){//查看操作
        vue.rptCoProfileAccountList = []
        vue.rptCoProfileAccountList.push(row)
        vue.tlttt = '查看'
    }
  var vue = new Vue({//使用vue
        el: '#vue',
        data: {
           },
        computed: {

        },
        created(){
            this.getlist()//列表接口
        },
        methods: {
             getlist:function (){
                layui.table.render({
                    elem: '#pagination'//分页id
                    , url:'/api/rptCoProAccount/list'
                    , totalRow: true
                    ,id:'idTest'
                    , cols: []
                    , page: true
                    ,done: function(res, curr, count) {
                        vue.getsylist(res.data)//数据进行渲染
                    }
                });
            },
            getsylist:function(mockData){//数据渲染到页面
                var tbody = document.querySelector('#dataTable tbody');
                tbody.innerHTML = '';

                mockData.forEach((item,index) => {//数据循环
                    var parentRow = document.createElement('tr');
                    parentRow.className = 'parent-row';
                    parentRow.dataset.id = item.orgId;

                    var toggleCell = document.createElement('td');
                    toggleCell.innerHTML = '<button class="toggle-btn" data-                          row="'+item.orgId+'" onclick="toggleChildRow(\''+item.orgId+'\')">▼</button>';//展开按钮

                    var idCell = document.createElement('td');
                    idCell.textContent = index + 1;//序号

                    var nameCell = document.createElement('td');
                    nameCell.textContent = item.orgName;//名称

                    parentRow.append(idCell, nameCell, toggleCell);
                    tbody.appendChild(parentRow);
                      //子数据
                    if(item.children && item.children.length > 0) {
                        var childRow = document.createElement('tr');
                        childRow.className = 'child-row';
                        childRow.dataset.parentId = item.orgId;
                        childRow.style.display = 'none';

                        var containerCell = document.createElement('td');
                        containerCell.colSpan = 4;

                        var childTable = document.createElement('table');
                        childTable.className = 'child-table';

                        var thead = document.createElement('thead');
                        thead.innerHTML = '<tr><th>名称</th><th>开户行</th><th>开户行账号</th><th>操作</th></tr>';

                        var childTbody = document.createElement('tbody');
                        item.children.forEach(function(child) {
                            var row = document.createElement('tr');
                            row.innerHTML = '' +
                                '<td>' + child.accountName + '</td>' +
                                '<td>' + child.openingBank + '</td>'+
                                '<td>' + child.accountNum + '</td>'+
                                '<td> <a class="layui-btn layui-btn-xs" onclick="viewChildRow('+JSON.stringify(child).replace(/"/g, '&quot;')+')" lay-event="resetPwd">\n' +
                                '                <img src="../../../../assets/img/look.png" alt=""/>\n' +
                                '                <span>查看</span>\n' +
                                '            </a>\n' +
                                '            <a class="layui-btn layui-btn-xs" onclick="editChildRow('+JSON.stringify(child).replace(/"/g, '&quot;')+')" lay-event="edit">\n' +
                                '                <img src="../../../../assets/img/edit.png" alt=""/>\n' +
                                '                <span>修改</span>\n' +
                                '            </a>\n' +
                                '            <a class="layui-btn layui-btn-xs" onclick="scChildRow('+JSON.stringify(child).replace(/"/g, '&quot;')+')" lay-event="del">\n' +
                                '                <img src="../../../../assets/img/delete.png" alt="">\n' +
                                '                <span>删除</span>\n' +
                                '            </a></td>'
                            ;
                            childTbody.appendChild(row);
                        });

                        childTable.append(thead, childTbody);
                        containerCell.appendChild(childTable);
                        childRow.appendChild(containerCell);
                        tbody.appendChild(childRow);
                    }
                });
            },
        }
  })
</script>
相关推荐
不可能掉发10 小时前
Env Guard:让浏览器一眼分清生产、测试和开发环境
前端·javascript·chrome·测试工具·html·开源软件·个人开发
扯蛋43810 小时前
langchain1.x 时代的记忆系统 (二)
javascript·llm·agent
烂蜻蜓11 小时前
Node.js入门教程(一):初识Node.js——服务端JavaScript的诞生与特点
开发语言·javascript·node.js
嘟嘟071711 小时前
从手写 HashRouter 到 React Router:逐层拆解 SPA 前端路由的完整实现
前端·javascript
生戎马 平安京策12 小时前
只学一点点:我的技术学习策略
前端·javascript·react.js
__zRainy__12 小时前
React之reduce实现compose和中间件
javascript·react.js·ecmascript
得物技术14 小时前
实战从零开始构建一个Coding Agent:Violin |得物技术
javascript·架构·llm
JiaLin_Denny15 小时前
Vue3中ref和reactive用法与双向数据绑定
前端·javascript·vue.js
无人生还15 小时前
从 Vue3 到 React · 快速上手系列第 12 篇:并发特性与性能优化
前端·vue.js·react.js
wear工程师15 小时前
React 的 key 为什么不能随手用 index:一插入列表,状态就串了
javascript·react.js