Skip to content

Commit 4218d71

Browse files
committed
avoid mixed dragging between rows and columns
1 parent cdc7268 commit 4218d71

File tree

2 files changed

+19
-29
lines changed

2 files changed

+19
-29
lines changed

src/components/compounds/Table/DataGrid.ts

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import { type } from "os";
2-
import { threadId } from "worker_threads";
3-
41
class DataGrid {
52
columns: any[];
63
rows: any[];
@@ -80,7 +77,6 @@ class DataGrid {
8077
: this.checkedRows.filter(current_row => current_row.id !== row.id)
8178
}
8279

83-
// pagination
8480
switchPage() {
8581
this.rows = this.originalRows.slice(this.currentPage*this.rowsPerPage, this.currentPage*this.rowsPerPage + this.rowsPerPage)
8682
}
@@ -89,34 +85,25 @@ class DataGrid {
8985
this.columns = this.columns.map(column => column.name === name ? ({ ...column, show: !column.show }) : column)
9086
}
9187

92-
// construct new columns object with only the subobjects that have a key show as true
88+
// returns visible columns
9389
getColumns() {
9490
return this.columns.filter(column => column.show)
9591
}
9692

97-
/**
98-
* Row dragging methods
99-
*/
10093
onDragStartRow(event, row, idx) {
10194
this.draggingRow = row;
10295
this.draggingRowIdx = idx;
10396
}
10497

105-
onDragEndRow(event, row, idx) {
106-
// do nothing
107-
}
108-
109-
// callback called when user drops a row
11098
onDropRow(event, row, idx) {
11199
const chunk = this.rows.splice(this.draggingRowIdx, 1)
112100
this.rows.splice(idx, 0, chunk[0])
113-
114-
// remove selected from all rows
115-
this.rows.forEach(row => row.selected = false)
101+
this.resetDraggingRow()
116102
}
117103

118-
// the event must be prevented for the onDrop method to get called
119104
onDragOverRow(event, row, idx) {
105+
if (this.draggingRowIdx === null) return;
106+
120107
this.rows[idx].selected = true
121108
event.preventDefault()
122109
}
@@ -125,29 +112,28 @@ class DataGrid {
125112
this.rows[idx].selected = false
126113
}
127114

128-
/**
129-
* Column dragging methods
130-
*/
115+
resetDraggingRow() {
116+
this.rows.forEach(row => row.selected = false)
117+
this.draggingRow = null;
118+
this.draggingRowIdx = null;
119+
}
120+
131121
onDragStartColumn(event, column, idx) {
132122
this.draggingColumn = column;
133123
this.draggingColumnIdx = idx;
134124
}
135125

136-
onDragEndColumn(event, row, idx) {
137-
// do nothing
138-
}
139-
140126
// callback called when user drops a column
141127
onDropColumn(event, column, idx) {
142128
const chunk = this.columns.splice(this.draggingColumnIdx, 1)
143129
this.columns.splice(idx, 0, chunk[0])
144-
145-
// remove selected from all columns
146-
this.columns.forEach(column => column.selected = false)
130+
this.resetDraggingColumn()
147131
}
148132

149133
// the event must be prevented for the onDrop method to get called
150134
onDragOverColumn(event, column, idx) {
135+
if (this.draggingColumnIdx === null) return;
136+
151137
this.columns[idx].selected = true
152138
event.preventDefault()
153139
}
@@ -156,6 +142,12 @@ class DataGrid {
156142
this.columns[idx].selected = false
157143
}
158144

145+
resetDraggingColumn() {
146+
this.columns.forEach(column => column.selected = false)
147+
this.draggingColumn = null;
148+
this.draggingColumnIdx = null;
149+
}
150+
159151
// returns an object that maps column names to column instances
160152
getColumnsObject() {
161153
return this.columns.reduce((obj, column) => {

src/components/compounds/Table/Table.vue

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ export default Table
154154
@click="sortable ? sortColumn(column) : null"
155155
:draggable="draggableColumns"
156156
@dragstart="data.onDragStartColumn($event, row, idx)"
157-
@dragend="data.onDragEndColumn($event, column, idx)"
158157
@drop="data.onDropColumn($event, column, idx)"
159158
@dragover="data.onDragOverColumn($event, column, idx)"
160159
@dragleave="data.onDragLeaveColumn($event, column, idx)"
@@ -185,7 +184,6 @@ export default Table
185184
<tr
186185
:draggable="draggableRows"
187186
@dragstart="data.onDragStartRow($event, row, idx)"
188-
@dragend="data.onDragEndRow($event, row, idx)"
189187
@drop="data.onDropRow($event, row, idx)"
190188
@dragover="data.onDragOverRow($event, row, idx)"
191189
@dragleave="data.onDragLeaveRow($event, row, idx)"

0 commit comments

Comments
 (0)