【问题标题】:Fail to sort few different list using sorting library ng2-dnd无法使用排序库 ng2-dnd 对几个不同的列表进行排序
【发布时间】:2016-12-01 04:50:19
【问题描述】:

我使用了一些不错的 simple drag-and-drop library from github,并且我有一个视图,其中包含 3 个不同的按钮,每个按钮在同一个地方但不是在同一时间被点击时都会显示一个列表。

由于某种原因,只有第一个列表可以排序,但其他 2 个不能排序...我可以移动对象但它们不可放置,因此我无法更改顺序。

这是我的html:

<div>
  <!-- list 1 button -->
  <button md-button
          (click)="showFirstList()"
          class="md-primary">List 1
  </button>

  <!-- list 2 button -->
  <button md-button
          (click)="showSecondList()"
          class="md-primary">List 2

  </button>

  <!-- list 3 button -->
  <button md-button
          (click)="ThirdList()"
          class="md-primary">List 3

  </button>
</div>


        <md-content>

          <div *ngIf="showingListOne">
            <div dnd-sortable-container [dropZones]="['zone-one']" [sortableData]="listOneToDisplay | async">
              <div class="list-bg" *ngFor="#item of listOneToDisplay | async; #i = index" dnd-sortable [sortableIndex]="i">
                ID: {{item.id}} <p></p> name: {{item.name}}
              </div>
            </div><br><br>
          </div>


          <div *ngIf="showingListTwo">
            <div dnd-sortable-container [dropZones]="['zone-two']" [sortableData]="listTwoToDisplay | async">
              <div class="list-bg" *ngFor="#item of listTwoToDisplay | async; #i = index" dnd-sortable [sortableIndex]="i">
                ID: {{item.id}} <p></p> age: {{item.age}}
              </div>
            </div>
          </div>


          <div *ngIf="showingListThree">
            <div dnd-sortable-container [dropZones]="['zone-three']"  [sortableData]="listThreeToDisplay | async">
              <div class="list-bg" *ngFor="#item of listThreeToDisplay | async; #i = index" dnd-sortable [sortableIndex]="i">
                ID: {{item.id}} <p></p> age: {{item.age}}
              </div>
            </div>
    </div>
          </div>

        </md-content>

这是我的排序列表.component.ts:

@Component({
  selector: 'sorting-lists',
  styles: [require('./sorting-lists.css')],
  directives: [DND_DIRECTIVES],
  providers: [DND_PROVIDERS],
  template: require('./sorting-lists.component.html')
})

@Injectable()
export class MyCmp implements OnInit {

  listOneToDisplay  = this._myService.getFirstListData();
  listTwoToDisplay = this._myService.getSecondListData();
  listThreeToDisplay = this._myService.getThirdListData();

  showingListOne = false;
  showingListTwo = false;
  showingListThree = false;

  constructor(private _myService: MyService) {
  };


  public showFirstList(): void {
    this.showingListOne = true;
    this.showingListTwo = false;
    this.showingListThree = false;
  }

  public showSecondList(): void {
    this.showingListTwo = true;
    this.showingListOne = false;
    this.showingListThree = false;
  }

  public showThirdList(): void {
    this.showingListThree = true;
    this.showingListTwo = false;
    this.showingListOne = false;
  }

}

如果有人能帮我弄清楚为什么只有第一个是可排序的,那将是幸运的!

谢谢:)

【问题讨论】:

  • 把这段代码放到plunkr中,方便调试

标签: javascript html typescript angular angular2-routing


【解决方案1】:

EDIT2:(根据您的新版本问题):

尝试在应用程序的引导程序中提供 DND_PROVIDERS,而不是在组件中提供它。更多详情请查看here

bootstrap(AppComponent, [
    DND_PROVIDERS // It is required to have 1 unique instance of your service
]);



您也可以使用ng2-dragula,它是dragula 的官方Angular2 包装器。

通过 npm 安装

编辑:要将 ng2-dragular 与 angular 2.0.0-beta.15 一起使用,请在最后一次提交之前添加带有 github 链接的 ng2-dragula 包在package.json 中切换到 RC.1:

ng2-dragula": "git://github.com/valor-software/ng2-dragula.git#0cdcd52b1a486609ed4b4a43465b1dac2bb1b007

> npm install

用法

将 DragulaService 添加到您的组件中并将 Dragula 添加到您的组件指令中。

@Component({
  selector: 'sample',
  directives: [Dragula],
  viewProviders: [DragulaService],
  template:`
  <div>
    <div class='wrapper'>
      <div class='container' [dragula]='"first-bag"'>
        <div>You can move these elements between these two containers</div>
        <div>Moving them anywhere else isn't quite possible</div>
        <div>There's also the possibility of moving elements around in the same container, changing their position</div>
      </div>
      <div class='container' [dragula]='"first-bag"'>
        <div>This is the default use case. You only need to specify the containers you want to use</div>
        <div>More interactive use cases lie ahead</div>
        <div>Make sure to check out the <a href='https://github.com/bevacqua/dragula#readme'>documentation on GitHub!</a></div>
      </div>
    </div>
  </div>
  `
})
class Sample {}

【讨论】:

  • 我不是在寻找 angular 2 库,而是特别是 angular 2.0.0-beta.15,他们不支持它
  • 您可以在 beta.15 版本中使用 ng2-draguar。看看我更新的答案。
  • iv 将其添加到我的依赖项中并 npm 安装了它,但是当我添加 Dragula 和 DragulaService 时,它​​找不到从哪里导入它
【解决方案2】:

我使用 ng2-dragula (https://github.com/valor-software/ng2-dragula) 或 primeng (http://www.primefaces.org/primeng)。

如果我理解您的示例,ng2-dragula 应该可以完成这项工作。

不确定这些是否适用于您需要使用的 Angular2 版本。

希望对你有帮助

【讨论】:

    【解决方案3】:

    您可以使用ng2-dnd

    ng2-dnd

    我曾在 angular2 beta 中使用过它,它使用起来非常简单灵活。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2012-11-07
      • 2020-06-18
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 2013-10-16
      • 1970-01-01
      • 1970-01-01
      • 2017-07-25
      相关资源
      最近更新 更多