我的函数在单击元素时切换类名。但它不会从其他元素中删除类名。所以现在所有元素都在highliting。但是每当用户点击一个元素时,它应该有classname
其他兄弟姐妹必须删除classname
怎么办?
这是我的尝试但不起作用:
import Ember from 'ember';
export default Ember.Component.extend({
isSelected : false,
deSelectOthers : function(){
this.set( "isSelected", false ); //deselecting all before add the class to clicked element
},
actions : {
selectCard : function(card) {
this.deSelectOthers(); //de select others not works
this.toggleProperty('isSelected'); // add class to this element only works. how to remove class from other element?
}
}
});
更新我很抱歉对我的要求的描述不好。我已经创建了使用jQuery
- 简单地如何实现相同的要求emberjs
?演示要求
这是工作的旋转
您需要维护 selectedIndex/value 并使用 if 检查根据 selectedIndex/value 更新类。
应用程序.hbs
<div class="parent">
<ul>
{{#each model as | temp index|}}
<li class={{if (eq selectedIndex index) 'highlight' }} {{action 'changeSelectedIndex' index}}>{{temp}}</li>
{{/each}}
</ul>
</div>
路线/application.js
import Ember from 'ember';
export default Ember.Route.extend({
model(){
return [1,2,3,4];
}
});
控制器/application.js
import Ember from 'ember';
export default Ember.Controller.extend({
selectedIndex:undefined,
actions:{
changeSelectedIndex(index){
this.set('selectedIndex',index);
}
}
});
您需要通过运行安装 ember-truth-helpers 插件 ember install ember-truth-helpers
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句