聚合物1.x:访问'选择'铁清单中的项目
Polymer 1.x: Accessing 'selected' items from iron list
如何访问元素、属性以及以其他方式迭代iron-list
的selected
项?
这是JSBin。
- 打开控制台
- 从列表中选择两个或三个项目
- 单击标有"在控制台中显示项目"的按钮
- 请注意控制台输出的最后三行中的输出问题。它们显示了未定义的数组长度,并且任何对象键所在的数组都是空的
那么,我们如何访问这些选定项目的值呢?
http://jsbin.com/duwasisoyo/1/edit?html输出_showItems: function(){
console.log(this.selectedList); // Okay
console.log(this.selectedList[0]); // Okay
console.log(this.selectedList[0]['name']); // Okay
console.log(this.selectedLength); // Undefined
console.log(this.selectedKeys); // Empty array
console.log(this.selectedNames); // Empty array
}
注意:此问题使用iron-list
"选定项目"演示的源代码
也许铁名单没有通知selectedList的更改。您可以将方法更改为
_showItems: function(){
console.log(this.selectedList);
console.log(this.selectedList[0]);
console.log(this.selectedList[0]['name']);
console.log(this._computeSelectedItemsLength(this.selectedList));
console.log(this._computeSelectedKeys(this.selectedList));
console.log(this._computeSelectedNames(this.selectedList));
}
此外,您还应该更改:
_computeSelectedNames: function(ob) {
var out = [];
for(x in ob){
out.push(ob[x]['name']);
}
return out;
},
啊,添加控件检查selectedList
是否为空;)
聚合物松弛网站的rob说:也许你需要使用
<array-selector id="selector" items="{{employees}}" selected="{{selected}}" multi toggle></array-selector>
就像这里建议的那样https://www.polymer-project.org/1.0/docs/devguide/templates.html#array-选择器
关键变更:https://jsbin.com/hiruducole/1/edit?html输出这是一个固定版本https://jsbin.com/hiruducole/1/edit?html,输出
您需要观察(selectedItems.*)
然后你可以使用更改记录来找出的更改
https://www.polymer-project.org/1.0/docs/devguide/properties.html#array-观察
你也可以观察(selectedItems.peets),但我很懒,只做(selectedItem.*),它会捕捉到任何变化。
以下是关于变更记录的解释:https://www.polymer-project.org/1.0/docs/devguide/properties.html#deep-观察
/** / Before
selectedLength: {
computed: '_computeSelectedLength(selectedItems)'
},
/**/
// After
selectedLength: {
computed: '_computeSelectedLength(selectedItems.*)'
},
...
/** / Before
_computeSelectedLength: function(ar) {
return ar.length;
},
/**/
// After
_computeSelectedLength: function(record) {
return record.base.length;
},
相关文章:
- 通过单击表单中的按钮,在代码生成中使用javascript生成字母数字代码
- 后焦点更改为IE 11中的地址栏,而不是转移到表单中的下一个控件
- 使用javascript取消隐藏和隐藏表单中的某些元素
- 如何读取表单中的输入数据
- 使用html表单中的参数调用JavaScript函数
- 单击表单中的按钮会导致页面刷新
- 从表单中获取用户输入执行计算(操作顺序)并输出回该表单
- 获取中继器控件ASP.Net中显示的单击项目文本框
- 如何将一个表单中的项目添加到 Rails 中另一个表单的下拉选项
- Jquery脚本删除表单中的所有项目'的收藏
- 谷歌表单:将项目添加到响应中
- 在 foreach 中选择特定的单选项目
- 如何在 Javascript Metro 应用程序中检测列表视图中项目的右键单击
- 如何让我在我的画廊中针对 setInterval 单击项目符号
- 从jquery表单中获取选中的项目
- 试图使嵌入在表单中的表中的每一行可单击,以使项目按设计工作(没有提交按钮)
- 如何从外部json文件中获取id数据,并在url中显示id号,当使用jQuery单击项目时
- 如何使用带有输入字段的表单将项目推送到控制器数组中,该表单将显示在视图中
- sencha touch:在表单中使用复选框管理项目列表
- 如何在asp.net中使用javascript显示与所单击项目相关的信息