排序在我的inputField上无法正常工作。点击inputField
列后,数值得到排序,但问题是排序对于修改后的数值不正确,所有数值都在最后。我将model-change-blur
添加到文本框中的模糊处理后进行排序。我不知道我在做什么错。修改后的值。AngularJs排序顺序在ng-repeat中无法正常工作
这是我的HTML代码:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/bootstrap.min.css">
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/myscript.js"></script>
</head>
<body ng-app="app" ng-controller="myController">
<table class="table table-bordered">
<thead>
<tr>
<th class="text-center" style="width: 65px;">
<div href="#" ng-click="sortType = 'id'; sortReverse = !sortReverse">
ID <span class="glyphicon sort-icon" ng-show="sortType=='id'" ng-class="{'glyphicon-chevron-up':sortReverse,'glyphicon-chevron-down':!sortReverse}"></span>
</div>
</th>
<th class="text-center" style="width: 65px;">
<div href="#" ng-click="sortType = 'checkBoxField'; sortReverse = !sortReverse">
Checked <span class="glyphicon sort-icon" ng-show="sortType=='checkBoxField'" ng-class="{'glyphicon-chevron-up':sortReverse,'glyphicon-chevron-down':!sortReverse}"></span>
</div>
</th>
<th class="text-center" style="width: 61px;">
<div href="#" ng-click="sortType = 'inputField'; sortReverse = !sortReverse">
Input <span class="glyphicon sort-icon" ng-show="sortType=='inputField'" ng-class="{'glyphicon-chevron-up':sortReverse,'glyphicon-chevron-down':!sortReverse}"></span>
</div>
</th>
</thead>
<tbody>
<tr ng-repeat="row in filtered = (tableData | orderBy:sortType:sortReverse)" id="row-{{$index}}">
<td align="center" style="width: 68px;">{{row.id}}</td>
<td align="center" style="width: 68px;"><input id="checkBoxField-{{$index}}" type="checkbox" ng-model="row.checkBoxField" ng-true-value="'Y'" ng-false-value="'N'"/></td>
<td align="center" style="width: 61px;"><input id="inputField-{{$index}}" class="onlyNumber" type="text" ng-model="row.inputField" maxlength="3"style="width: 50px;" model-change-blur></td>
</tr>
</tbody>
</table>
</body>
这里是我的js文件:
angular.module('app', [])
.controller('myController', function($scope) {
$scope.sortType = '';
$scope.sortReverse = false;
$scope.tableData = [];
for(i=1; i<= 8; i++){
$scope.tableData.push({"id":i,"checkBoxField": i%3==0 ,"inputField":1+i});
}
})
.directive('modelChangeBlur', function() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, elm, attr, ngModelCtrl) {
if (attr.type === 'radio' || attr.type === 'checkbox') return;
elm.unbind('input').unbind('keydown').unbind('change');
elm.bind('blur', function() {
scope.$apply(function() {
ngModelCtrl.$setViewValue(elm.val());
});
});
}
};
});
更新:添加图像
不排序表看起来如下:
表格排序升序排列后的样子如下:
后修改的第三排顺序看起来像下面这里是问题:
修正值将最下面一行。
你想我之后留下的文本框右侧对数据进行排序? –
@PareshGami是的,你是对的。 –