gpt4 book ai didi

javascript - 筛选器未按预期显示

转载 作者:行者123 更新时间:2023-11-29 21:20:15 26 4
gpt4 key购买 nike

我制作了一个表格来显示数组中的值,并向其中添加了一个过滤器。该表分页,每页最多 5 条记录。

问题发生在我应用过滤器时。例如,如果您搜索术语“ora”,将有三个记录,每页一个。

这些结果如何一起显示(在一个页面上,或者它需要多少页,但不消散)以及何时删除过滤器以正常显示(在所有页面中)?

实时代码可在此处获得:https://plnkr.co/edit/QIcmQUqABDJcV3H6vqRo?p=preview

HTML:

<body ng-app="salariesApp" ng-controller="mainCtrl">
<table class="table">
<thead>
<tr>
<th ng-click="sortBy(id)">Nr. crt.</th>
<th ng-click="sortBy(an)">Anul</th>
<th ng-click="sortBy('den')">Denumirea institutiei/companiei</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableSearch"></td>
<td class="tableSearch"><input class="form-control" type="text" id="inputYear" ng-model="searchSal.an" ng-value={{crrYear}} /></td>
<td class="tableSearch"><input class="form-control" type="text" id="inputName" ng-model="searchSal.den" /></td>
</tr>
<tr ng-repeat="rows in showSal | filter: searchSal | orderBy: sortProperty: !reverse | filter : paginate track by $index">
<td class="spa-col-md-1">{{rows.id}}</td>
<td class="spa-col-md-2">{{rows.an}}</td>
<td class="col-md-5">{{rows.den}}</td>
</tr>
</tbody>
</table>

<pagination total-items="totalItems" ng-model="currentPage" max-size="5" boundary-links="true"
items-per-page="numPerPage" class="pagination-sm">
</pagination>
</body>

JS:

var app = angular.module("salariesApp", ['ui.bootstrap']);

app.constant("appConst", {
pagesLimit: 5,
crrYear: 2016
});

app.controller('mainCtrl', ['$scope', 'appConst', function($scope, appConst) {
$scope.crrYear = appConst.crrYear;

$scope.pageNum = {
linkBtnClass: 'linkBtnClass cursor-pointer'
};

$scope.showSal = [
{ id: '1', an: '2016', den: 'Oracle Romania' },
{ id: '2', an: '2016', den: 'Microsoft' },
{ id: '3', an: '2016', den: 'Computer Generated Solutions - CGS Europe' },
{ id: '4', an: '2016', den: 'IBM' },
{ id: '5', an: '2016', den: 'Luxoft' },
{ id: '6', an: '2016', den: 'Oracle Romania' },
{ id: '7', an: '2016', den: 'Luxoft' },
{ id: '8', an: '2016', den: 'Computer Generated Solutions - CGS Europe' },
{ id: '9', an: '2016', den: 'IBM' },
{ id: '10', an: '2016', den: 'Luxoft' },
{ id: '11', an: '2016', den: 'Oracle Romania' },
{ id: '12', an: '2016', den: 'Microsoft' },
{ id: '13', an: '2016', den: 'IBM' },
{ id: '14', an: '2016', den: 'Luxoft' }
];

$scope.searchSal = '';

$scope.sortProperty = $scope.showSal.id;
$scope.reverse = true;
$scope.sortBy = function (sortProperty) {
$scope.reverse = ($scope.sortProperty === sortProperty) ? !$scope.reverse : false;
$scope.sortProperty = sortProperty;
};



// PAGINATION:
$scope.totalItems = $scope.showSal.length;
$scope.currentPage = 1;
$scope.numPerPage = 5;

$scope.paginate = function(value) {
var begin, end, index;
begin = ($scope.currentPage - 1) * $scope.numPerPage;
end = begin + $scope.numPerPage;
index = $scope.showSal.indexOf(value);
return (begin <= index && index < end);
};


$scope.getOptionsClass = function (index) {
if (index > 0) {
return 'thumbnail nav-option-r';
} else {
return 'thumbnail nav-option-l';
}
};
}]);

最佳答案

我已经对您的实时版本做了一些更改,以实现您想要的效果。检查以下链接://plnkr.co/edit/TZ1woevPK8uLlbERBtlR?p=preview

你的版本不起作用的原因是过滤器只做过滤工作来决定模型是否满足条件而不是改变模型。如果模型没有发生变化,那么分页或表格列表就不会发生任何变化。

所以我所做的只是创建原始模型的副本并向其添加观察者。如果我在上面附加了 plnkr 链接,您可以找到更多信息。

关于javascript - 筛选器未按预期显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38699966/

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