如何找出数值下降的元素?

huangapple go评论55阅读模式
英文:

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:

&lt;table
class=&quot;hoverable child-table&quot;
style=&quot;background-color: white; border-radius: 8px; width: 100%;&quot;
&gt;
&lt;tr
cdkDropList
cdkDropListSortingDisabled
cdkDropListLockAxis=&quot;x&quot;
cdkDropListOrientation=&quot;horizontal&quot;
(cdkDropListSorted)=&quot;f($event)&quot;
class=&quot;container&quot;
&gt;
&lt;th style=&quot;background-color: rgb(244, 243, 243); max-width: 5%;&quot;&gt;MON&lt;/th&gt;
&lt;td
style=&quot;font-family: &#39;belgrano&#39;, sans-serif; max-width: 5%; border-right: 1px solid black;&quot;
&gt;
NULL
&lt;/td&gt;
&lt;td
id=&quot;BH&quot;
style=&quot;font-family: &#39;belgrano&#39;, sans-serif; max-width: 5%; border-left: 1px solid black;&quot;
(cdkDropListDropped)=&quot;onDrop(&#39;BH&#39;, $event)&quot;
&gt;
{{ BH }}
&lt;/td&gt;
&lt;td
id=&quot;OT&quot;
style=&quot;font-family: &#39;belgrano&#39;, sans-serif; max-width: 5%;&quot;
(cdkDropListDropped)=&quot;onDrop(&#39;OT&#39;, $event)&quot;
&gt;
{{ OT }}
&lt;/td&gt;
&lt;td
id=&quot;UP&quot;
class=&quot;draggable&quot;
style=&quot;font-family: &#39;belgrano&#39;, sans-serif; max-width: 5%; border-left: 1px solid black;&quot;
&gt;
&lt;span
cdkDrag
ocColumnDrag
(cdkDragMoved)=&quot;dragMoved(&#39;UP&#39;, $event)&quot;
[cdkDragData]=&quot;employee.attendance?.[dateFormt(date)][&#39;UP&#39;]&quot;
class=&quot;column&quot;
&gt;
{{ UP }}
&lt;/span&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

TS:

  BH: number = 2;
OT: number = 3;
UP: number = 10;
draggedTdId: string = &#39;&#39;;
draggedTdValue: any = &#39;&#39;;
dragMoved(tdId: string, e: CdkDragMove) {
this.draggedTdId = tdId;
this.draggedTdValue = e.source.data;
console.log(&#39;1&#39;, tdId, e.source.data);
}
onDrop(tdId: string, event: any) {
console.log(&#39;2&#39;, tdId, event);
const draggedTdId = event.item.element.nativeElement.id;
const draggedTdValue = event.item.data;
const droppedTdId = tdId;
console.log(&#39;Dragged TD:&#39;, draggedTdId, &#39;Value:&#39;, draggedTdValue);
console.log(&#39;Dropped TD:&#39;, droppedTdId);
}
f(e: any) {
console.log(&#39;sorted&#39;, e);
}

答案1

得分: 0

以下是您要翻译的内容:

这不是一个回应,只是一个简要解释关于如何使用 cdkDropList 的说明。

我们有两个指令(指令使一个标签成为“超级标签”(具有方法、属性和可以使用事件的标签)):cdkDragList - 我们可以想象成一个“盒子” - 和 cdkDrag - 我们可以想象成一个“球”,这些球在“盒子”中。

我们可以监听发生在“盒子”上的事件,即 cdkDropList: cdkDropListDroppedcdkDropListEnteredcdkDropListExitedcdkDropListSorted

我们还可以监听发生在“球”上的事件,即 cdkDrag: cdkDragDroppedcdkDragEndedcdkDragEnteredcdkDragMoved,等等。

但是,只有当.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)=&quot;onDrop($event)&quot;

Our function onDrop will be like

onDrop(event:CdkDragDrop&lt;T, any&gt;){
...
}

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&lt;T,any&gt; 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

huangapple
  • 本文由 发表于 2023年5月25日 19:10:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76331644.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定