灰烬循环通过一系列的复选框,并获得检查的项目? [英] Ember loop through an array of checkboxes and get checked items?

查看:74
本文介绍了灰烬循环通过一系列的复选框,并获得检查的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Ember.js应用程序(使用Ember CLI)。我有一个名为 sideMenu 的组件,其中包含文件 sideMenu.js sideMenu.hbs 。在.js文件中,我有一系列的车辆名称,如下所示:

I have an Ember.js application (using the Ember CLI). I have a component called sideMenu with the files sideMenu.js and sideMenu.hbs. Within the .js file, I have an array of vehicle names like this:

export default Ember.Component.extend({
    vehicles: ['Audi', 'Chrysler', 'Harley Davidson'],
    vehicleCheckboxes: { 'Audi': true, 'Chrysler': true, 'Harley Davidson': true }
});

在此组件的模板中,我遍历这些工具并为每个项目创建一个复选框,例如此:

In my template for this component, I am looping through these vehicles and creating a checkbox for each item like this:

{{#each vehicles as |vehicle|}}
    {{input type='checkbox' checked='true'}} {{vehicle}}
{{/each}}

这给了我我想要的东西-旁边带有车辆名称的几个复选框。我想知道该列表中用户未选中/选中的内容。我尝试通过在模板中动态创建一些Ember属性来做到这一点:

This gives me what I want - a few checkboxes with the vehicle names next to it. I want to be able to know what the user unchecked / checked in this list. I tried to do this by creating some Ember properties dynamically and in the template:

{{input type='checkbox' checked="{{vehicleCheckboxes}}.{{vehicle}}"}}

虽然工作。我该怎么做? Ember文档上似乎没有任何迹象表明可以在框架内以任何方式实现。

This didn't seem to work though. How can I accomplish this? There doesn't seem to be any indication on the Ember documentation that it is possible from within the framework in any way.

推荐答案

已选中期望布尔值( true / false
您可以使用此插件,例如:

checked expects boolean value (true/false) you can either use this addon like :

{{multiselect-复选框options = vehicles selection = selectedVehicles}}

,也可以像这样循环:

{{#each vehicles as |vehicle|}}
    {{input type='checkbox' checked=(get vehicleCheckboxes vehicle)}} {{vehicle}}
{{/each}}

,您需要将 vehicleCheckboxes 转换为Ember。对象我认为如果它不能正常工作

and you need your vehicleCheckboxes converted to Ember.Object I think if it does not work as it is

vehicleCheckboxes:Ember.Object.create({'Audi':true,'Chrysler':true,'哈雷戴维森(Harley Davidson):true})

这篇关于灰烬循环通过一系列的复选框,并获得检查的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆