排序不适用于 ng-grid 中的 IP 地址列
Sorting is not working on IP address column in 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%'
}];
有什么想法吗?
默认情况下,列应该进行字符串排序。如果你的意思是,它应该进行数字排序而不是字符串排序,你可以尝试对 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%'
}
];
希望这对您有所帮助
我有一个包含 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%'
}];
有什么想法吗?
默认情况下,列应该进行字符串排序。如果你的意思是,它应该进行数字排序而不是字符串排序,你可以尝试对 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%'
}
];
希望这对您有所帮助