简体   繁体   English

带有 web 组件、发光元素、polymer 的拖放列表

[英]Drag and drop list with web-components, lit-element, polymer

I am trying to create a simple list of 'cards' that I can re-order by dragging and dropping.我正在尝试创建一个简单的“卡片”列表,我可以通过拖放重新排序。

I came across this fluid-grid web component and have had some success.我遇到了这个流体网格 web 组件并取得了一些成功。 I have created a list of cards and I can drag them around.我已经创建了一个卡片列表,我可以将它们拖动到周围。 The problem is, how to handle the 'drop' event so that I can update the location of the dropped item in the list.问题是,如何处理“放置”事件,以便我可以更新列表中放置项目的位置。

Here is my code:这是我的代码:

import {LitElement, html, customElement, property, css} from 'lit-element';
import { repeat } from 'lit-html/directives/repeat'
import '@fluidnext-polymer/paper-grid';
import '@polymer/paper-card/paper-card'

interface VaadinRouterLocation {
  baseUrl?: string
  params?: {
    id: string
  }
  pathname?: string
  route?: {
    path: string
    component: string
    search: string
  }
}
@customElement('my-element')
export class MyElement extends LitElement {
  static styles = css`
    :host {
      display: block;
      border: solid 1px gray;
      padding: 16px;
      max-width: 800px;
    }
  `;

  @property({type: Object})
  location:VaadinRouterLocation = {};
  constructor() {
    super(); 
  }
  @property({type: Array})
  tasks = [
    {"name": "Task 1", "sequence": 1, "id": "A"},
    {"name": "Task 2", "sequence": 2, "id": "B"},
    {"name": "Task 3", "sequence": 3, "id": "C"},
  ] 
  render() {
    return html`
      <div id="container" @ondrop="(e) => console.log(e.target)">
        <paper-grid animated id="grid" draggable cell-margin="5" col-count="2" row-count="${this.tasks.length}" ondrop="(e) => console.log(e.target)">
        ${repeat(this.tasks, (task) => html`
          <paper-card col="0" row=${task.sequence} height="1" width="1" @drop="${this.handleDrop}">
              <div class="card-content">${task.name}</div>
            </paper-card>
        `)}
        </paper-grid>
    </div>
    `
  }


  handleDrop(e:any) {
    console.log("Event", e)
  }
  allowDrop(e:any){
    e.preventDefault()
  }
}

declare global {
  interface HTMLElementTagNameMap {
    'my-element': MyElement;
  }
}

In LitElement you don't need the on... prefix.在 LitElement 中,您不需要on...前缀。 Replace @ondrop and ondrop with @drop - currently that's only on the <paper-card> element.@ondropondrop替换为@drop - 目前仅在<paper-card>元素上。

Also note that if you want to access the elements being dragged over you may need to use e.relatedTarget.getRootNode().host to get the actual custom element, as the event fires from an element that may be nested in the shadow DOM for your component.另请注意,如果您想访问被拖动的元素,您可能需要使用e.relatedTarget.getRootNode().host来获取实际的自定义元素,因为事件从可能嵌套在 shadow DOM 中的元素触发你的组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM