简体   繁体   中英

How to sort nested properties in the field column definition of ag-grid?

var data = [{
   id: {
        id: "108662", 
        absoluteUri:"abc.com", 
        devicedisplayId: "045551"
       }
  devicename: "Printer"
}]

var columnDefs = [

     {
      headerName: 'Device Id', field: 'id',filter:
      'agTextColumnFilter',sortable: true,
      filterValueGetter : function(params) {
         return params.data.id.devicedisplayId;
      },
     cellRenderer: function(params) {
        return '<a href=" ' + params.value.absoluteUri + '" target="_blank">' + 
                 params.value.devicedisplayId+ '</a>';
      }
   }

I have nested data.wanted to achieve sorting based on devicedisplayId. i have used filterValueGetter to filter but did not came across any such function wrto sorting.

For custom sorting function you can use the comparator method in column definition like so.

    comparator: (id1, id2) => {
    if (id1.devicedisplayId < id2.devicedisplayId) {
      return -1;
    }
    if (id1.devicedisplayId < id2.devicedisplayId) {
      return 1;
    }
    return 0;
    }

Check Custom Sorting in ag-grid documentation for more details - https://www.ag-grid.com/javascript-grid-sorting/#custom-sorting

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM