英文:
How to find on which element value dropped?
问题
我正在尝试进行简单的拖放计算。
用户可以向上拖动并允许放置在BH / OT上。但问题是找不到用户放置的ID,*onDrop()*函数不起作用。
假设如果拖动了UP(10)值并放置在BH(2)上,期望更新后的UP和BH值分别为0和12。
StackBlitz 链接
尝试的代码:
<table
class="hoverable child-table"
style="background-color: white; border-radius: 8px; width: 100%;"
>
<tr
cdkDropList
cdkDropListSortingDisabled
cdkDropListLockAxis="x"
cdkDropListOrientation="horizontal"
(cdkDropListSorted)="f($event)"
class="container"
>
<th style="background-color: rgb(244, 243, 243); max-width: 5%;">MON</th>
<td
style="font-family: 'belgrano', sans-serif; max-width: 5%; border-right: 1px solid black;"
>
NULL
</td>
<td
id="BH"
style="font-family: 'belgrano', sans-serif; max-width: 5%; border-left: 1px solid black;"
(cdkDropListDropped)="onDrop('BH', $event)"
>
{{ BH }}
</td>
<td
id="OT"
style="font-family: 'belgrano', sans-serif; max-width: 5%;"
(cdkDropListDropped)="onDrop('OT', $event)"
>
{{ OT }}
</td>
<td
id="UP"
class="draggable"
style="font-family: 'belgrano', sans-serif; max-width: 5%; border-left: 1px solid black;"
>
<span
cdkDrag
ocColumnDrag
(cdkDragMoved)="dragMoved('UP', $event)"
[cdkDragData]="employee.attendance?.[dateFormt(date)]['UP']"
class="column"
>
{{ UP }}
</span>
</td>
</tr>
</table>
TS:
BH: number = 2;
OT: number = 3;
UP: number = 10;
draggedTdId: string = '';
draggedTdValue: any = '';
dragMoved(tdId: string, e: CdkDragMove) {
this.draggedTdId = tdId;
this.draggedTdValue = e.source.data;
console.log('1', tdId, e.source.data);
}
onDrop(tdId: string, event: any) {
console.log('2', tdId, event);
const draggedTdId = event.item.element.nativeElement.id;
const draggedTdValue = event.item.data;
const droppedTdId = tdId;
console.log('拖动的单元格ID:', draggedTdId, '值:', draggedTdValue);
console.log('放置的单元格ID:', droppedTdId);
}
f(e: any) {
console.log('排序后', e);
}
英文:
I am trying to do simple drag and drop calculation.
user can drag UP and allowed to drop on BH / OT. But issue is couldn't find the ID where user drop, not working onDrop() func.
Suppose if UP(10) value draged and drop on BH(2), desire updated UP and BH value would be 0 and 12 respectively.
StackBlitz link
Tried Code:
<table
class="hoverable child-table"
style="background-color: white; border-radius: 8px; width: 100%;"
>
<tr
cdkDropList
cdkDropListSortingDisabled
cdkDropListLockAxis="x"
cdkDropListOrientation="horizontal"
(cdkDropListSorted)="f($event)"
class="container"
>
<th style="background-color: rgb(244, 243, 243); max-width: 5%;">MON</th>
<td
style="font-family: 'belgrano', sans-serif; max-width: 5%; border-right: 1px solid black;"
>
NULL
</td>
<td
id="BH"
style="font-family: 'belgrano', sans-serif; max-width: 5%; border-left: 1px solid black;"
(cdkDropListDropped)="onDrop('BH', $event)"
>
{{ BH }}
</td>
<td
id="OT"
style="font-family: 'belgrano', sans-serif; max-width: 5%;"
(cdkDropListDropped)="onDrop('OT', $event)"
>
{{ OT }}
</td>
<td
id="UP"
class="draggable"
style="font-family: 'belgrano', sans-serif; max-width: 5%; border-left: 1px solid black;"
>
<span
cdkDrag
ocColumnDrag
(cdkDragMoved)="dragMoved('UP', $event)"
[cdkDragData]="employee.attendance?.[dateFormt(date)]['UP']"
class="column"
>
{{ UP }}
</span>
</td>
</tr>
</table>
TS:
BH: number = 2;
OT: number = 3;
UP: number = 10;
draggedTdId: string = '';
draggedTdValue: any = '';
dragMoved(tdId: string, e: CdkDragMove) {
this.draggedTdId = tdId;
this.draggedTdValue = e.source.data;
console.log('1', tdId, e.source.data);
}
onDrop(tdId: string, event: any) {
console.log('2', tdId, event);
const draggedTdId = event.item.element.nativeElement.id;
const draggedTdValue = event.item.data;
const droppedTdId = tdId;
console.log('Dragged TD:', draggedTdId, 'Value:', draggedTdValue);
console.log('Dropped TD:', droppedTdId);
}
f(e: any) {
console.log('sorted', e);
}
答案1
得分: 0
以下是您要翻译的内容:
这不是一个回应,只是一个简要解释关于如何使用 cdkDropList 的说明。
我们有两个指令(指令使一个标签成为“超级标签”(具有方法、属性和可以使用事件的标签)):cdkDragList - 我们可以想象成一个“盒子” - 和 cdkDrag - 我们可以想象成一个“球”,这些球在“盒子”中。
我们可以监听发生在“盒子”上的事件,即 cdkDropList: cdkDropListDropped
、cdkDropListEntered
、cdkDropListExited
和 cdkDropListSorted
。
我们还可以监听发生在“球”上的事件,即 cdkDrag: cdkDragDropped
、cdkDragEnded
、cdkDragEntered
、cdkDragMoved
,等等。
但是,只有当.html标签具有指令时才能使用这些事件(在您的代码中我看不到它)。
在文档中,我们可以捕获不同事件的参数,所以,当我们有,例如
(cdkDropListDropped)="onDrop($event)"
我们的 onDrop
函数将会是这样的
onDrop(event: CdkDragDrop<T, any>) {
...
}
在文档中,"Output" 被定义为
@Output('cdkDropListDropped') dropped: EventEmitter<CdkDragDrop<T, any>>
那么,函数的参数类型是 CdkDragDrop<T,any>
,并具有属性(请查看 "interface" cdkDragDrop):container
(“球”被放置在哪个“盒子”中)、currentIndex
(在哪个索引)、previousContainer
(“球”离开的盒子)、previousIndex
(在哪个索引),等等。
嗯,没有人谈论列表。我们可以有一个唯一的盒子,两个盒子,一个盒子里有一个唯一的球...
检查一下您的代码。实际上,我不知道您是否需要三个“盒子”,每个盒子里都有一个唯一的“球”,还是一个唯一的“盒子”,里面有三个“球”,但请记住,如果您的标签不是 cdkDropList,您不能使用事件(cdkDropListDropped)。
英文:
It's not a response, only a brief explanation if how works with cdkDropList.
We have two directives (a directive makes that a tag becomes a "super tag" (a tag that have methods, properties and we can use events): cdkDragList -we can imagine like a "box"- and cdkDrag -we can imagine like a "ball" that there're inside the "box".
We can listen events happens to the "box" the cdkDropList: cdkDropListDropped
, cdkDropListEntered
, cdkDropListExited
and cdkDropListSorted
We can listen events happens to the "balls", the cdkDrag: cdkDragDropped
, cdkDragEnded
, cdkDragEntered
, cdkDragMoved
,...
But only if the tag of .html have the directive (in your code I can not see it)
In the docs we can capture the arguments of the different events, so, when we have, e.g.
(cdkDropListDropped)="onDrop($event)"
Our function onDrop will be like
onDrop(event:CdkDragDrop<T, any>){
...
}
See in the docs that the "Output" are defined like
> @Output('cdkDropListDropped') dropped: EventEmitter<CdkDragDrop<T,
> any>>
Well, the argument of the function is of kind CdkDragDrop<T,any>
and have the properties (see the "interface" cdkDragDrop): container
(the "box" where is dropped the "ball"), currentIndex
(in wich index), previousContainer
(the box from the "ball" leave), previousIndex
(in wich index),...
Well, nobody talk about list. we can to have an unique box, two box, one box with a unique ball inside...,
Check your code. Really I don't know if you need three "box" with an unique "ball" in each, or an unique "box" with three "balls", but rememeber you can not use the event (cdkDropListDropped) if your tag is not a cdkDropList
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论