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

fix(sortable): sortable should be refreshed when a new item is added to the array #1716

Open
Vrael opened this issue Mar 6, 2017 · 13 comments

Comments

@Vrael
Copy link

Vrael commented Mar 6, 2017

Sortable component only shows initial array elements. When a new value is pushed into array, the sortable does not display it.

import { Component } from '@angular/core';

@Component({
  selector: 'custom-item-template-demo',
  templateUrl: './custom-item-template.html'
})
export class CustomItemTemplateDemoComponent {
  public itemStringsLeft: any[] = [
    'Windstorm',
    'Bombasto',
    'Magneta',
    'Tornado'
  ];

  public addItem() {
    this.itemStringsLeft.push("new item");
  }
}
<button type="button" (click)="addItem()">Add</button> 
<template #itemTemplate let-item="item" let-index="index"><span>{{index}}: {{item.value}}</span></template>
  {{itemStringsLeft.length}}
  <pre>{{ itemStringsLeft | json }}</pre>
  <bs-sortable
    [(ngModel)]="itemStringsLeft"
    [itemTemplate]="itemTemplate"
    itemClass="sortable-item"
    itemActiveClass="sortable-item-active"
    placeholderItem="Drag here"
    placeholderClass="placeholderStyle"
    wrapperClass="sortable-wrapper"
  ></bs-sortable>
@Vrael
Copy link
Author

Vrael commented Mar 7, 2017

Workaround: Call manually to writeValue of the SortableComponent

import { Component, ViewChild } from '@angular/core';

@Component({
  selector: 'custom-item-template-demo',
  templateUrl: './custom-item-template.html'
})
export class CustomItemTemplateDemoComponent {
  public itemStringsLeft: any[] = [
    'Windstorm',
    'Bombasto',
    'Magneta',
    'Tornado'
  ];

  @ViewChild(SortableComponent) sortableComponent: SortableComponent;
  public addItem() {
    this.itemStringsLeft.push("new item");
    this.sortableComponent.writeValue(this.itemStringsLeft);
  }
}

@dumpingrom
Copy link

Thanks @Vrael for this great workaround! I'm wondering if/when this is going to be fixed so the model is properly bound to the bs-sortable though @valorkin :)

@redraushan
Copy link

@Vrael I am also having the same issue. Is it going to be fixed or we will have to stick with the workaround? Thanks!

@valorkin
Copy link
Member

Sortable is an experimental component, fully reworked version of it is on the way

@ghost
Copy link

ghost commented Sep 7, 2017

Using this too. Workaround ok. Thx again for the AWESOME work Valor.

@joingenor
Copy link

When I try the workaround @ViewChild(SortableComponent) sortableComponent: SortableComponent;
I get a compiler error: Cannot find name 'SortableComponent'.

@Varshamathukumalli
Copy link

@valorkin when is this fully reworked version planned for release?

@valorkin
Copy link
Member

valorkin commented Nov 1, 2017

This year :)

@Varshamathukumalli
Copy link

Thanks for the respone. It would be great if this is released this month hopefully.

@royporter7
Copy link

Hi @valorkin
Something you may want to consider implementing in the next release is conditional binding to the [draggable] attribute.

Being able to disable specific elements at a specific index from being rearranged is something that I (and I assume many others) are going to need. :)

@valorkin
Copy link
Member

valorkin commented Nov 9, 2017

I am planning to decompose DND functionality to allow more flexibility

@YevheniiaMazur YevheniiaMazur changed the title Refresh sortable when a new item is added to the array fix(sortable): sortable should be refreshed when a new item is added to the array Jan 15, 2018
@mrfx
Copy link

mrfx commented Mar 1, 2018

When item is removed from the model's array then sortable should be refreshed too or provide method to refresh manually. BTW is there now any workaround?

@Darkeye9
Copy link

Another workaround, (and it's just more logical) is to re-create the array each time you want to modify it. Replacing the reference triggers the bs-sortable component refresh mechanism.

    selectChapter(chap: IChapter) {
        this.subtemplate.chapters = [...this.subtemplate.chapters, chap];
    }

    removeChapter(chap: IChapter) {
        this.subtemplate.chapters = this.subtemplate.chapters.filter(v => v.id !== chap.id);
    }

[...]

<bs-sortable
                [(ngModel)]="subtemplate.chapters"
                [itemTemplate]="itemTemplate"
                itemClass="sortable-item st-chapter-item"
                itemActiveClass="sortable-item-active"
                placeholderItem="Drag here"
                placeholderClass="placeholderStyle"
                wrapperClass="st-sortable-wrapper"
            ></bs-sortable>

@Domainv Domainv modified the milestones: 3.1.5, 3.1.4 Jan 4, 2019
@valorkin valorkin modified the milestones: 3.2.0, 3.2.1 Feb 4, 2019
@Domainv Domainv removed this from the 3.2.1 milestone Feb 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests