【问题标题】:Drag n' drop between two material tables在两个材料表之间拖放
【发布时间】:2019-04-12 01:03:01
【问题描述】:

我正在尝试在我的项目中实现拖放操作,但对于如何开始有点困惑。在 Angular 7 中,我们现在有了 cdkDragDrop,这似乎很有希望。但是,所有示例都基于一个或多个列表/div。我想将它与两个材料表一起使用 - 这甚至可能吗?还有其他例子吗,other than the ones at Angular Material?

【问题讨论】:

    标签: angular drag-and-drop angular-material angular7 angular-material-table


    【解决方案1】:

    我创建了一个working example at Stackblitz. 但是,从一个数组移动到另一个数组的实际项目与被拖动的项目不同。我想这可能是因为数组中的索引不一定与表中的索引相同?有什么意见吗?

    import {Component, ViewChild} from '@angular/core';
    import {CdkDragDrop, moveItemInArray, transferArrayItem} from '@angular/cdk/drag-drop';
    import { MatTable, MatTableDataSource } from '@angular/material';
    
    export interface PeriodicElement {
      name: string;
      position: number;
      weight: number;
      symbol: string;
    }
    
    /**
     * @title Basic use of `<table mat-table>`
     */
    @Component({
      selector: 'table-basic-example',
      styleUrls: ['table-basic-example.css'],
      templateUrl: 'table-basic-example.html',
    })
    export class TableBasicExample {
      @ViewChild(MatTable) table: MatTable<any>;
      displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
      ELEMENT_DATA: PeriodicElement[] = [
      {position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
      {position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O'},
      {position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F'},
      {position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne'},
    ];
    
    ELEMENT_DATA2: PeriodicElement[] = [
      {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
      {position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
      {position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
      {position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
      {position: 5, name: 'Boron', weight: 10.811, symbol: 'B'},
    
    ];
      dataSource = new MatTableDataSource(this.ELEMENT_DATA);
      dataSource2 = new MatTableDataSource(this.ELEMENT_DATA2);
    
        drop(event: CdkDragDrop<string[]>) {
          console.log("event.previousContainer =>", event.previousContainer);
          console.log("event.container =>", event.container)
          console.log("event.container.data =>", event.container.data)
          console.log("event.previousIndex =>", event.previousIndex);
          console.log("event.currentIndex =>", event.currentIndex);
        if (event.previousContainer === event.container) {
          moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
        } else {
          transferArrayItem(event.previousContainer.data,
                            event.container.data,
                            event.previousIndex,
                            event.currentIndex);
        }
        this.dataSource = new MatTableDataSource(this.ELEMENT_DATA);
        this.dataSource2 = new MatTableDataSource(this.ELEMENT_DATA2);
      }
    }
    
    
    /**  Copyright 2018 Google Inc. All Rights Reserved.
        Use of this source code is governed by an MIT-style license that
        can be found in the LICENSE file at http://angular.io/license */
    

    -

    <table mat-table [dataSource]="dataSource"
      class="mat-elevation-z8" cdkDropList #table1="cdkDropList"
      [cdkDropListData]="ELEMENT_DATA" [cdkDropListConnectedTo]="[table2]"
      (cdkDropListDropped)="drop($event)">
    
      <!--- Note that these columns can be defined in any order.
            The actual rendered columns are set as a property on the row definition" -->
    
      <!-- Position Column -->
      <ng-container matColumnDef="position">
        <th mat-header-cell *matHeaderCellDef> No. </th>
        <td mat-cell *matCellDef="let element"> {{element.position}} </td>
      </ng-container>
    
      <!-- Name Column -->
      <ng-container matColumnDef="name">
        <th mat-header-cell *matHeaderCellDef> Name </th>
        <td mat-cell *matCellDef="let element"> {{element.name}} </td>
      </ng-container>
    
      <!-- Weight Column -->
      <ng-container matColumnDef="weight">
        <th mat-header-cell *matHeaderCellDef> Weight </th>
        <td mat-cell *matCellDef="let element"> {{element.weight}} </td>
      </ng-container>
    
      <!-- Symbol Column -->
      <ng-container matColumnDef="symbol">
        <th mat-header-cell *matHeaderCellDef> Symbol </th>
        <td mat-cell *matCellDef="let element"> {{element.symbol}} </td>
      </ng-container>
    
      <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
      <tr mat-row *matRowDef="let row; columns: displayedColumns;" cdkDrag></tr>
    </table>
    <hr>
    <table mat-table [dataSource]="dataSource2" class="mat-elevation-z8"
      cdkDropList #table2="cdkDropList" [cdkDropListData]="ELEMENT_DATA2"
      [cdkDropListConnectedTo]="[table1]" (cdkDropListDropped)="drop($event)">
    
      <!--- Note that these columns can be defined in any order.
            The actual rendered columns are set as a property on the row definition" -->
    
      <!-- Position Column -->
      <ng-container matColumnDef="position">
        <th mat-header-cell *matHeaderCellDef> No. </th>
        <td mat-cell *matCellDef="let element"> {{element.position}} </td>
      </ng-container>
    
      <!-- Name Column -->
      <ng-container matColumnDef="name">
        <th mat-header-cell *matHeaderCellDef> Name </th>
        <td mat-cell *matCellDef="let element"> {{element.name}} </td>
      </ng-container>
    
      <!-- Weight Column -->
      <ng-container matColumnDef="weight">
        <th mat-header-cell *matHeaderCellDef> Weight </th>
        <td mat-cell *matCellDef="let element"> {{element.weight}} </td>
      </ng-container>
    
      <!-- Symbol Column -->
      <ng-container matColumnDef="symbol">
        <th mat-header-cell *matHeaderCellDef> Symbol </th>
        <td mat-cell *matCellDef="let element"> {{element.symbol}} </td>
      </ng-container>
    
      <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
      <tr mat-row *matRowDef="let row; columns: displayedColumns;" cdkDrag></tr>
    </table>
    
    
    
    
    <!-- Copyright 2018 Google Inc. All Rights Reserved.
        Use of this source code is governed by an MIT-style license that
        can be found in the LICENSE file at http://angular.io/license -->
    

    -

    table {
      width: 100%;
    }
    .cdk-drag-preview {
      box-sizing: border-box;
      border-radius: 4px;
      box-shadow: 0 5px 5px -3px rgba(0, 0, 0, 0.2),
                  0 8px 10px 1px rgba(0, 0, 0, 0.14),
                  0 3px 14px 2px rgba(0, 0, 0, 0.12);
    }
    
    .cdk-drag-placeholder {
      opacity: 0;
    }
    
    .cdk-drag-animating {
      transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
    }
    
    .example-box:last-child {
      border: none;
    }
    
    .example-list.cdk-drop-list-dragging .example-box:not(.cdk-drag-placeholder) {
      transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-10
      • 2019-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-03
      相关资源
      最近更新 更多