简体   繁体   English

AngularJs - <ul><li> 选中其复选框时,元素到顶部

[英]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.

相关问题 如何保持 <li> 元素固定的顶部和底部 <ul> 滚动时的元素 - How to keep <li> elements fixed top and bottom of <ul> element when scrolling 有没有办法显示溢出其父元素的 ul 元素的最后一个 li 元素? - Is there a way to display the last li element of a ul element that overflows its parent? 在 UL、LI 和 Label 内的 Bootstrap 下拉菜单中获取选中复选框的 id,然后根据是否选中其他复选框 - Get id of checked checkbox in Bootstrap dropdown inside UL, LI, and Label, then set others based on it being checked or not 换班的 <li> 当每个的复选框 <li> 检查 - Changing class for the <li> when checkbox of each <li> is checked 将选中的 Li 移动到 UL 的底部 - Moving a Checked Li to the Bottom of a UL 单击新的UL LI元素时关闭/隐藏上一个UL LI元素 - Closing/Hiding previous UL LI element when a new UL LI element is clicked on 隐藏一个 <li> 元素 <ul> - Hiding an <li> element in a <ul> 需要 Jquery foreach 函数来找出嵌套的 UL LI 选中的复选框项 - need Jquery foreach function to find out nested UL LI checked checkbox items 在AngularJS中选中复选框后如何打印天气? - How to print weather when the checkbox is checked in AngularJS? 在angularjs中选中复选框时提交值 - submit value when checkbox is checked in angularjs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM