angulardevexpressdevextremedevextreme-angular

How to programmatically get current sorted column in Devextreme datagrid?


I use devextreme datagrid 17.1.1 version in my Angular 4 project. I want get current sorted column in typeScript, but datagrid's instance hasn't sorting field. How can I get current sorted column ? Thanks


Solution

  • Try reading the sortIndex of a column. It will be 0 or 1 depending on how many columns are sorted. If you only have one column sorted at a time you can do something like this:

    @ViewChild(DxDataGridComponent, { static: false }) dataGrid: DxDataGridComponent;
    
    getSortedColumn() {
      const instance = this.dataGrid.instance;
      const allColumns = Array.from(Array(instance.columnCount()).keys()).map(index => instance.columnOption(index));
      return allColumns.find(col => col.sortIndex != null);
    }