Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unable to render the top and left position of the div with cdkdrag #14674

Closed
anandsunku opened this issue Dec 29, 2018 · 7 comments · Fixed by #14696
Closed

unable to render the top and left position of the div with cdkdrag #14674

anandsunku opened this issue Dec 29, 2018 · 7 comments · Fixed by #14696
Assignees
Labels
P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent

Comments

@anandsunku
Copy link

anandsunku commented Dec 29, 2018

What is the expected behavior?

i am trying to store and restore the position of the div over sessions using local storage.

What is the current behavior?

after rendering the elements with stored left.px and top.px the elements are jumping.

` ngOnInit() {
let temp: any[] = [];
let tempstr = '';

tempstr = localStorage.getItem('things');
temp = JSON.parse(tempstr);
console.log(temp);
if (!temp) {
  this.things.push(
    {
      text: 'first thing',
      id: 10, top: 100, left: 50,
      items: ['things like that '],
    });
  this.things.push({text: 'second thing', id: 20, top: 400, left: 200});
  this.things.push({text: 'third ', id: 30, top: 400, left: 200});
  this.things.push({text: 'fourth', id: 40, top: 400, left: 200});
  this.things.push({text: 'fifth', id: 50, top: 400, left: 200});
  this.things.push({text: 'sixth', id: 60, top: 400, left: 200});
} else {
  this.things = temp;
}
console.log(this.things);

}`


  onDragEnd(event: CdkDragEnd, id) {
    this.dirty = true;

    console.log('drag end');
    this.things.forEach( thing => {
      if (thing.id === id) {
        //--> problematic code next 2 lines <---
        thing.top = event.source.element.nativeElement.getBoundingClientRect().top;
        thing.left = event.source.element.nativeElement.getBoundingClientRect().left;

        localStorage.setItem('things', JSON.stringify(this.things));
      }
    });
  }

<body>
<mat-toolbar color="primary">{{title}}</mat-toolbar>

<div>
  top = {{top}}
  left = {{left}}
</div>

  <div
    class="example-box" cdkDrag
    *ngFor="let thing of things"
    (cdkDragEnded)="onDragEnd($event, thing.id)"
    (cdkDragMoved)="onDragMoved($event, thing.id)"
     [ngStyle]="{'top.px':thing.top, 'left.px':thing.left}">

    <div style="background-color: #3748ac;color: white; width: 100%;height: 50px; padding-left: 10px">
      <span style="padding-left: 10px;padding-right: 10px;padding-top: 20px;vertical-align: middle;display: inline-block;line-height: normal;">{{thing.text}}</span>
    </div>
    <div>
      <p>thing.top = {{thing.top}} <br>thing.left = {{thing.left}}</p>
    </div>
    <div style="width: 80%;">
      <ul>
        <li *ngFor="let item of thing.items">{{item}}</li>
      </ul>
    </div>
  </div>

  <div class="example-box" cdkDrag>
    hai
  </div>

</body>
</html>

[ngStyle]="{'top.px':thing.top, 'left.px':thing.left}">

What are the steps to reproduce?

Providing a StackBlitz reproduction is the best way to share your issue.

StackBlitz starter: https://goo.gl/wwnhMV

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

Is there anything else we should know?

@crisbeto
Copy link
Member

The CDK uses transform to move the element around. You should be able to restore its position by setting style="transform: translate3d({{left}}, {{top}})".

@anandsunku
Copy link
Author

anandsunku commented Dec 31, 2018

Wow that works... thanks a lot!
But i had to subtract the offset top to make it sit exactly in that location, is that ok to do it?

onDragEnd(event: CdkDragEnd, id) {
/.......

div's .top = event.source.element.nativeElement.getBoundingClientRect().top - 
 event.source.element.nativeElement.offsetTop;

/.....
}

crisbeto added a commit to crisbeto/material2 that referenced this issue Jan 2, 2019
…draggable

Adds an API that allows the consumer to get the current position of a standalone draggable and to set it. This is useful for cases where the dragged position should be preserved when the user navigates away and then restored when they return.

Fixes angular#14420.
Fixes angular#14674.
@crisbeto
Copy link
Member

crisbeto commented Jan 2, 2019

Glad that it worked out. I've submitted #14696 that adds a proper API for it since I can see this coming up in the future.

@crisbeto crisbeto self-assigned this Jan 2, 2019
@crisbeto crisbeto added P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent has pr labels Jan 2, 2019
crisbeto added a commit to crisbeto/material2 that referenced this issue Jan 2, 2019
…draggable

Adds an API that allows the consumer to get the current position of a standalone draggable and to set it. This is useful for cases where the dragged position should be preserved when the user navigates away and then restored when they return.

Fixes angular#14420.
Fixes angular#14674.
crisbeto added a commit to crisbeto/material2 that referenced this issue Jan 2, 2019
…draggable

Adds an API that allows the consumer to get the current position of a standalone draggable and to set it. This is useful for cases where the dragged position should be preserved when the user navigates away and then restored when they return.

Fixes angular#14420.
Fixes angular#14674.
@anandsunku
Copy link
Author

The CDK uses transform to move the element around. You should be able to restore its position by setting style="transform: translate3d({{left}}, {{top}})".

yes..having an API is great. By the way after using the animations it works ...but sometimes the elements again jump. and surprisingly after reload of the page it starts working how do i debug the rendering of the jumping behaviour

crisbeto added a commit to crisbeto/material2 that referenced this issue Jan 12, 2019
…draggable

Adds an API that allows the consumer to get the current position of a standalone draggable and to set it. This is useful for cases where the dragged position should be preserved when the user navigates away and then restored when they return.

Fixes angular#14420.
Fixes angular#14674.
crisbeto added a commit to crisbeto/material2 that referenced this issue Jan 12, 2019
…draggable

Adds an API that allows the consumer to get the current position of a standalone draggable and to set it. This is useful for cases where the dragged position should be preserved when the user navigates away and then restored when they return.

Fixes angular#14420.
Fixes angular#14674.
crisbeto added a commit to crisbeto/material2 that referenced this issue Jan 19, 2019
…draggable

Adds an API that allows the consumer to get the current position of a standalone draggable and to set it. This is useful for cases where the dragged position should be preserved when the user navigates away and then restored when they return.

Fixes angular#14420.
Fixes angular#14674.
crisbeto added a commit to crisbeto/material2 that referenced this issue Jan 19, 2019
…draggable

Adds an API that allows the consumer to get the current position of a standalone draggable and to set it. This is useful for cases where the dragged position should be preserved when the user navigates away and then restored when they return.

Fixes angular#14420.
Fixes angular#14674.
crisbeto added a commit to crisbeto/material2 that referenced this issue Jan 27, 2019
…draggable

Adds an API that allows the consumer to get the current position of a standalone draggable and to set it. This is useful for cases where the dragged position should be preserved when the user navigates away and then restored when they return.

Fixes angular#14420.
Fixes angular#14674.
crisbeto added a commit to crisbeto/material2 that referenced this issue Feb 10, 2019
…draggable

Adds an API that allows the consumer to get the current position of a standalone draggable and to set it. This is useful for cases where the dragged position should be preserved when the user navigates away and then restored when they return.

Fixes angular#14420.
Fixes angular#14674.
crisbeto added a commit to crisbeto/material2 that referenced this issue Feb 10, 2019
…draggable

Adds an API that allows the consumer to get the current position of a standalone draggable and to set it. This is useful for cases where the dragged position should be preserved when the user navigates away and then restored when they return.

Fixes angular#14420.
Fixes angular#14674.
@s001dxp
Copy link

s001dxp commented Feb 19, 2019

@crisbeto, This seems to be sitting in limbo--when can we expect to see this released.
Currently, I set my own transform-translate and must add the values from the prepended translate to get an end value to save to the server. This works fine.

However, after setting my own translate to reposition the element after getting the top and left info from the server, the element skips around when I do initial drags and will only work correctly after refreshing the browser (the same behavior that @anandsunku is experiencing).

crisbeto added a commit to crisbeto/material2 that referenced this issue Feb 20, 2019
…draggable

Adds an API that allows the consumer to get the current position of a standalone draggable and to set it. This is useful for cases where the dragged position should be preserved when the user navigates away and then restored when they return.

Fixes angular#14420.
Fixes angular#14674.
crisbeto added a commit to crisbeto/material2 that referenced this issue Feb 24, 2019
…draggable

Adds an API that allows the consumer to get the current position of a standalone draggable and to set it. This is useful for cases where the dragged position should be preserved when the user navigates away and then restored when they return.

Fixes angular#14420.
Fixes angular#14674.
crisbeto added a commit to crisbeto/material2 that referenced this issue Mar 7, 2019
…draggable

Adds an API that allows the consumer to get the current position of a standalone draggable and to set it. This is useful for cases where the dragged position should be preserved when the user navigates away and then restored when they return.

Fixes angular#14420.
Fixes angular#14674.
crisbeto added a commit to crisbeto/material2 that referenced this issue Mar 23, 2019
…draggable

Adds an API that allows the consumer to get the current position of a standalone draggable and to set it. This is useful for cases where the dragged position should be preserved when the user navigates away and then restored when they return.

Fixes angular#14420.
Fixes angular#14674.
@pradt
Copy link

pradt commented Mar 25, 2019

Running into the same issues as what others are experiencing, the api is much needed.

jelbourn pushed a commit that referenced this issue Mar 26, 2019
…draggable

Adds an API that allows the consumer to get the current position of a standalone draggable and to set it. This is useful for cases where the dragged position should be preserved when the user navigates away and then restored when they return.

Fixes #14420.
Fixes #14674.
crisbeto added a commit to crisbeto/material2 that referenced this issue Apr 1, 2019
…draggable

Adds an API that allows the consumer to get the current position of a standalone draggable and to set it. This is useful for cases where the dragged position should be preserved when the user navigates away and then restored when they return.

Fixes angular#14420.
Fixes angular#14674.
mmalerba pushed a commit that referenced this issue Apr 3, 2019
…draggable (#14696)

Adds an API that allows the consumer to get the current position of a standalone draggable and to set it. This is useful for cases where the dragged position should be preserved when the user navigates away and then restored when they return.

Fixes #14420.
Fixes #14674.
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 10, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants