Sort not working on IP address column in ng-grid

I have a grid view that has 4 columns as shown below:

IP           | Name | Status  |Value

192.168.1.3  |MAS   | UP     |x 

192.168.1.5  |HGR   | UP     |Y 

192.168.1.10 |UYR   |DOWN   |Z

192.168.1.7  |IOR   |UP      |P

      

I am using the ng-grid framework to display them. Here sorting doesn't work on the ip address column, it works great for other columns. My column definition looks like this: -

columnDefs : [{
                    field : 'ip',
                    displayName : 'IP',
                    width : '25%'
                }, 
                {
                    field : 'name',
                    displayName : 'NAME',
                    width : '25%'
                },
                {
                    field : 'status',
                    displayName : 'Status',
                    width : '25%'
                },
                {
                    field : 'value',
                    displayName : 'Value',
                    width : '25%'
                }];

      

Any idea on this?

+3


source to share


1 answer


By default, the column should sort the rows. In case you mean it should sort in order, not sort the rows, you can try using the sort function for the ip column.

columnDefs: [
  {
    field: 'ip',
    displayName: 'IP',
    width: '25%',
    sortFn: function (a, b) {
      if (a == b) return 0;
      if ( +a.replace(/\./g, '') < +b.replace(/\./g, '')) return - 1;
      return 1;
    }
  },
  {
    field: 'name',
    displayName: 'NAME',
    width: '25%'
  },
  {
    field: 'status',
    displayName: 'Status',
    width: '25%'
  },
  {
    field: 'value',
    displayName: 'Value',
    width: '25%'
  }
];

      



hope this helps

+2


source







All Articles