悟生慧

 

agGrid使用Id和ParentId实现层级列表显示层级列表显示

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>agGrid层级列表展示</title>
    <!-- 引入agGrid样式 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ag-grid-community@30.0.0/styles/ag-grid.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ag-grid-community@30.0.0/styles/ag-theme-alpine.css">
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin: 0;
            padding: 20px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
            padding: 24px;
        }
        .header {
            text-align: center;
            margin-bottom: 30px;
        }
        .header h1 {
            color: #2c3e50;
            margin-bottom: 10px;
        }
        .description {
            color: #7f8c8d;
            text-align: center;
            margin-bottom: 30px;
            line-height: 1.6;
        }
        #myGrid {
            height: 500px;
            width: 100%;
        }
        .controls {
            margin-bottom: 20px;
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
        }
        .btn {
            padding: 8px 16px;
            background: #3498db;
            color: white;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        .btn:hover {
            background: #2980b9;
            transform: translateY(-2px);
        }
        .info-panel {
            background: #f8f9fa;
            padding: 15px;
            border-radius: 8px;
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>agGrid层级列表展示</h1>
            <p class="description">基于id和Parentid字段实现树形数据结构的层级展示</p>
        </div>
        
        <div class="controls">
            <button class="btn" onclick="expandAll()">展开所有</button>
            <button class="btn" onclick="collapseAll()">折叠所有</button>
            <button class="btn" onclick="showDataStructure()">查看数据结构</button>
        </div>
        
        <div id="myGrid" class="ag-theme-alpine"></div>
        
        <div class="info-panel">
            <h3>实现要点:</h3>
            <ul>
                <li>设置 treeData: true 启用树形数据模式</li>
                <li>使用 getDataPath 函数定义数据层级路径</li>
                <li>数据需要包含id和parentId字段建立关系</li>
            </ul>
        </div>
    </div>

    <!-- 引入agGrid库     <script src="ag-grid-enterprise.min.js"></script>-->

    <script src="https://blog-static.cnblogs.com/files/wuhuisheng/ag-grid-enterprise.min.js?t=1763367200&download=true"></script>
    
    <script>
        // 树形结构数据示例
        const rowData = [
            { id: 1, name: '张总', jobTitle: 'CEO', parentId: null },
            { id: 2, name: '李经理', jobTitle: '技术总监', parentId: 1 },
            { id: 3, name: '王经理', jobTitle: '产品总监', parentId: 1 },
            { id: 4, name: '赵工程师', jobTitle: '前端开发', parentId: 2 },
            { id: 5, name: '钱工程师', jobTitle: '后端开发', parentId: 2 },
            { id: 6, name: '孙设计师', jobTitle: 'UI设计师', parentId: 3 },
            { id: 7, name: '周工程师', jobTitle: '全栈开发', parentId: 2 },
            { id: 8, name: '吴产品', jobTitle: '产品经理', parentId: 3 }
        ];

        const gridOptions = {
            // 启用树形数据
            treeData: true,
            
            // 指定子节点字段
            groupDefaultExpanded: 2,

            getDataPath: (params) => {
                debugger;
                const path = [];
                let current = params;
                 while (current) {
                    path.unshift(current.jobTitle);  // 通过节点名称构建路径
                 current = rowData.find(item => item.id === current.parentId);  // 查找父节点
                  }
                 return path;
            },
            
            // 列定义
            columnDefs: [
                { 
                    field: 'name', 
                    headerName: '姓名',
                    width: 150
                },
                { 
                    field: 'jobTitle', 
                    headerName: '职位',
                    width: 200
                },
                { 
                    field: 'id', 
                    headerName: 'ID',
                    width: 100
                }
            ],

            defaultColDef: { flex: 1, sortable: false, filter: false, resizable: true, suppressMenu: true },
            rowSelection: 'single',
            animateRows: false,
            rowData: rowData,
            // 默认展开级别
            autoGroupColumnDef: {
                headerName: '组织架构',
                minWidth: 300
            },

            enableRangeSelection: false,
            suppressContextMenu: true,            
            // 启用动画
            animateRows: true,            
            autoGroupColumnDef: {
              headerName: '职位',
              minWidth: 150,
              suppressSizeToFit: true,
              cellRendererParams: {
              suppressCount: true,
               },
           },
        };

        // 初始化网格
        document.addEventListener('DOMContentLoaded', () => {
            const gridDiv = document.querySelector('#myGrid');
            new agGrid.Grid(gridDiv, gridOptions);
            
                        
        });

       
    </script>
</body>
</html>

  

posted on 2025-11-17 16:16  悟生慧  阅读(4)  评论(0)    收藏  举报

导航