polymer-2.x - Polymer 2.0 将 id 动态分配给在 dom-repeat 模板中使用 this.shadowRoot.querySelector() 在数组上的观察方法中返回 null
问题描述
我正在尝试在 Polymer2.0 应用程序中实现 Sortablejs,其中我动态地将 id 分配给 dom 重复模板中的 ul 元素。
我遇到的问题是我无法在数据列表 todos 的观察者方法中访问带有 id 的 ul。
我尝试了 document.getElementId, this.$.querySelector 但两者都返回 null 并且 Sortablejs 创建方法会抛出需要 html el 的错误。我也试过 this.shadowRoot.querySelector(child.id) ...它也返回 null。
自过去 2 天以来,我一直在努力使其正常工作。我需要做什么来修复它?请帮忙。
<!DOCTYPE html>
<dom-module id="t-page">
<script src="http://SortableJS.github.io/Sortable/Sortable.js"> .
</script>
<template is="dom-bind">
<style include="shared-styles">
....
</style>
<div class="board__sprint">
<template is="dom-repeat" items="{{todos}}" as="row">
<div class="list">
<div class="list-content">
<ul id="[[row.id]]">
<!-- id is v789878 (some random unique number prefix by v per row -->
<template is="dom-repeat" items="{{row.tasks}}" as="todo">
<li>
<div class="ticket" data-index$="{{todo.id}}">
<paper-card style="float:center; width: 100%;" class="singleColor" data-index$="{{todo}}"
data-index$="{{row}}">
<h7 class="banksTitle" style="color: black; font-size: 12px; text-align:left;">
<b>[{{index}}]</b> [[todo.actTitle]]
</h7>
<h7 class="banksTitle" style="color: grey; font-size: 12px; text-align:left;">
[[todo.actDesc]]
</h7>
</paper-card>
</div>
</li>
</template>
</ul>
</div>
<div class="addTicket">
<paper-button raised class="blue" on-tap="_addTicketDialog" row={{row}}>Add Ticket</paper-button>
</div>
</div>
</div>
</template>
</div>
<script>
/**
* @polymer
* @extends HTMLElement
*/
class TPage extends Polymer.Element {
static get is() {
return 't-page';
}
static get properties() {
return {
todos: {
type: Object,
notify: true,
observer: '_todosChanged'
},
....
}
_todosChanged() {
console.log('this.todos.length = ' + this.todos.length);
if (this.todos !== null || this.todos !== undefined) {
var self = this;
this.todos.forEach(function (child) {
Sortable.create(self.$.querySelector(child.id), {
group: 'shared'
});
});
}
}
window.customElements.define(TPage.is, TPage);
解决方案
为了选择你的元素,有三点:
- ID 标准:该值在元素的主子树中的所有 ID 中必须是唯一的,并且必须至少包含
one character
. 该值不得包含任何空格字符...
<ul id="myid[[row.id]]">
- dom-repeat 的渲染时间。为此,您可以包装您的函数
setTimeout
以确保在 dom-repeat 中呈现您的所有项目。
_todosChanged() {
setTimeout(()=>{
console.log('this.todos.length = ' + this.todos.length);
if (this.todos !== null || this.todos !== undefined) {
var self = this
this.todos.forEach(child=> {
console.log(child.id)
var selector = this.shadowRoot.querySelector('#myid'+child.id);
Sortable.create(selector, {
group: 'shared'
});
});
}
})
}
- 您的函数的上下文未绑定到 Polymer 对象,因此最短的解决方案是使用数组函数(ES6):
this.todos.forEach(child=> {....
代替:
this.todos.forEach(function (child) {...
推荐阅读
- rust - 取决于枚举是什么变体,如何在其中返回 2 种不同的可能类型之一?
- java - Maven-versions-plugin:用于否定不遵循特定标签格式的版本的正则表达式
- mysql - 求和条件排除没有正和的行
- javascript - javascript视频标签源更改
- java - JPA 多对一关系和外部表的问题
- python - Pytorch:尽管矩阵的大小匹配(m1:[256 x 200],m2:[256 x 200]),但大小不匹配错误
- swift - 在 Swift 中将地图从 Firestore 转换为字典
- javascript - jQuery:使用 jQuery 保存表单会使表单无效
- android - 我不能在 androidx.appcompat:appcompat:1.1.0 上使用 API 反射
- c - 如何在函数内初始化结构数组?