Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Row reordering in Angular material mat-table using drag events

I am developing a website where I am using Data Table component of Angular Material. I want the user to be able to set some kind of priority to each row by dragging rows up and down. Something similar to Data Table for jQuery. I cannot find support for reordering using drag events in their documentation. How can I achieve this functionality while adding least dependencies to my project?

like image 201
Vikrant Yadav Avatar asked Jan 28 '18 05:01

Vikrant Yadav


1 Answers

For those looking for an answer on how to use this with Angular Material tables (mat-table):

Instead of using <table mat-table ...> you will have to use the <mat-table ..> selector. The former will have a tbody element between the table element (where you applied your dragula bag) and the rows. Attempting to drag the rows will instead make dragula pick up the tbody element. For <mat table ...> however, the rows will be immediate child elements of the container.

You can then use dragula like so:

<!-- my-table.component.html -->
<mat-table 
    [dragula]='"my-bag"' 
    [dataSource]="myTableDataSource">
    <!-- your column and row defs go here -->
</mat-table>

In your component

// my-table.component.ts
constructor(private dragulaService: DragulaService) {
  dragulaService.drop.subscribe((value) => {
    this.onDrop(value);
  });
}

private onDrop(args) {
    console.log(args);
}
like image 123
Jan Wendland Avatar answered Oct 24 '22 17:10

Jan Wendland