【问题标题】:Movable Expansion-Panel in Angular using Material使用材料的 Angular 中的可移动扩展面板
【发布时间】:2020-04-19 19:46:03
【问题描述】:

我有一个对象数组

  listA  = [
  { name : "Pink Floyd", 
    sequence : 1,
    favSong : "Shine On You", 
    favMember :[
    {artist : "Nick Mason"}
  ]},
    { name : "Guns and Roses", 
    sequence  :2,
    favSong : "November Rain", 
    favMember :[
    {artist : "Slash"}
  ]},
    { name : "Led Zeppelin",
    sequence  :3,
    favSong : "Stairway To heaven", 
    favMember :[
    {artist : "Jimmy page"}
  ]},
]

我将它们显示为 UI 上的多个扩展面板。我想添加一个功能,我可以在其中拖动单个扩展面板并更改名称的显示顺序。我尝试使用 sortableJs,但我无法弄清楚。

这里是 HTML

<div>
    <strong>My favorite bands in order  : - </strong>
</div>
<hr />
<hr />



<div class="row mt-3">

            <div class="nomarLR mt-1 float-left col-12" *ngFor="let x of listA;let i =index">
                <mat-accordion multi="false" class="cus-accordion-style2 col-12" class="nomarLR nomarTB">
                    <mat-expansion-panel class="nomarLR nomarTB">
                        <mat-expansion-panel-header class="nomarLR nomarTB">
                            <mat-panel-title class="nomarLR nomarTB">
                                {{x.name}}
                            </mat-panel-title>
                        </mat-expansion-panel-header>
                        <div *ngFor="let y of listA">
                            <span *ngFor="let z of y.favMember"> {{z.artist}} </span>
                        </div>
                    </mat-expansion-panel>
                </mat-accordion>
            </div>

</div>

<!-- why is multi not working ??-->

谁能帮帮我。这是Stackblitz 此外,当用户移动扩展面板时,它应该将数组对象推送到具有更新顺序的不同数组中。

【问题讨论】:

  • 我喜欢你数组的内容。
  • 谢谢。摇滚乐是爱。有什么解决办法吗?
  • 遗憾的是,我没有任何线索。不过祝你好运..
  • 你帮了我很多,新年快乐:)
  • 新年快乐!

标签: html angular typescript user-interface material-design


【解决方案1】:

为拖放事件添加 (cdkDropListDropped)="drop($event)" 事件并为要拖动的 dom 元素添加 cdkDrag

HTML:

<div cdkDropList class="example-list" (cdkDropListDropped)="drop($event)"> // added html for drag and drop
  <div class="nomarLR mt-1 float-left col-12" *ngFor="let x of listA;let i =index" cdkDrag> // cdkDrag elements which are draggable.
                <mat-accordion multi="false" class="cus-accordion-style2 col-12"
                    class="nomarLR nomarTB">
                    <mat-expansion-panel class="nomarLR nomarTB">
                        <mat-expansion-panel-header class="nomarLR nomarTB">
                            <mat-panel-title class="nomarLR nomarTB">
                                {{x.name}}
              </mat-panel-title>
                        </mat-expansion-panel-header>
          <div *ngFor= "let y of listA">
            <span *ngFor="let z of y.favMember"> {{z.artist}} </span>
          </div>
                    </mat-expansion-panel>
                </mat-accordion>
        </div>
 </div>

TS

sortedArray = []


listA  = [
{ name : "Pink Floyd", 
favSong : "Shine On You", 
favMember :[
{artist : "Nick Mason"}
]},
{ name : "Guns and Roses", 
favSong : "November Rain", 
favMember :[
{artist : "Slash"}
]},
{ name : "Led Zeppelin", 
favSong : "Stairway To heaven", 
favMember :[
{artist : "Jimmy page"}
]},

drop(event: CdkDragDrop<string[]>) {
    moveItemInArray(this.listA, event.previousIndex, event.currentIndex);
}

【讨论】:

  • 您能否解释一下您为使 OP 发挥作用而错过的哪些工作? IMO,它对未来的读者也有帮助。
  • @NicholasK cmets 以及如何实施已添加。
  • @MenimE 是有用的解决方案,如果对您有帮助,请接受并投票
猜你喜欢
  • 1970-01-01
  • 2018-03-23
  • 1970-01-01
  • 2017-11-18
  • 1970-01-01
  • 1970-01-01
  • 2018-08-23
  • 1970-01-01
  • 2019-04-30
相关资源
最近更新 更多