I have a large table that spans multiple horizontal screen widths on a normal display because it has many columns. These columns are grouped in column groups, which can be quite large. Now I have the problem that when looking at the values in the table, it is not directly visible in which column group a cell is because the only visual guide is in the column header. Since in my use case there are multiple column groups with similar information, it is quite hard to tell which cell belongs to which column group and/or column.
Let's say we have the following column defs:
var columnDefs = {
headerName: "Sports Results 2016",
marryChildren: true,
children: [
{headerName: "Gold", field: "gold", width: 75},
{headerName: "Silver", field: "silver", width: 75},
{headerName: "Bronze", field: "bronze", width: 75},
{headerName: "Total", field: "total", width: 75}
]
},
{
headerName: "Sports Results 2017",
marryChildren: true,
children: [
{headerName: "Gold", field: "gold", width: 75},
{headerName: "Silver", field: "silver", width: 75},
{headerName: "Bronze", field: "bronze", width: 75},
{headerName: "Total", field: "total", width: 75}
]
},
{
headerName: "Sports Results 2018",
marryChildren: true,
children: [
{headerName: "Gold", field: "gold", width: 75},
{headerName: "Silver", field: "silver", width: 75},
{headerName: "Bronze", field: "bronze", width: 75},
{headerName: "Total", field: "total", width: 75}
]
};
Now when I look at a row at the bottom, it's kind of hard to compare the values between column groups. And it gets worse if the units differ from column to column (m, kg, sec, etc) and are only written in the header.
Here the plunkr for the code with the resulting layout.
Now I am searching for a way to add a some styling to the column group, so there is a visual distinction within the data table between different column groups. Something like vertical lines between column groups. Like in the following picture (color red chosen for demonstration, in the end black or a similar style as in the header row would be nice):
I tried to find a easy way to get such a layout in ag-grid, but I only found ways to add a style only to the headers or every cell in the column group. Does someone have some experience with such a situation or have an idea how to make the distinction more clear?
Help is appreciated!
I found a solution that works, but uses a undocumented property of the cell object used during the cell customization. And it only works with cellStyle and not with cellClass or cellClassRule. (See section Cell Styles -> cell style in Ag grid documentation)
Basically you can add a default column definition to the grid options that sets the cell style of each column and therefore of each cell to a function. This function paints a border on the left side of a cell, if the column of the cell is the first displayed child of it's parent.
var defaultColumnDefs = {
cellStyle: (cell) => isFirstColumn(cell) ? {'border-left': '2px solid red'} : null
};
var gridOptions = {
...
columnDefs: columnDefs,
defaultColDef: defaultColumnDefs
};
function inFirstColumn(cell) {
// cell.column is not a documented property!!
return cell.column.colId === cell.column.parent.displayedChildren[0].colId;
}
Here is the updated Plnkr.
The demo of the solution I would do:
updated plunker
What I did is I used the cellStyle in column definition and gave it a solid red border :
cellStyle: {'border-left': '5px solid red'}}
Or to put that together in appropriate element:
{
headerName: "Sports Results 2016",
marryChildren: true,
children: [
{headerName: "Gold", field: "gold", width: 75, filter: 'agNumberColumnFilter',
cellStyle: {'border-left': '5px solid red'}},
{headerName: "Silver", field: "silver", width: 75, filter: 'agNumberColumnFilter'},
{headerName: "Bronze", field: "bronze", width: 75, filter: 'agNumberColumnFilter'},
{headerName: "Total", field: "total", width: 75, filter: 'agNumberColumnFilter'}
]
}
Simply do that for all the cells columns you wanna. The only difference is that there are breaks in the lines, if that does not bother you, solution conveys the message of splitting up a table at certain rows and is really simple to do.
You might also wanna to separate CSS from your cell definitions and you can do that with cellClass property in the definition.
More info on both approaches here:
ag-grid Cell Styling
Since you told you already know how to style headers, add the same to headers if you wanna line to go from header to bottom. If you wanna full line, however, I can only think of making a really thin cell with all borders and background red to mimic the vertical line. Then you need to add that in front of every element you wanna to have the line on left side.
Maybe something like this:
{
headerName: "Sports Results 2017",
marryChildren: true,
children: [
{width: 1,
cellStyle: {
'padding': 0, background: 'red', width: '3px'}
},
{headerName: "Gold", field: "gold", width: 75, filter: 'agNumberColumnFilter'},
{headerName: "Silver", field: "silver", width: 75, filter: 'agNumberColumnFilter'},
{headerName: "Bronze", field: "bronze", width: 75, filter: 'agNumberColumnFilter'},
{headerName: "Total", field: "total", width: 75, filter: 'agNumberColumnFilter'}
]
},
Related
I am working on grid using AgGridReact and enabling rowGrouping by setting it to true. However, it only adds the column "Group" but no actual grouping happens
const columnDef = [
{field: "module", width: 120, rowGroup: true, hide: true},
{headerName: "Ref Name", field: "referenceName", width: 120, },
{headerName: "ID", field: "referenceId", width: 120, }
]
const gridOptions = {
columnDefs: columnDef,
animateRows: true,
defaultColDef: {
sortable: true,
resizable: true,
enableRowGroup: true
},
rowData: mediaUsage
}
As you can see in the below image, it only adds GROUP column but no actual label appears
It should be group by Module column which is below
I've watched multiple tutorials and it's the rowGroup property that will display the grouping, but it doesnt in my case. Hope anyone could help. Thanks!
I'm trying to display the data grid with all columns should take equal width to fit the parent component. but there is a blank space at the end which I'm not able to remove and cannot make columns to take auto width to fit page width.
can anyone please help me on this
https://codesandbox.io/s/musing-montalcini-tn04y
Include flex value for each column level like below,
const columns = [
{ field: "id", headerName: "ID", flex: 1 },
{ field: "firstName", headerName: "First name", flex: 1 },
{ field: "lastName", headerName: "Last name", flex: 1 },
{
field: "age",
headerName: "Age",
type: "number",
flex: 1
},
{
field: "fullName",
headerName: "Full name",
description: "This column has a value getter and is not sortable.",
sortable: false,
flex: 1,
valueGetter: (params) =>
`${params.getValue(params.id, "firstName") || ""} ${
params.getValue(params.id, "lastName") || ""
}`
}
];
codesandbox - https://codesandbox.io/s/dry-https-7pp93?file=/src/App.js:266-850
in my case, I include flex as well as minWidth property which helps the table show full data in SM (small) and (XS) extra small devices.
const columns = [
{field: "id", hide: true},
{field: "transaction", headerName: "Transactions", minWidth: 330, flex: 1},
{field: "date", headerName: "Date", minWidth: 100, flex: 1},
{field: "type", headerName: "Type", minWidth: 100, flex: 1},
{field: "price", headerName: "Price", minWidth: 110, flex: 1},
{field: "gas", headerName: "Gas", minWidth: 110, flex: 1},
{field: "total", headerName: "Total", minWidth: 110, flex: 1} ]
I am using ag-grid-react and would like to sortable columns in it.
When I put enableSorting=true on , it enables sorting on all the columns. I don't want all columns to be sortable and instead would like sorting to be driven from the columnDef.(E.g sort the column only if its respective columnDef contain sortable=true.
Attaching the sample and columnDef with codesandbox link
<AgGridReact
columnDefs={this.state.columnDefs}
enableColResize={true}
onGridReady={this.onGridReady.bind(this)}
rowData={this.state.rowData}
enableSorting={true}
/>
columnDefs: [
{
headerName: "Athlete",
field: "athlete",
width: 150,
suppressSizeToFit: true
},
{
headerName: "Age",
field: "age",
width: 90,
minWidth: 50,
maxWidth: 100
},
{
headerName: "Country",
field: "country",
width: 120
},
{
headerName: "Year",
field: "year",
width: 90
},
{
headerName: "Date",
field: "date",
width: 110
},
{
headerName: "Sport",
field: "sport",
width: 110
},
{
headerName: "Gold",
field: "gold",
width: 100
},
{
headerName: "Silver",
field: "silver",
width: 100
},
{
headerName: "Bronze",
field: "bronze",
width: 100
},
{
headerName: "Total",
field: "total",
width: 100
}
],
Please refer : https://codesandbox.io/s/jz317zpo43
Please suggest what needs to be added.
Please check it here. maybe it will help. https://www.ag-grid.com/javascript-grid-sorting/
only enable sorting for a particular column
gridOptions: {
// enable sorting on name and age columns only
columnDefs: [
{field: 'name', sortable: true},
{field: 'age', sortable: true},
{field: 'address'},
]
}
I was able to achieve it by upgrading the ag-grid-react and ag-grid-community versions to 20.0.0. Refer AG-644 in https://www.ag-grid.com/ag-grid-changelog/
I have a below column definitions:
{headerName: 'Country Group - No Renderer', field:'year', showRowGroup: 'year'},
{headerName: "Country", field: "country", width: 120, rowGroup: true},
{headerName: "Year", field: "year", width: 90, rowGroup: true},
{headerName: "Gold", field: "gold", width: 100, aggFunc: 'sum'},
{headerName: "Silver", field: "silver", width: 100, aggFunc: 'sum'},
{headerName: "Bronze", field: "bronze", width: 100, aggFunc: 'sum'}
Is there any way to show 'Country Group - No Renderer' column left to the auto grouped column along with aggregated columns when grid is in pivot mode?
Is there any possibility that we can achieve this with InMemoryModel?
Note: I am beginner to ag-grid and just now started playing with it.
I use ag-grid with Angular 1.5 and I have defined 5 columns in the following format in an array:
[
{headerName: 'Username', width: 150, field: 'username'},
{headerName: 'Full Name', width: 150, field: 'full_name'},
{headerName: 'Location', width: 150, field: 'location'},
{headerName: 'Email', width: 150, field: 'email'},
{headerName: 'Check In Comment', width: 370, field: 'comment'},
];
The rendering shows 6 columns with the right-most one being blank. I've tried everything, but I cannot still remove the blank column. See the :
gridOptions:
options = {
rowData: dataArray,
rowModelType: 'pagination',
columnDefs: getColumnDefs(),
enableColResize: true,
enableSorting: false,
enableFilter: true,
rowHeight: 25,
angularCompileRows: true,
suppressRowClickSelection: true
};
The html code:
<div style="width: 100%;"
ag-grid="vm.gridOptions"
class="ag-fresh ag-basic">
</div>
I wonder how to remove the blank column right to the 'Check In Comment' column. any ideas?
Have you tried gridOptions.api.sizeColumnsToFit() ?
Source (7min 50sec)
extra column is there even if we use that api gridOptions.api.sizeColumnsToFit().