how do I make a subgrid inside an ag-grid? - ag-grid

I would like to be able to open an other ag-grid (subgrid) in my main ag-grid after clicking a row button. However, after searching for several hours in forums and ag-grid docs, I can't seem to find a way of making the subgrid appear. Here is an example of how I would like my grid to behave:
subgrid desired behaviour example with dhtmlxGrid
Is there any way this can be done with ag-grid? How?

I suppose you are talking about something like this. I found it under the section Full Width Rows & Master Detail and scrolling all the way to the bottom

I supose you want to use a nested ag grid
https://www.ag-grid.com/javascript-grid-master-detail-detail-grids/
This small piece is what u need to render a grid inside your grid
<AgGridReact
modules={[
ClientSideRowModelModule,
MasterDetailModule,
MenuModule,
ColumnsToolPanelModule,
]}
masterDetail={true}
detailCellRendererParams={{
detailGridOptions: {
columnDefs: [
{ field: 'callId' },
{ field: 'direction' },
{
field: 'number',
minWidth: 150,
},
{
field: 'duration',
valueFormatter: "x.toLocaleString() + 's'",
},
{
field: 'switchCode',
minWidth: 150,
},
],
defaultColDef: { flex: 1 },
},
getDetailRowData: function (params) {
//callRecords here is any data u want to render
params.successCallback(params.data.callRecords);
},
}}

Related

AG-Grid FillHandle Increment Value Cross Row Groups

Example at https://plnkr.co/edit/0CPdb8dXPCafCJjI
const gridOptions = {
columnDefs: [
{ field: 'athlete',
minWidth: 150,
rowGroup: true,
hide: true, },
{ field: 'year', maxWidth: 90 },
],
defaultColDef: {
flex: 1,
minWidth: 100,
editable: true,
},
enableRangeSelection: true,
enableFillHandle: true,
fillHandleDirection: "y",
groupDisplayType: "groupRows",
groupDefaultExpanded: 1,
};
Grid has row groups. Selecting 2004, holding down "alt" and dragging down increases the value in column "Year" as intended. The issue comes when dragging over to a new group. I would like first row for "Natalie Coughlin" to be 2006, but it seems like AG grid is counting group row header as a regular row.
Anyone know how to accomplish this?

Ag-grid Master/Detail grid Independent pagination at Master and Detail

if number of rows of detail grid are more, it is tedious to scroll through all rows,
Is it possible to add pagination at Master and Detail independently?
I have gone through the below link of ag-grid documentation but did not find a way or mention of pagination.
https://www.ag-grid.com/javascript-grid-master-detail/
You just need to add pagination related properties pagination : true to the detail grid options. Here is what your detailGridOptions would look like -
this.detailCellRendererParams = {
detailGridOptions: {
columnDefs: [
{ field: 'callId' },
{ field: 'direction' },
{
field: 'number',
minWidth: 150,
},
{
field: 'duration',
valueFormatter: "x.toLocaleString() + 's'",
},
{
field: 'switchCode',
minWidth: 150,
},
],
defaultColDef: { flex: 1 },
pagination : true, //set pagination to true
paginationPageSize: 5 // define page size, default is 100
},
getDetailRowData: function(params) {
params.successCallback(params.data.callRecords);
},
};
}
Attaching the example screenshot -
Used this example

How can I add particular component(like Input component) in Ag-grid by Row wise or how can i remove component by Row in Ag-grid?

How can I add a particular component (like Checkbox, Input , select, dropdown component) in a particular row? Whenever I add a component into columns, it comes in every row. How can I remove the component from a particular row?
this.columnDefs1 = [
{
headerName: "M1",
field: "m1",
width: 90,
cellRendererFramework: InputComponent,
controlData: {
type: 'text'
},
},
]
this.rowData1 = [
{
m1: "50",
},
{
m1: "30",
},
{
m1: "",
//If I don't want input component in this row, how can I remove?
//I have tried by not giving m1
//also but its coming in whole column or every row.
},
{
m1: "20",
},
]
You can see in the linked image for reference, I don't want the last 4 select components, but I need those rows blank:
You can use colDef.cellRendererSelector which is a function that selects the renderer based on the row data
headerName: "M1",
field: "m1",
width: 90,
cellRendererSelector: function (params) {
if (params.data.m1) {
return InputComponent;
} else {
return null;
}
},
controlData: {
type: 'text'
}
You can read more about it here - https://www.ag-grid.com/javascript-grid-cell-rendering/#many-renderers-one-column

Editable and non-editable some ag-grid cells dynamically

I have to implement editable/non-editable of the corresponding cells in a particular row depending on datatype selection. When we select datatype="NUMERIC" then it should be editable particular that cell in a row only under Min and Max column instead of full column.
Example
```
$scope.gridOptions.onCellValueChanged = function(event) {
if (event.colDef.field === 'validation_type') {
if (event.newValue.name === 'NUMERIC') {
event.columnApi.getColumn('min_value').editable = true;
}
}
}
```
Then it allow all cells of that column editable. But as per my requirement it should be editable only one particular cell. Please suggest.
Screenshots:
The easiest place to do this is in your column definitions:
const columnDefs = [
// ...
{
headerName: 'Data Type',
field: 'validation_type',
},
{
headerName: 'min',
field: 'min_value',
editable: function(params) {
// allow `min_value` cell to be edited for rows with correct `validation_type`
return params.node.data.validation_type === 'NUMERIC';
},
},
{
headerName: 'max',
field: 'max_value',
editable: function(params) {
return params.node.data.validation_type === 'NUMERIC';
},
},
// ...
];

How to hide column in AG-Grid?

How to hide the column in AG-Grid and also it should not be displayed in Tool Panel...
var columnDefs = [{ headerName: "Stone_ID", field: "Stone_ID", width: 100, hide: "true" }]
You can set the column property of suppressToolPanel to true to achieve that.
var columnDefs = [
{
headerName: "Stone_ID",
field: "Stone_ID",
width: 100,
hide: true,
suppressToolPanel: true
}
]
If you are looking for dynamically show/hide columns follow below:
You can use either setColumnVisible or setColumnsVisible.
NOTE: those functions expect a colKey(s) which is related to the field you set in columnDefs.
columnDefs = [
{
headerName: "Name",
field: "name", // => that!
width: 150
},
{
headerName: "Last Name",
field: "last_name", // => that!
width: 150
},
// ...
];
When using setColumnVisible you can show/hide a single column
gridOptions.columnApi.setColumnVisible('name', false) //In that case we hide it
When using setColumnsVisible you can show/hide multiple columns
gridOptions.columnApi.setColumnsVisible(['name', 'last_name'], true) //In that case we show them
To do this programatically you can use:
Hide columns:
this.agGrid.columnApi.setColumnsVisible(["COL_ID_1", "COL_ID_2"], false);
Show columns:
this.agGrid.columnApi.setColumnsVisible(["COL_ID_1", "COL_ID_2"], true);
To do this for a whole column group you can use:
const group = this.columnApi.getColumnGroup("MY_GROUP");
group.children.forEach(child => this.columnApi.setColumnsVisible(child, false));
Posting late but It may helpful for others with some extra details:
If you want to hide your column on the grid you can use hide property to hide that column from the grid.
Only hide from grid colDef example:
colMainDef = [
{
headerName: 'Approved',
field: 'status',
hide: true
}]
But the same column will still be accessible on the side menu panel if you want to hide it from the side menu you can use one more property suppressColumnsToolPanel by making this true it will hide the column from the side menu.
Hide column from the grid as well as from side panel colDef example:
colMainDef = [
{
headerName: 'Approved',
field: 'status',
suppressColumnsToolPanel: true,
hide: true,
}]
Hope this will helps to hide/show columns as per your requirements.
var columnDefs = [{ headerName: "Stone_ID", field: "Stone_ID", width: 100, hide: true }]
{
...,
hide: true,
suppressColumnsToolPanel: true
}
In the package.json:
"dependencies": {
"#ag-grid-community/angular": "^24.1.0",
"#ag-grid-enterprise/all-modules": "^24.1.0",
...
}
hide: should get the value true, not the string "true" (like width: gets 100, not "100")
Just add "hide: true" in your column definition.
Ex:
var columnDefs = [{ headerName: "ABC", field: "XYZ", hide: true }]
hide column on load
{
headerName: 'ROE',
field: 'roe',
width: 100,
hide: true
},
based on selection you can show/hide it using
example
this.gridColumnApi.setColumnVisible('roe',true);
We can hide column dynamically in the following way using onColumnVisible listen event
const onColumnVisible = useCallback(params => {
if (params.source === 'toolPanelUi') {
const colId = params.column ? [ params.column.colId ] : params.columns.map(col => col.colId)
const value = columns.map(v => {
if (v.colId === colId.find(c => c === v.colId)) {
params.visible ? v.hide = false : v.hide = true
}
return v
})
if (value) {
setColumns(value)
}
}
}, [ gridParams, setColumns ])
<Grid onColumnVisible={onColumnVisible } />