如何在AngularJS中显示AG Grid的侧边按钮(列)?
Hey there! Let's troubleshoot why that AG Grid side button (column) isn't showing up in your AngularJS project, even after copying the official example. I've run into similar snags before, so here are the most common fixes to work through:
First, make sure you've got all the required AG Grid files in place and loaded in the right order:
- Include the core AG Grid CSS (
ag-grid.min.css) and your chosen theme CSS (likeag-theme-alpine.min.css) in yourindex.html - Load the core AG Grid JS (
ag-grid.min.js) before the AngularJS wrapper (ag-grid-angular.min.js)
Example of the correct include order:
<link rel="stylesheet" href="path/to/ag-grid.min.css"> <link rel="stylesheet" href="path/to/ag-theme-alpine.min.css"> <script src="path/to/ag-grid.min.js"></script> <script src="path/to/ag-grid-angular.min.js"></script>
It's easy to miss a key detail in the column setup. Ensure your button column has a proper cellTemplate (for AngularJS) or cellRenderer that actually renders the button. Also, use grid.appScope to access your controller's functions from the template—this is a common gotcha!
Example of a working button column definition:
$scope.columnDefs = [ // ... your other columns { headerName: 'Actions', width: 130, cellTemplate: `<button ng-click="grid.appScope.handleEdit(params)" class="btn btn-sm btn-primary">Edit</button>` } ];
In your HTML, make sure the ag-grid directive has all necessary attributes, including a valid theme class (this is required for grid styling to render correctly):
<div ag-grid="gridOptions" class="ag-theme-alpine" style="height: 500px;"></div>
And don't forget to link your column definitions to the grid options in your controller:
$scope.gridOptions = { columnDefs: $scope.columnDefs, rowData: $scope.rowData, // ... other grid configs };
If your row data comes from an async API call, the grid might finish initializing before the data is ready, which can break column rendering. Use gridOptions.api.setRowData() to update the grid once your data is fetched:
$http.get('/api/your-data').then(function(response) { $scope.rowData = response.data; // Update grid with new data $scope.gridOptions.api.setRowData($scope.rowData); });
Sometimes custom CSS in your project can accidentally hide the button (e.g., display: none or opacity: 0). Use your browser's dev tools to inspect the button element—if it's present in the DOM but not visible, you'll need to adjust your styles to avoid overriding AG Grid's default classes.
Don't forget to inject the agGrid module into your AngularJS app—this is a super common oversight that breaks the entire grid:
angular.module('yourAppName', ['agGrid']);
If you've gone through all these steps and it's still not working, try creating a minimal, isolated test case (just the grid and button column) to rule out conflicts from other parts of your project.
内容的提问来源于stack exchange,提问作者K. Santos

