排序不适用于ng-grid中的IP地址列

时间:2015-04-20 06:40:05

标签: angularjs sorting ng-grid

我有一个网格视图,其中有4列如下:

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

我正在使用ng-grid框架来显示这些。这里的排序不适用于ip地址列,它适用于其他列。 我的列定义如下: -

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%'
                }];

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

默认情况下,该列应进行字符串排序。如果你的意思是,它应该进行数字排序而不是字符串排序,你可以尝试使用ip列的自定义排序函数,

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%'
  }
];

希望这会有所帮助