我正在研究这个应该对py IP地址进行排序的应用程序,因此要解决基本上是字符串的问题,我将IP地址分解成八位字节并尝试连续按每个八位字节进行排序。但是当我应用于我的数据时,我对Angularjs的“orderBy”函数有一些问题:
<tr ng-hide="row.length" class="tablehover tr-list" ng-repeat="row in entries | filter:search | orderBy:['ipA','ipB','ipC','ipD']">
<td class="table-item col-1 col"><img ng-src="./img/{{row.health}}" class="status-color"></td>
<td class="table-item col-2 col text-ip">{{ row.ipaddr }}</td>
<td class="table-item col-3 col">{{ row.nickname }}</td>
<td class="table-item col-4 col text-dns">{{ row.dnsname }}</td>
<td class="table-item col-5 col text-type">{{ row.type }}</td>
<td class="table-item col-6 col"><center><img ng-src="./img/{{row.reserved}}" class="status-reserved"></center></td>
<td class="table-item col-7 col"><div id="editBtn" class="flat icon" name="editBtn" ng-model="editBtn" type="button" ng-click="edit(row)"><img src="./img/edit.png" class="inline-icon edit"></button></div>
<td class="table-item col-8 col"><div id="deleteBtn" class="flat icon" name="deleteBtn" value="./img/delete.png" ng-click="delete(row)"><img src="./img/cancel.png" class="inline-icon delete"></div></td>
<!-- these are for sorting purposes only -->
<td class="hidden">{{ row.subnet }}</td>
<td class="hidden">{{ row.location }}</td>
<td class="hidden">{{ row.vlan }}</td>
<td class="hidden">{{ row.notes }}</td>
<td class="">{{row.ipA}}.{{row.ipB}}.{{row.ipC}}.{{row.ipD}}</td>
</tr>
理论上,这应该是这样命令:
1.1.1.1
8.8.8.8
10.10.10.10
但实际上它的命令是这样的:
1.1.1.1
10.10.10.10
8.8.8.8
我想避免使用八位字节并将其转换为3位整数(即001.001.001.001);我觉得这应该是简单的事情,我只是简单地搞砸了。
我已经尝试过的事情:
强制角度将每个八位字节视为一个数字(不起作用)
{{ row.ipA | number }}
将每个新记录保存到数据库时将每个八位字节转换为浮点数:
var octets = ($scope.ipaddr).split(".");
$scope.ipA = parseFloat(octets[0]);
$scope.ipB = parseFloat(octets[1]);
$scope.ipC = parseFloat(octets[2]);
$scope.ipD = parseFloat(octets[3]);
谢谢!
答案 0 :(得分:4)
尝试这样做:
在您的控制器中添加此功能:
$scope.ipOrder = function(row){
return (
(parseInt(row.ipA)*Math.pow(256,3))+
(parseInt(row.ipB)*Math.pow(256,2))+
(parseInt(row.ipC)*256)+
parseInt(row.ipD));
}
然后在ng-repeat
执行此操作:
ng-repeat="row in entries | filter:search | orderBy:ipOrder
答案 1 :(得分:1)
尝试将八位字节转换为数字()而不是?
row.ipA = Number(八位字节[0]);