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.

2 Answers 2

1

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'},
    ]
}
Sign up to request clarification or add additional context in comments.

1 Comment

I had seen this and tried. What I don't get is why all columns are sorted by default when enableSorting={true} even if I place sortable:false in column def for few of the columns.
1

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.