ag-grid setColumnVisible not working - ag-grid

Is there something else I need to do to ensure gridOptions.columnApi.setColumnVisible does what it says on the tin? :-
This code is running at runtime on a rendered grid and should just hide the column instantly, but does nothing:-
gridOptions.columnApi.setColumnVisible("the col name", false);

Two possible causes of this:
The grid isn't ready when you attempt to hide the column. This probably isn't the case as you say the grid has been rendered, but it's worth checking
You're not using correct column identifier. This can be either a column id or Column object.
It's probably the latter - are you perhaps using the header name instead of the field/col id?
For example, if you have this:
var columnDefs = [
{headerName: "Athlete", field: "athlete", width: 200}
];
Then the first parameter would be 'athlete', not 'Athlete'.
You can additionally specify a colId to rule out any conflicts and then use this id in your api call:
var columnDefs = [
{headerName: "Athlete", field: "athlete", width: 200, colId: "athleteCol"}
];
But this isn't normally necessary.

In case this helps someone else - I was having a similar problem, but with the actual column method itself. So for my application at least, the setVisible method of the column doesn't work correctly, but the setColumnVisible method of the columnApi method does work:
// does NOT work correctly
this.columnApi.getColumn('name').setVisible(true);
// does work correctly
this.columnApi.setColumnVisible('name', true);

Referring to the Aurelia ag-grid example code:
I am hiding Columns at runtime as follows in the *.ts component's constructor:
this.gridOptions.onGridReady = () => {
...snip
this.columnApi = that.gridOptions.columnApi;
this.gridOptions.columnApi.setColumnVisible('REQCONTEXT', false);
}//onGridReady
Where 'REQCONTEXT' is the name of the columns to hide.

I've had the exact problem, solved by adding
setTimeout(() => {
this.grid.api.refreshView();
this.grid.api.sizeColumnsToFit();
}, 0)
after calling setColumnVisible

Make sure you got
(gridReady)="onGridReady($event)"
in the template.
I do that all the time!
Cheers!

Related

AG-Grid - How to increase row height dynamically?

This question is related to Ag-Grid row height on Angular 4 Project. Please see the below scenario:-
I have an Ag-Gird having 3 columns respectively:-
Id (resizable column from UI)
Name (resizable column from UI)
Address (resizable column from UI)
I do not have any limitations( like the limited number of character or words is allowed) on Address column. Users can type any number of characters or words they want to.
Issues:-
How to increase the row height, when Address column width is completely filled-up with words or when users press Enter or Shift + Enter?
How to adjust height automatically when users resize the Address column?
Please help me with these issues.
Thanks
There are multiple things to be taken care.
Have a look at the updated Stackblitz
Have cellClass: "cell-wrap-text" attribute in the ColDef for Address column and have the appropriate CSS
Handle columnResized event so that this.gridApi.resetRowHeights() can be called to adjust the height of the rows whenever the column is resized
Also handle cellEditingStopped event, so that when the data for the column is updated, the row height also gets updated accordingly.
onColumnResized() {
this.gridApi.resetRowHeights();
}
onCellEditingStopped() {
this.onColumnResized();
}
Provide autoHeight: true property in the defaultColDef
defaultColDef = { autoHeight: true };
Update:
provide cellEditor: 'agLargeTextCellEditor' if you want to have textarea like control for this field.
Check this StackBlitz
I was facing the same issue in react I wanted to increase the height of row according to the content of the text area and on enter it should go to next line in text area instead of not turning into read only, so what I did i used the suppressKeyboardEvent of ag-grid and wrote the code into it, here is my code
cellClass: "description-cell",
width: 200,
cellRendererFramework: (params) =>{
return <pre> {params.data.description}</pre>
},
cellEditor: 'agLargeTextCellEditor',
cellEditorParams: (params) => {
return {
maxLength: '1000',
cols: this.props.cols,
rows: 2
}
},
suppressKeyboardEvent: (params) => {
const KEY_ENTER = 13;
const keyCode = params.event.keyCode;
const gridShouldDoNothing = params.event.target.value && params.editing && keyCode === KEY_ENTER;
params.event.target.style.height = 'inherit';
params.event.target.style.height = `${params.event.target.scrollHeight}px`;
params.node.setRowHeight(params.event.target.scrollHeight); // adjust it according to your requirement
this.gridApi && this.gridApi.onRowHeightChanged();
return gridShouldDoNothing;
}
I hope this could help you or someone who is looking for it :)
What helped me was to call redrawRows()
Typescript + React example:
const onCellEditingStopped = (event: CellEditingStoppedEvent<any>) => {
event.api.redrawRows();
};

How to remove ag-grid default filtering columns

I tried removing the default filter of ag-grid which hides the column(s) and appears in the most right side of each column header, but I couldn't find the property. Can anyone help me?
You should use menuTabs property in column definition like this -
{
headerName: "Athlete",
field: "athlete",
width: 150,
filter: "agSetColumnFilter",
menuTabs: [ 'filterMenuTab', 'generalMenuTab']
}
As per docs -
menuTabs Set to an array containing zero, one or many of the
following options 'filterMenuTab', 'generalMenuTab' and
'columnsMenuTab'. This is used to figure out which menu tabs and in
which order the tabs are shown

A checkbox data type and showing more than one row per "row" (in Adazzle react-data-grid)

We'd like to use React Data Grid from Adazzle (as mentioned in the tag with this question) now that we're using React (16.xx) and the older jsGrid is less appealing.
Two questions:
Our old jsGrid had a data type to include in the grid called Checkbox. This was immensely helpful. I haven't found a similar type in the React Grid. Have I just missed it or is it not a part of the library?
In each row, in some columns, we'd like to actually have two rows of data inside the column -- i.e., two lines of text separated by carriage return. But the react-data-grid demos only seem to show one line per column. Is this a limitation, or can we use CSS in the values inside columns?
Is there search included for the rows shown in the table at any given point in time?
I will provide answer to your question part by part
1) In react-data-grid column you can include a checkbox by using the formatter
attribute that is specified while the columns are defined.
Consider the following example
let columns = [
{
key: 'name',
name: 'Name',
resizable: true,
width: 100
},
{
key: 'checkbox',
name: 'CheckBox',
formatter:checkBoxFormatter,
resizable: true,
width: 100
}
]
class checkBoxFormatter extends React.Component {
render() {
return (
<div>
<CheckBox></Checkbox> //Provide your checkbox code here
</div>
)
}
}
2)You can exapand the rows for that you need to use getSubRowDetails and onCellExpand attributes of ReactDataGrid component .For example refer this documentation
3)Search is available for filtering .For example refer this documentation

Why is the autoHeight property not being recognized?

I am trying to create some columns using the autoHeight property set to true. However, I keep getting these warnings:
ag-grid: invalid colDef property 'autoHeight' did you mean any of these: suppressAutoSize,headerTooltip,openByDefault,headerComponent,suppressSizeToFit,sort,headerGroupComponent,pivot
<...snip...>
ag-grid: to see all the valid colDef properties please check: https://www.ag-grid.com/javascript-grid-column-properties/
My column definitions look something like this:
const columnDefs = [
{
headerName: 'Id',
field: 'id',
filter: 'agTextColumnFilter',
width: 90
}, {
headerName: 'Description',
field: 'sportLeage',
filter: 'agTextColumnFilter',
autoHeight: true,
valueGetter: params =>
params.context.liveEventsController
.createDescription(params.data),
width: 90
};
autoHeight is clearly listed as a valid column definition property. Though, in the debugger, I am looking at colDefUtil_1.ColDefUtil.ALL_PROPERTIES and autoHeight is not one of the available properties.
I am using ag-grid-enterprise v17.0.0.
My thinking is that either my version is out of date or the documentation is.
Check ag-grid v17.0.0 type definition code for ColDef here
autoHeight is not a property of ColDef.
I am currently at v17.1.1 and autoHeight is declared as number.
/** True if this column should stretch rows height to fit contents */
autoHeight?: number;
PS: The comments suggests that it should be a boolean.
Now this reminds me of a saying :)
Code never tells a lie, comments however, sometimes do.

Exported excel from ag-grid has incorrect formatting

I am facing an issue in ag-grid. I have created a plnkr to reproduce the issue.
In following plunkr, first column's cells are displayed green but when we download in excel they shows blue.
This is because of one cell having both classes, blue and green.
Please note the cellClass function provided to Age column def.
{
headerName: 'Age', field: 'age', width: 90,
cellClass:function(params){
if(params.data.age === 23){
return ['blueBackground', 'greenBackground'];
}
return '';
}
}
But the question is, why does this affect the other column cells in exported excel?
I could understand, if only that particular cell, having both conflicting classes would have the problem of showing either one color.
Plnkr:
https://plnkr.co/edit/OdIwGznvmTYwkUTKZOEa?p=preview