Angular 如何在角度拖动过程中显示单列值而不是整行
大家好,我正在处理angular中的一个基本拖放示例,当前的工作是将一行从一个位置拖到另一个位置时,它显示整行。我的要求是,它应该只显示单列值,如“name”,而不是整行 这是我的HTML: dragtable.component.htmlAngular 如何在角度拖动过程中显示单列值而不是整行,angular,Angular,大家好,我正在处理angular中的一个基本拖放示例,当前的工作是将一行从一个位置拖到另一个位置时,它显示整行。我的要求是,它应该只显示单列值,如“name”,而不是整行 这是我的HTML: dragtable.component.html <table cdkDropList (cdkDropListDropped)="drop($event)"> <thead> <tr> <th>Name<
<table cdkDropList (cdkDropListDropped)="drop($event)">
<thead>
<tr>
<th>Name</th>
<th>ID</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let student of Student" cdkDrag >
<td>{{student.name}}</td>
<td>{{student.id}}</td>
<td>{{student.age}}</td>
</tr>
</tbody>
</table>
名称
身份证件
年龄
{{student.name}
{{student.id}
{{学生年龄}
ts文件:
dragtable.component.ts
import { Component } from '@angular/core';
import { CdkDragDrop, moveItemInArray } from '@angular/cdk/drag-drop';
@Component({
selector: 'dragtable-app',
templateUrl: './dragtable.component.html',
styleUrls: ['./dragtable.component.css']
})
export class DragTableComponent {
Student = [
{
name: 'abc',
id: '1',
age: '23'
},
{
name: 'bdc',
id: '2',
age: '24'
},
{
name: 'abg',
id: '3',
age: '25'
},
{
name: 'dty',
id: '4',
age: '26'
},
];
drop(event: CdkDragDrop<string[]>) {
moveItemInArray(this.Student, event.previousIndex, event.currentIndex);
}
}
从'@angular/core'导入{Component};
从“@angular/cdk/drag drop”导入{CdkDragDrop,moveItemInArray};
@组成部分({
选择器:“可拖动应用程序”,
templateUrl:'./dragtable.component.html',
样式URL:['./dragtable.component.css']
})
导出类DragTableComponent{
学生=[
{
姓名:“abc”,
id:'1',
年龄:'23'
},
{
名称:“bdc”,
id:'2',
年龄:24岁
},
{
名称:“abg”,
id:'3',
年龄:25岁
},
{
名称:“dty”,
id:'4',
年龄:'26'
},
];
drop(事件:CdkDragDrop){
moveItemInArray(this.Student、event.previousIndex、event.currentIndex);
}
}
您可以在内部使用ngIf,如果拖动只显示一个td,而行跨度为else,则显示所有td。我还没有测试过这个,但是这个可以work@Noob编码器我必须在第一个td中添加cdkDrag项,即{{{student.name}},它按照要求为我工作。感谢您的回复,这让我以不同的方式思考如何获得此解决方案!很高兴听到这个消息。@Noob Coder所以我的解决方案只适用于第一行,但当我试图拖动其他两行,即ID和age时,它不起作用。您能否对此进行测试并提出解决方案?