如何使用拆分字符串函数的结果对 Angular.js 进行排序?
How to orderBy with Angular.js using the result of a split string function?
我正在显示 table 样本员工数据,我需要按姓氏排序。数据通过 .JSON 文件提供,名称在单个字符串中:
users.json
{
"id": 5,
"name": "Bob Smith",
"username": "Eloyn.Skilles",
"email": "smith.bob@bolly.biz",
"address": {
"street": "Rox Trial",
"suite": "Suite 2890",
"city": "Smashmouth",
"zipcode": "586041099",
"geo": {
"lat": "27.8718",
"lng": "21.8984"
}
},
"phone": "210.067.6132",
"website": "elvis.io",
"company": {
"name": "Johns Group",
"catchPhrase": "Configurable multimedia taskforce",
"bs": "generate enterprise etailers"
}
}
到目前为止,我已经能够将字符串分成两部分:
MainController.js
/* Function to split strings where there is a space present */
$scope.nameSplitter = function(string, index) {
$scope.array = string.split(' ');
return $scope.result = $scope.array[index];
}
这是HTML:
<table class="table table-striped table-bordered table-condensed table-hover">
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<!-- Would like to ideally orderBy last name -->
<tr ng-repeat="user in userData | orderBy:'name'">
<td>{{ nameSplitter(user.name,1) }}, {{ nameSplitter(user.name,0) }}</td> <!--0 returns first name, 1 returns last name-->
<td><a ng-href="mailto:{{user.email}}" target="_blank">{{user.email}}</a></td>
<td><a ng-if="user.phone !== ''" href="tel:{{nameSplitter(user.phone, 0)}}" data-toggle="tooltip" title="{{nameSplitter(user.phone, 0)}}">
<span class="glyphicon glyphicon-earphone"></span></a></td>
</table>
我用各种括号组合尝试了这个orderBy:'nameSplitter(user.name, 1)'
,但没有成功。
您可以在 orderBy
Angular orderBy docs
中使用 Array
、function
或 attribute name
所以在html
<tr ng-repeat="user in userData | orderBy:myCustomOrderBy">
并在控制器中
$scope.myCustomOrderBy = function(user){
return user.name.split(' '); //what you intent to here play on your own
}
作用域中的当前用户作为参数传递
带有字符串的 orderBy 是一个表达式,因此您可以包含函数,或者可以提供自定义函数作为参数而不是字符串。
<tr ng-repeat="user in userData | orderBy:'name.split(\' \')[1]'">
orderBy 也接受函数。
ng-repeat="user in userData | orderBy:getlastname"
$scope.getlastname = function(user) {
var name = user.name.split(' ');
return name[1];
}
我正在显示 table 样本员工数据,我需要按姓氏排序。数据通过 .JSON 文件提供,名称在单个字符串中:
users.json
{
"id": 5,
"name": "Bob Smith",
"username": "Eloyn.Skilles",
"email": "smith.bob@bolly.biz",
"address": {
"street": "Rox Trial",
"suite": "Suite 2890",
"city": "Smashmouth",
"zipcode": "586041099",
"geo": {
"lat": "27.8718",
"lng": "21.8984"
}
},
"phone": "210.067.6132",
"website": "elvis.io",
"company": {
"name": "Johns Group",
"catchPhrase": "Configurable multimedia taskforce",
"bs": "generate enterprise etailers"
}
}
到目前为止,我已经能够将字符串分成两部分:
MainController.js
/* Function to split strings where there is a space present */
$scope.nameSplitter = function(string, index) {
$scope.array = string.split(' ');
return $scope.result = $scope.array[index];
}
这是HTML:
<table class="table table-striped table-bordered table-condensed table-hover">
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<!-- Would like to ideally orderBy last name -->
<tr ng-repeat="user in userData | orderBy:'name'">
<td>{{ nameSplitter(user.name,1) }}, {{ nameSplitter(user.name,0) }}</td> <!--0 returns first name, 1 returns last name-->
<td><a ng-href="mailto:{{user.email}}" target="_blank">{{user.email}}</a></td>
<td><a ng-if="user.phone !== ''" href="tel:{{nameSplitter(user.phone, 0)}}" data-toggle="tooltip" title="{{nameSplitter(user.phone, 0)}}">
<span class="glyphicon glyphicon-earphone"></span></a></td>
</table>
我用各种括号组合尝试了这个orderBy:'nameSplitter(user.name, 1)'
,但没有成功。
您可以在 orderBy
Angular orderBy docs
Array
、function
或 attribute name
所以在html
<tr ng-repeat="user in userData | orderBy:myCustomOrderBy">
并在控制器中
$scope.myCustomOrderBy = function(user){
return user.name.split(' '); //what you intent to here play on your own
}
作用域中的当前用户作为参数传递
带有字符串的 orderBy 是一个表达式,因此您可以包含函数,或者可以提供自定义函数作为参数而不是字符串。
<tr ng-repeat="user in userData | orderBy:'name.split(\' \')[1]'">
orderBy 也接受函数。
ng-repeat="user in userData | orderBy:getlastname"
$scope.getlastname = function(user) {
var name = user.name.split(' ');
return name[1];
}