2019-06-16 15:38:18 +00:00
|
|
|
import { Component, OnInit, ViewChild } from '@angular/core';
|
|
|
|
import { TableListDataSource } from './table-list-datasource';
|
|
|
|
import { Table } from '../../core/table';
|
|
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
|
|
|
import { MatTable } from "@angular/material";
|
|
|
|
import { ProductGroup } from "../../core/product-group";
|
|
|
|
import { BehaviorSubject } from "rxjs";
|
|
|
|
import { CdkDragDrop, moveItemInArray } from "@angular/cdk/drag-drop";
|
|
|
|
import { ToasterService } from "../../core/toaster.service";
|
|
|
|
import { TableService } from "../table.service";
|
2019-06-13 19:02:34 +00:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-table-list',
|
|
|
|
templateUrl: './table-list.component.html',
|
|
|
|
styleUrls: ['./table-list.component.css']
|
|
|
|
})
|
2019-06-14 03:58:30 +00:00
|
|
|
export class TableListComponent implements OnInit {
|
2019-06-16 15:38:18 +00:00
|
|
|
@ViewChild('table', { static: true }) table: MatTable<ProductGroup>;
|
2019-06-14 03:58:30 +00:00
|
|
|
dataSource: TableListDataSource;
|
|
|
|
list: Table[];
|
2019-06-16 15:38:18 +00:00
|
|
|
data: BehaviorSubject<Table[]>;
|
2019-06-14 03:58:30 +00:00
|
|
|
/** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */
|
2019-06-16 15:38:18 +00:00
|
|
|
displayedColumns = ['name', 'location', 'isActive'];
|
2019-06-13 19:02:34 +00:00
|
|
|
|
2019-06-16 15:38:18 +00:00
|
|
|
constructor(
|
|
|
|
private route: ActivatedRoute,
|
|
|
|
private router: Router,
|
|
|
|
private toaster: ToasterService,
|
|
|
|
private ser: TableService
|
|
|
|
) {
|
|
|
|
this.data = new BehaviorSubject([]);
|
|
|
|
this.data.subscribe((data: Table[]) => {
|
|
|
|
this.list = data;
|
|
|
|
})
|
2019-06-14 03:58:30 +00:00
|
|
|
}
|
2019-06-13 19:02:34 +00:00
|
|
|
|
2019-06-14 03:58:30 +00:00
|
|
|
ngOnInit() {
|
|
|
|
this.route.data
|
|
|
|
.subscribe((data: { list: Table[] }) => {
|
2019-06-16 15:38:18 +00:00
|
|
|
this.data.next(data.list);
|
2019-06-14 03:58:30 +00:00
|
|
|
});
|
2019-06-16 15:38:18 +00:00
|
|
|
this.dataSource = new TableListDataSource(this.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateSortOrder() {
|
|
|
|
this.ser.updateSortOrder(this.list)
|
|
|
|
.subscribe(
|
|
|
|
(result) => {
|
|
|
|
this.toaster.show('Success', '');
|
|
|
|
},
|
|
|
|
(error) => {
|
|
|
|
this.toaster.show('Danger', error.error);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
dropTable(event: CdkDragDrop<ProductGroup[]>) {
|
|
|
|
const prevIndex = this.list.indexOf(event.item.data);
|
|
|
|
moveItemInArray(this.list, prevIndex, event.currentIndex);
|
|
|
|
this.data.next(this.list);
|
2019-06-14 03:58:30 +00:00
|
|
|
}
|
2019-06-13 19:02:34 +00:00
|
|
|
}
|