ag-grid 学习

项目要将 angular 从 1.5升级到 5,ui-grid 在 5 中并不支持,所以为了替换 ui-grid ,来学习了 ag-grid 。

简单来说,2 者相差并不大,使用方式也大致雷同,这里用 js 直观的记录一下:

<html>
<head>
    <!-- reference the ag-Grid library-->
    <!--<script src="https://cdnjs.cloudflare.com/ajax/libs/ag-grid/10.1.0/ag-grid.js"></script>-->
    <script src="ag-grid.js"></script>
    <style>
        .orange {
            color: orange;
        }
    </style>
</head>
<body>
    <h1>Simple ag-Grid Example</h1>

    <!-- the div ag-Grid will use to render it's data -->
    <div id="myGrid" style="height: 200px;width:700px" class="ag-fresh"></div>
    
    <p>        
        <button type='button' onclick="deselectAllFnc()">clear selection</button>
        <button type='button' onclick="selectAllFnc()">select all</button>
    </p>
    <script>
        // row data ,行内数据
        var rowData = [
            {name: "Toyota", model: "Celica", price: 35000,operation: 'a', folder: true, children: [{name:'3x', model:'sss', price:37000}]},
            {name: "Ford", model: "Mondeo", price: 32000,folder: true, open: true, children: [{name:'test', model:'test', price:100}]},
            {name: "Porsche", model: "Boxter", price: 72000}
        ]
        // column definitions,表格列属性
        var columnDefs = [
            {
                headerName: 'Name',
                field: 'name',
                width: 200,
                enableRowGroup: true,
                checkboxSelection: function (params) {
                    // we put checkbox on the name if we are not doing grouping
                    return params.columnApi.getRowGroupColumns().length === 0;
                },
                headerCheckboxSelection: function (params) {
                    // we put checkbox on the name if we are not doing grouping
                    return params.columnApi.getRowGroupColumns().length === 0;
                },
                headerCheckboxSelectionFilteredOnly: true,
                cellRenderer: 'group',
                cellRendererParams: {
                    innerRenderer: function (params) { return params.data.name; },
                    suppressCount: true
                }
            },
            {headerName: "Model", field: "model"},
            {headerName: "Price", field: "price"},        
            {headerName: "Operation", field: "operation", cellRenderer: function(params) { return '<button οnclick="btnClick(1);">click1</button> <button οnclick="btnClick(2);">click2</button>'}}    
        ]
        // Grid Definition 
        // let the grid know which columns and what data to use
        // 表格初始化配置
        var gridOptions = {
            // PROPERTIES - object properties, myRowData and myColDefs are created somewhere in your application
            //列标题设置
            columnDefs: columnDefs,        
            //行内数据插入
            rowData: rowData,    
            animateRows: true,
            
            // PROPERTIES - simple boolean / string / number properties    
            //是否支持列宽调整
            enableColResize: true,
            //行高设置
            rowHeight: 26,
            //单行选中,"multiple" 多选(ctrl),"single" 单选
            rowSelection: 'multiple',
            // enable sorting ,是否支持排序
            enableSorting: true,
            // enable filtering ,是否可以筛选
            enableFilter: true,
            //默认筛选字段
            //quickFilterText: 's',
            //选中组可选中组下子节点
            //groupSelectsChildren: true,
            //true的话,点击行内不会进行行选择
            suppressRowClickSelection: true,
            //阻止列拖拽移动
            //suppressMovableColumns: true,
            //阻止列拖拽出表格,列移除
            suppressDragLeaveHidesColumns: true,
            //给整行加样式,
            getRowClass: function(params) { return (params.data.name === 'Ford') ? "orange" : null; },
            
            // EVENTS - add event callback handlers
            onModelUpdated: function(event) { console.log('model updated'); },
            //行内点击触发事件
            onRowClicked: function(event) { 
                //event.data 选中的行内数据,event.event 为鼠标事件,等其他。可以log自己看一下
                console.log('a row was clicked', event); 
            },
            //列宽度改变触发
            onColumnResized: function(event) { console.log('a column was resized'); },
            //表格加载完成触发
            onGridReady: function(event) { console.log('the grid is now ready'); },
            //单元格点击触发
            onCellClicked: function(event) { console.log('a cell was cilcked'); },
            //单元格双击触发
            onCellDoubleClicked: function(event) { console.log('a cell was dbcilcked'); },
            
            onCellContextMenu: function(event) { },
            onCellValueChanged: function(event) { },
            onCellFocused: function(event) { },
            onRowSelected: function(event) { },
            onSelectionChanged: function(event) { },
            onBeforeFilterChanged: function(event) { },
            onAfterFilterChanged: function(event) { },
            onFilterModified: function(event) { },
            onBeforeSortChanged: function(event) { },
            onAfterSortChanged: function(event) { },
            onVirtualRowRemoved: function(event) { },
            
            // CALLBACKS
            isScrollLag: function() { return false; },
            
            
            getNodeChildDetails: function(file) {
                if (file.folder) {
                    return {
                        group: true,
                        children: file.children,
                        expanded: file.open
                    };
                } else {
                    return null;
                }
            }            
        }
        
        //取消选中状态
        function deselectAllFnc() {
            gridOptions.api.deselectAll();
        }
        //全选
        function selectAllFnc() {
            gridOptions.api.selectAll();
        }
        
        function btnClick(value) {
            console.log(value);
        }
        
        // wait for the document to be loaded, otherwise,
        // ag-Grid will not find the div in the document.
        document.addEventListener("DOMContentLoaded", function() {
            // lookup the container we want the Grid to use
            var eGridDiv = document.querySelector('#myGrid');

            // create the grid passing in the div to use together with the columns & data we want to use
            new agGrid.Grid(eGridDiv, gridOptions);
            
            
            // create handler function,增加监听事件
            function myRowClickedHandler(event) {
                console.log('the row was clicked');
            }
            // add the handler function
            gridOptions.api.addEventListener('rowClicked', myRowClickedHandler);
        });
    </script>
</body>
</html>

 

效果图:

转载于:https://www.cnblogs.com/guofan/p/8352407.html

ag-Grid Vue 是一个用于构建灵活的、高性能的数据表格和数据网格的 Vue 组件库。它是与 Vue 框架集成的 ag-Grid 社区版。使用 ag-Grid Vue,您可以轻松地在 Vue 应用程序中创建功能丰富的数据表格和网格,并利用 ag-Grid 提供的丰富功能和性能优势。 要在 Vue 应用程序中使用 ag-Grid Vue,您需要先安装 ag-Grid Vue 包。您可以使用 npm 或 yarn 进行安装: ```bash npm install --save ag-grid-vue # 或 yarn add ag-grid-vue ``` 安装完成后,您可以在 Vue 组件中引入并使用 ag-Grid Vue: ```vue <template> <ag-grid-vue :columnDefs="columnDefs" :rowData="rowData" class="ag-theme-alpine" ></ag-grid-vue> </template> <script> import { AgGridVue } from 'ag-grid-vue'; export default { components: { AgGridVue, }, data() { return { columnDefs: [...], // 列定义 rowData: [...], // 行数据 }; }, }; </script> <style> @import '~ag-grid-community/dist/styles/ag-grid.css'; @import '~ag-grid-community/dist/styles/ag-theme-alpine.css'; </style> ``` 在上面的示例中,您可以通过传递列定义(columnDefs)和行数据(rowData)来配置 ag-Grid Vue 组件。您还需要为 ag-Grid 使用的主题样式添加对应的 CSS。 只是一个简单的示例,您可以根据您的需求和数据结构进行更多的配置和自定义。您可以参考 ag-Grid Vue 的官方文档以获取更多详细信息和示例代码。希望这可以帮助您开始使用 ag-Grid Vue!如果有任何其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值