微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在拖动项目时向上或向下滚动页面,以便用户可以在 Angular 7 cdk 拖放中放置不在当前视图中的项目

如何解决如何在拖动项目时向上或向下滚动页面,以便用户可以在 Angular 7 cdk 拖放中放置不在当前视图中的项目

请在我的 html 和 ts 文件下面找到模拟数据。 在列表中拖动时,我想放置在当前视图中未显示的项目上,因为我需要在拖动时向上或向下滚动页面。 我如何在下面的代码中实现滚动功能 请在下面找到我的 ts 和 html 文件和图像的详细信息以供参考

**page-grid-template.component.html**
<div cdkDropListGroup>
  <div class="grid-layout-col">
    <div *ngFor="let col of tempalteConfig?.structure?.col;let i = index"
      [ngStyle]="{'height': structureMap[i]?.height+'px'}" class="{{structureMap[i]?.size + ' layout-margin'}}">
      <div *ngFor="let item of col;let ii = index" (click)="onClick(item,templateDetails[item])"
        [ngClass]="{'layout-vertical':col.length>1}" cdkDropList [cdkDropListData]="{data: item,index: ii}"
        [cdkDropListEnterPredicate]="canDropItem(templateDetails[item])"
        (cdkDropListDropped)="dropListDropped($event)">
        <div cdkDrag [cdkDragdisabled]="templateDetails[item].isHidden || isEditMode" [cdkDragData]="templateDetails[item]" 
        (cdkDragStarted)="cdkDragStarted($event)">
          <div [ngStyle]="{'min-height.px':placeHolderHeight,'min-width.px':placeHolderWidth}" class="drag-placeholder"
            *cdkDragPlaceholder></div>
          <sx-grid-layout [data]='templateDetails[item]' layout='col'></sx-grid-layout>
        </div>
        <div class="{{'Now-edit-mode-on '+ templateDetails[item].size+'-edit'}}"
          *ngIf="templateDetails[item].tempId===selectId"
          [ngStyle]="{'height':(templateDetails[item].height+'px'),'margin-top':(-templateDetails[item].height+'px')}">
        </div>
      </div>
    </div>
  </div>
  <div class="grid-layout-row">
    <div *ngFor="let row of tempalteConfig?.structure?.row" class="layout-grid">
      <div *ngFor="let item of row; let ii = index" (click)="onClick(item,templateDetails[item])"
        class="Equal layout-margin" [ngStyle]="{'height': (templateDetails[item]?.height||191)+'px'}" cdkDropList
        [cdkDropListData]="{data: item,index: ii}" 
        [cdkDropListEnterPredicate]="canDropItem(templateDetails[item])"
        (cdkDropListDropped)="dropListDropped($event)">
        <div cdkDrag [cdkDragdisabled]="templateDetails[item].isHidden || isEditMode" [cdkDragData]="templateDetails[item]" (cdkDragStarted)="cdkDragStarted($event)">
          <div [ngStyle]="{'min-height.px':placeHolderHeight,'min-width.px':placeHolderWidth}" class="drag-placeholder"
            *cdkDragPlaceholder></div>
          <sx-grid-layout [data]='templateDetails[item]' layout='row'></sx-grid-layout>
        </div>
        <div class="{{'Now-edit-mode-on '+ templateDetails[item].size+'-edit'}}"
          *ngIf="templateDetails[item].tempId===selectId"
          [ngStyle]="{'height':((templateDetails[item].height||191)+'px'),'margin-top':(-(templateDetails[item].height||191)+'px')}">
        </div>
      </div>
    </div>
  </div>
</div>
**page-grid-template.component.ts**
export class PageGridTemplateComponent implements OnInit {

  @Input('data') templateConfig: any;
  @ViewChildren('list') lists: QueryList<CdkDropList>;
  public templateDetails: any = {};
  private sizeMap = {
    'L': 'Large','M': 'Medium','S': 'Small','R': 'Equal'
  };
  private structureMap = {};
  public isRowLayoutVisible: Boolean = false;
  public screenWidth: any;

  constructor(private messageService: MessageService) {
    this.screenWidth = screen.width;
  }

  @HostListener('window:resize',['$event']) onResize(event) {
    this.screenWidth = screen.width;
  }

  ngOnInit() {
    this.messageService.notify$.subscribe(item => {
      if (item && item.option && item.option === 'grid-layout-visible') {
        this.isRowLayoutVisible = item.value.visible;
        if (!item.value.isNotScroll) {
          this.scrollToView(item.value.visible);
        }
      }
    });
    if (this.templateConfig && this.templateConfig.structure) {
      this.formatTemplateConfig(this.templateConfig);
      this.calculateHeight(this.templateConfig.structure.col);
    }
    console.log(this.templateDetails);
  }

  dropListDropped(event: CdkDragDrop<string[]>) {
     console.log(event);
    console.log(event.prevIoUsContainer);
    console.log(event.container);
    console.log(event.prevIoUsIndex);
    console.log(event.currentIndex);
    console.log(event.prevIoUsContainer === event.container);
    console.log(this.templateDetails);
      const prev = event.prevIoUsContainer.data[event.prevIoUsIndex];
      const current = event.container.data[event.currentIndex];
      const el = this.templateConfig.content;
      const prevElIndex = _.findindex(el,{ 'section_name': prev });
      const currentElIndex = _.findindex(el,{ 'section_name': current });
      el[prevElIndex].section_name = current;
      el[currentElIndex].section_name = prev;
      this.formatTemplateConfig(this.templateConfig);
      this.calculateHeight(this.templateConfig.structure.col);
  }

  formatTemplateConfig(data) {
    if (data && data.content && data.content.length) {
      data.content.forEach(element => {
        if (element.section_name) {
          if (!element.section_content) {
            element.section_content = {};
          }
          element.section_content['size'] = '';
          const char = element.section_name.charat(0);
          if (char && this.sizeMap[char]) {
            element.section_content['size'] = this.sizeMap[char];
          }
          this.templateDetails[element.section_name] = element.section_content;
        }
      });
    }
  }

  calculateHeight(structure = []) {
    structure.forEach((element,idx) => {
      const height = ((460 - (6 * (element.length - 1))) / (element.length));
      if (element.length) {
        const char = element[0].charat(0);
        this.structureMap[idx] = {};
        this.structureMap[idx]['height'] = height - 1;
        if (char && this.sizeMap[char]) {
          this.structureMap[idx]['size'] = this.sizeMap[char];
        }
      }
      element.forEach(item => {
        if (this.templateDetails[item]) {
          this.templateDetails[item]['height'] = height;
        } else {
          this.templateDetails[item] = this.structureMap[idx];
        }
      });
    });
  }
}

**My templateConfig Data format**
 {
      
        "structure": {
          "col": [
            [
              "L1"
            ],[
              "M1","M2"
            ],[
              "S1","S2","S3"
            ]
          ],"row": [
            [
              "R1-1","R1-2","R1-3","R1-4"
            ],[
              "R2-1","R2-2","R2-3","R2-4"
            ]
          ]
        },"content": [
          {
            "section_name": "L1","section_content": {}
          },{
            "section_name": "M1",{
            "section_name": "M2","section_content": {}
             
          },{
            "section_name": "S1","section_content": {}
            
            }
          ]
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?