gpt4 book ai didi

javascript - AngularJS 对自定义控件内的文本和大数字进行排序

转载 作者:行者123 更新时间:2023-12-03 05:54:53 33 4
gpt4 key购买 nike

请引用以下链接

http://codepen.io/anon/pen/fjkcg

html:

<section ng-app="app" ng-controller="MainCtrl">
<span class="label">Ordered By: {{orderByField}}, Reverse Sort: {{reverseSort}}</span><br><br>
<table class="table table-bordered">
<thead>
<tr>
<th>
<a href="#" ng-click="orderByField='firstName'; reverseSort = !reverseSort">
First Name <span ng-show="orderByField == 'firstName'"><span ng-show="!reverseSort">^</span><span ng-show="reverseSort">v</span></span>
</a>
</th>
<th>
<a href="#" ng-click="orderByField='lastName'; reverseSort = !reverseSort">
Last Name <span ng-show="orderByField == 'lastName'"><span ng-show="!reverseSort">^</span><span ng-show="reverseSort">v</span></span>
</a>
</th>
<th>
<a href="#" ng-click="orderByField='age'; reverseSort = !reverseSort">
Age <span ng-show="orderByField == 'age'"><span ng-show="!reverseSort">^</span><span ng-show="reverseSort">v</span></span>
</a>
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="emp in data.employees|orderBy:orderByField:reverseSort">
<td>{{emp.firstName}}</td>
<td> <input type="text"
ng-init="empx['name_'+$index]=emp['lastName']"
ng-model="empx['name_'+$index]"/> </td>
<td>{{emp.age}}</td>
</tr>
</tbody>
</table>
</section>

Controller :

var app = angular.module('app', []);

app.controller('MainCtrl', function($scope) {
//$scope.orderByField = 'firstName';
$scope.reverseSort = false;
$scope.empx={};
$scope.data = {
employees: [{
firstName: 'John',
lastName: 'F',
age: '6703114'
},{
firstName: 'Frank',
lastName: 'D',
age: '665087589'
},{
firstName: 'Sue',
lastName: 'F',
age: '5271761234'
},
{
firstName: 'Sue',
lastName: 'F',
age: '52'
},{
firstName: 'Sue',
lastName: 'F',
age: '2334564564564564564'
}]
};
});

我们在 ng-repeat 中有文本框控件和日期选择器控件。当我过滤姓氏时,排序不正确。我如何对控件内的集合进行排序。年龄和姓氏排序不起作用,姓氏排序不起作用,因为该值位于文本框控件内,而年龄因为数字很大而不起作用,但它被表示为字符串。我如何才能实现排序在所有情况下都能正常工作。

最佳答案

我猜是因为 ng-init.除了嵌套的 ng-repeats 之外,你永远不应该使用 ng-init。

尝试删除 ng-init 并直接使用员工数据的克隆(或原始)-

empx = data.employees.map(item => angular.extend({}, item));

并在模板中使用 ng-model="item.lastName" 迭代 empx

关于javascript - AngularJS 对自定义控件内的文本和大数字进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39976161/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com