|
1 | 1 | import { Component } from '@angular/core'; |
2 | | -import { ClickEvent } from 'devextreme/ui/button'; |
| 2 | +import { KeyValue } from '@angular/common'; |
| 3 | +import { Task, Priority, GridDataService } from 'src/app/services/grid-data.service'; |
| 4 | +import * as AspNetData from 'devextreme-aspnet-data-nojquery'; |
| 5 | +import DxDataGrid, { |
| 6 | + RowDraggingStartEvent, RowDraggingAddEvent, Column, Row, |
| 7 | +} from 'devextreme/ui/data_grid'; |
| 8 | +import CustomStore from 'devextreme/data/custom_store'; |
| 9 | +import { DataSourceOptions } from 'devextreme/data/data_source'; |
| 10 | +import notify from 'devextreme/ui/notify'; |
3 | 11 |
|
4 | 12 | @Component({ |
5 | 13 | selector: 'app-root', |
6 | 14 | templateUrl: './app.component.html', |
7 | 15 | styleUrls: ['./app.component.scss'], |
8 | 16 | }) |
9 | 17 | export class AppComponent { |
10 | | - title = 'Angular'; |
| 18 | + statuses: number[]; |
11 | 19 |
|
12 | | - counter = 0; |
| 20 | + priorities: Priority[]; |
13 | 21 |
|
14 | | - buttonText = 'Click count: 0'; |
| 22 | + tasksStore: CustomStore; |
15 | 23 |
|
16 | | - onClick(e: ClickEvent): void { |
17 | | - this.counter++; |
18 | | - this.buttonText = `Click count: ${this.counter}`; |
| 24 | + dataSource: DataSourceOptions; |
| 25 | + |
| 26 | + shouldClearSelection = false; |
| 27 | + |
| 28 | + updateInProgress: Boolean = false; |
| 29 | + |
| 30 | + constructor(service: GridDataService) { |
| 31 | + const url = 'https://js.devexpress.com/Demos/Mvc/api/DnDBetweenGrids'; |
| 32 | + |
| 33 | + this.priorities = service.getPriorities(); |
| 34 | + this.statuses = [1, 2]; |
| 35 | + this.tasksStore = AspNetData.createStore({ |
| 36 | + key: 'ID', |
| 37 | + loadUrl: `${url}/Tasks`, |
| 38 | + updateUrl: `${url}/UpdateTask`, |
| 39 | + onBeforeSend(method, ajaxOptions) { |
| 40 | + ajaxOptions.xhrFields = { withCredentials: true }; |
| 41 | + }, |
| 42 | + }); |
| 43 | + this.dataSource = { |
| 44 | + store: this.tasksStore, |
| 45 | + reshapeOnPush: true, |
| 46 | + }; |
| 47 | + |
| 48 | + this.onDragStart = this.onDragStart.bind(this); |
| 49 | + this.onAdd = this.onAdd.bind(this); |
| 50 | + this.canDrag = this.canDrag.bind(this); |
| 51 | + } |
| 52 | + |
| 53 | + onDragStart(e: any): void { |
| 54 | + const selectedData: Task[] = e.component.getSelectedRowsData(); |
| 55 | + e.itemData = this.getVisibleRowValues(selectedData, e.component); |
| 56 | + e.cancel = !this.canDrag(e); |
| 57 | + } |
| 58 | + |
| 59 | + async onAdd(e: any): Promise<void> { |
| 60 | + const fromGrid = e.fromComponent as DxDataGrid; |
| 61 | + const toGrid = e.toComponent as DxDataGrid; |
| 62 | + const selectedRowKeys: (keyof Task)[] = fromGrid.getSelectedRowKeys(); |
| 63 | + const updateProcess: Promise<any>[] = []; |
| 64 | + const changes: any[] = []; |
| 65 | + |
| 66 | + this.updateInProgress = true; |
| 67 | + fromGrid.beginCustomLoading('Loading...'); |
| 68 | + toGrid.beginCustomLoading('Loading...'); |
| 69 | + |
| 70 | + for (let key of selectedRowKeys) { |
| 71 | + const values: Task = { Status: e.toData }; |
| 72 | + |
| 73 | + updateProcess.push(this.tasksStore.update(key, values)); |
| 74 | + changes.push({ |
| 75 | + type: 'update', |
| 76 | + key, |
| 77 | + data: values, |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + try { |
| 82 | + await Promise.all(updateProcess); |
| 83 | + |
| 84 | + this.tasksStore.push(changes); |
| 85 | + fromGrid.endCustomLoading(); |
| 86 | + toGrid.endCustomLoading(); |
| 87 | + this.updateInProgress = false; |
| 88 | + |
| 89 | + fromGrid.clearSelection(); |
| 90 | + if (!this.shouldClearSelection) { |
| 91 | + await toGrid.selectRows(selectedRowKeys, true); |
| 92 | + } |
| 93 | + } catch { |
| 94 | + notify('An error occurred while saving changes.', 'error', 3000); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + getVisibleRowValues(rowsData: Task[], grid: DxDataGrid): Record<string, Task[keyof Task] | string | undefined>[] { |
| 99 | + const visibleColumns = grid.getVisibleColumns(); |
| 100 | + const selectedData = rowsData.map((rowData: Task) => { |
| 101 | + const visibleValues: Record<string, Task[keyof Task] | string | undefined> = {}; |
| 102 | + visibleColumns.forEach((column: Column) => { |
| 103 | + if (column.dataField) { |
| 104 | + visibleValues[column.dataField] = this.getVisibleCellValue(column, rowData); |
| 105 | + } |
| 106 | + }); |
| 107 | + return visibleValues; |
| 108 | + }); |
| 109 | + return selectedData; |
| 110 | + } |
| 111 | + |
| 112 | + getVisibleCellValue(column: Column, rowData: Task): Task[keyof Task] | string | undefined { |
| 113 | + if (column.dataField) { |
| 114 | + const propKey = column.dataField as keyof Task; |
| 115 | + const cellValue = rowData[propKey]; |
| 116 | + |
| 117 | + return column.lookup?.calculateCellValue |
| 118 | + ? String(column.lookup.calculateCellValue(cellValue)) |
| 119 | + : cellValue; |
| 120 | + } |
| 121 | + return undefined; |
| 122 | + } |
| 123 | + |
| 124 | + canDrag(e: RowDraggingStartEvent): Row | Boolean { |
| 125 | + if (this.updateInProgress) return false; |
| 126 | + const visibleRows = e.component.getVisibleRows(); |
| 127 | + return visibleRows.some((r: Row) => r.isSelected && r.rowIndex === e.fromIndex); |
| 128 | + } |
| 129 | + |
| 130 | + originalOrder(a: KeyValue<number, string>, b: KeyValue<number, string>): number { |
| 131 | + return 0; |
19 | 132 | } |
20 | 133 | } |
0 commit comments