[英]AngularJs - <ul> <li> element to top when its checkbox is checked
This question is based in this previous one 这个问题基于前一个问题
I have a ng-repeat
like this: 我有这样
ng-repeat
:
<li ng-repeat="opt in namesCtrl.uniqueCars">
<input type="checkbox" ng-model="namesCtrl.filter['cars'][opt.element]" | orderBy:['checkbox','-count']"><p2>{{opt.element}} ({{opt.count}})</p2>
</li>
Where the elements are taken from this.uniqueCars
元素取自
this.uniqueCars
this.uniqueCars=[
{'element':'Ford','count':3,'checkbox':false},
{'element':'Honda','count':2,'checkbox':false},
{'element':'Ferrari','count':1,'checkbox':false},
{'element':'Delorean','count':6,'checkbox':false}
];
And the checked items in the view go to this.filter
(that I use for another purpose) 并且视图中的已检查项目将转到
this.filter
(我将其用于其他目的)
this.filter = {
"cars": {}
}
So what I want to achieve is to order the checkbox list in the view, so when one of the items is checked, it would go to the top, something like this 所以我想要实现的是在视图中订购复选框列表,因此当选中其中一个项目时,它会转到顶部,类似这样的
In order to do that, I have a $watch
that goes like this 为了做到这一点,我有一个像这样的
$watch
$scope.$watch("namesCtrl.filter", function(nv,ov){
angular.forEach(self.uniqueCars, function(opt){
opt.checkbox = nv.cars[opt.element]
})
}, true)
But the behavior is pretty inconsistent when ordering by the checkbox value (when first clicking it does the job, OK, but when unclicking it doesn't work, and when clicking again it sorts it in the opposite way). 但是当按复选框值排序时行为非常不一致(当第一次点击它完成作业时,OK,但是当取消它时不起作用,再次点击它时会以相反的方式对其进行排序)。
So I would like to do this by modifying the $watch
. 所以我想通过修改
$watch
来做到这一点。 (I do it this way as IRL this.filter
could be modified by other elements, so I need to watch the changes there). (我这样做就像IRL
this.filter
可以被其他元素修改,所以我需要观察那里的变化)。
PS: the this.uniqueCars.checkbox
the field is inserted by me just in order to make the sorting possible, so it would be safe to modify it. PS:
this.uniqueCars.checkbox
字段是我插入的,只是为了使排序成为可能,所以修改它是安全的。
You can check the working Plunker here . 你可以在这里检查工作的Plunker 。
You almost got it right. 你几乎做对了。
There was a small problem with your $watch
function: In the case where a car is not yet a member of namesCtrl.filter
, its checkbox
value is removed from namesCtrl.uniqueCars
because it's undefined. 有一个小问题,您的
$watch
功能:在汽车尚未成员的情况下namesCtrl.filter
,其checkbox
值从删除namesCtrl.uniqueCars
因为它是不确定的。
If you handle this case like this: 如果你像这样处理这种情况:
$scope.$watch("namesCtrl.filter", function(nv,ov){
angular.forEach(self.uniqueCars, function(opt){
var value = nv.cars[opt.element] ? nv.cars[opt.element].checkbox : false;
opt.checkbox = value;
});
}, true);
Then your ordering is fine. 那你的订购就好了。 Except you also need to order by checkbox in reverse order:
除了您还需要按相反顺序的复选框订购:
<li ng-repeat="opt in namesCtrl.uniqueCars | orderBy:['-checkbox','-count']">
See working plunker . 见工作的plunker 。
Why can't you modify checkbox
propery itself? 为什么你不能修改
checkbox
属性本身? Changing input's model to opt.checkbox
and commenting watch out make everything work. 将输入的模型更改为
opt.checkbox
并注释注意使一切正常。 I think it's better this way. 我认为这种方式更好。
But you can modify opt.checkbox = nv.cars[opt.element]
to opt.checkbox = !!nv.cars[opt.element]
to make it work too (the checkbox property in cars
object is undefined
, that breaks proper ordering). 但是您可以将
opt.checkbox = nv.cars[opt.element]
修改为opt.checkbox = !!nv.cars[opt.element]
以使其工作( cars
对象中的checkbox属性undefined
,这违反了正确的顺序)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.