gpt4 book ai didi

javascript - 如何使用angularjs将匹配的行保持在表中其他行的顶部?

转载 作者:技术小花猫 更新时间:2023-10-29 12:39:30 25 4
gpt4 key购买 nike

我正在使用智能表,我想使用 angularjs(1) 将匹配的行保留在表中其他行的顶部,在我的例子中,我一直在匹配表列中的字符串,基于我正在更改的匹配字符串行背景颜色,但我想在其他行的顶部显示这些彩色行,以便我能够立即看到匹配的行。

如果我像下面这样排序,它是有效的,但它只影响当前页面,我想显示所有匹配的行到其他行的顶部,而不考虑分页和所有。

<tr ng-repeat="emp in employees | orderBy:set_color" ng-style="set_color(emp)">

我怎样才能做到这一点。如果您想了解更多信息,请查看此链接

var myApp = angular.module('myApp', [])

.controller('employeeController', function ($scope) {

var employees = [{
"Name": "Alfreds Futterkiste",
"City": "Berlin",
"Country": "Germany"
}, {
"Name": "Berglunds snabbköp",
"City": "Luleå",
"Country": "Sweden"
}, {
"Name": "Blauer See Delikatessen",
"City": "Mannheim",
"Country": "Germany"
}, {
"Name": "Blondel père et fils",
"City": "Strasbourg",
"Country": "France"
}, {
"Name": "Bólido Comidas preparadas",
"City": "Madrid",
"Country": "Spain"
}, {
"Name": "Bon app'",
"City": "Marseille",
"Country": "France"
}, {
"Name": "Bottom-Dollar Marketse",
"City": "Tsawassen",
"Country": "Canada"
}, {
"Name": "Cactus Comidas para llevar",
"City": "Buenos Aires",
"Country": "Argentina"
}, {
"Name": "Centro comercial Moctezuma",
"City": "México D.F.",
"Country": "Mexico"
}, {
"Name": "Chop-suey Chinese",
"City": "Bern",
"Country": "Switzerland"
}, {
"Name": "Comércio Mineiro",
"City": "São Paulo",
"Country": "Brazil"
}];
$scope.employees=employees;

$scope.set_color = function (emp) {
var inputString = emp.Country;
for (i = 0; i < inputString.length; i++) {
var findme = "France";
if (inputString.indexOf(findme) > -1) {
return { 'background-color': '#FFCCCB' }
}
}
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<body ng-app="myApp">
<div ng-controller="employeeController">
<div class="container" style="margin-top:40px;">
<div class="row">
{{error}}
<div class="col-md-6">
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th>Name</th>
<th>City</th>
<th>Country</th>
</tr>
</thead>
<tbody >
<tr ng-repeat="emp in employees" ng-style="set_color(emp)">
<td>{{emp.Name}}</td>
<td>{{emp.City}}</td>
<td>{{emp.Country}}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</body>

谢谢

最佳答案

对于 AngularJS,我相信我会选择这个选项:

HTML

orderBy 可以接受多个过滤器

 <tr ng-repeat="emp in employees | orderBy: [sort, 'Name']" 
ng-class="{'matched': emp.matched }">

JS

var findme = "France";
$scope.sort = (emp) => {
emp.matched = emp.Country.indexOf(findme) > -1;
return !emp.matched;
}

查看工作 fiddle :

https://jsfiddle.net/kraaness/vft90229/

关于javascript - 如何使用angularjs将匹配的行保持在表中其他行的顶部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48526901/

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