AngularJS orderby:将数组元素保留在顶部 [英] AngularJS orderby : keep an element of array on top
问题描述
我已经搜索过,但没有找到我的问题的答案.希望,我已经很好地搜索了!所以,这是我的问题:
I have searched and don't found an answer to my question. Hope, I've well searched ! So, this is my problem :
<div id="{{expression.comment_id.$id}}" class="comments" ng-repeat="expression in expressions| orderBy:orderProp"
所以,orderBy 效果很好,但我有一个特定的需求:id 为0"的 expression.comment_id 必须保持在最前面.
So, orderBy works great, but I have a specific need : expression.comment_id with '0' id must stay on top.
怎么能这样呢?我想我必须使用个人过滤器,但我不确定这是更有效的解决方案.如果这是一个好方法,我该如何正确实施?
How can do this ? I think I must use a personnal filter but I'm not sure this is the more efficient solution. If this is a good way, how can I implement it correctly ?
感谢您的帮助!
推荐答案
here you go made a fiddle for you: http://jsfiddle.net/sameersemna/81q9jwg9/
here you go made a fiddle for you: http://jsfiddle.net/sameersemna/81q9jwg9/
基本上你必须做一个自定义的 orderBy 过滤器:
basically you have to make a custom orderBy filter:
<ul ng-repeat="expression in expressions | orderBy:myValueFunction">
在控制器中:
$scope.myValueFunction = function(expression) {
if(expression.comment_id == 0){
return expression.comment_id;
}else{
return expression.orderProp;
}
}
这篇关于AngularJS orderby:将数组元素保留在顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!