Skip to content

Commit bb7c58b

Browse files
committed
change columns to array in datagrid.
add draggable column.
1 parent d98e529 commit bb7c58b

File tree

2 files changed

+73
-52
lines changed

2 files changed

+73
-52
lines changed

src/components/compounds/Table/DataGrid.ts

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,39 @@ import { type } from "os";
22
import { threadId } from "worker_threads";
33

44
class DataGrid {
5-
columns: object;
5+
columns: any[];
66
rows: any[];
77
originalRows: object[];
88
checkedRows: {id: any}[];
99
rowsPerPage: number;
1010
currentPage: number;
1111
draggingRow: {id: any};
1212
draggingRowIdx: number;
13+
draggingColumn: {id: any};
14+
draggingColumnIdx: number;
1315

1416
constructor() {
15-
this.columns = {};
17+
this.columns = [];
1618
this.rows = [];
1719
this.originalRows = [];
1820
this.checkedRows = [];
1921
this.rowsPerPage = 0;
2022
this.currentPage = 0;
2123
this.draggingRow = null;
2224
this.draggingRowIdx = null;
25+
this.draggingColumn = null;
26+
this.draggingColumnIdx = null;
2327
}
2428

2529
addColumn(name: string, caption: string, dataType: string, style: string) {
26-
this.columns[name] = {
30+
this.columns.push({
2731
name,
2832
caption,
2933
dataType,
3034
style,
31-
show: true
32-
}
35+
show: true,
36+
ascendant: true
37+
})
3338
}
3439

3540
addRow(content: object, index: number) {
@@ -80,41 +85,29 @@ class DataGrid {
8085
this.rows = this.originalRows.slice(this.currentPage*this.rowsPerPage, this.currentPage*this.rowsPerPage + this.rowsPerPage)
8186
}
8287

83-
// hide / show columns
84-
showColumn(name: string) {
85-
this.columns[name].show = true
86-
}
87-
88-
hideColumn(name: string) {
89-
this.columns[name].show= false
90-
}
91-
92-
toggleColumn(column) {
93-
column.show ? this.hideColumn(column.name) : this.showColumn(column.name)
88+
toggleColumn({ name } : { name: string }) {
89+
this.columns = this.columns.map(column => column.name === name ? ({ ...column, show: !column.show }) : column)
9490
}
9591

9692
// construct new columns object with only the subobjects that have a key show as true
9793
getColumns() {
98-
return Object.keys(this.columns).filter(column => this.columns[column].show).reduce((newColumns, key) => {
99-
newColumns[key] = this.columns[key]
100-
return newColumns
101-
}, {})
94+
return this.columns.filter(column => column.show)
10295
}
10396

10497
/**
10598
* Row dragging methods
10699
*/
107-
onDragStart(event, row, idx) {
100+
onDragStartRow(event, row, idx) {
108101
this.draggingRow = row;
109102
this.draggingRowIdx = idx;
110103
}
111104

112-
onDragEnd(event, row, idx) {
105+
onDragEndRow(event, row, idx) {
113106
// do nothing
114107
}
115108

116109
// callback called when user drops a row
117-
onDrop(event, row, idx) {
110+
onDropRow(event, row, idx) {
118111
const chunk = this.rows.splice(this.draggingRowIdx, 1)
119112
this.rows.splice(idx, 0, chunk[0])
120113

@@ -123,14 +116,45 @@ class DataGrid {
123116
}
124117

125118
// the event must be prevented for the onDrop method to get called
126-
onDragOver(event, row, idx) {
119+
onDragOverRow(event, row, idx) {
127120
this.rows[idx].selected = true
128121
event.preventDefault()
129122
}
130123

131-
onDragLeave(event, row, idx) {
124+
onDragLeaveRow(event, row, idx) {
132125
this.rows[idx].selected = false
133126
}
127+
128+
/**
129+
* Column dragging methods
130+
*/
131+
onDragStartColumn(event, column, idx) {
132+
this.draggingColumn = column;
133+
this.draggingColumnIdx = idx;
134+
}
135+
136+
onDragEndColumn(event, row, idx) {
137+
// do nothing
138+
}
139+
140+
// callback called when user drops a column
141+
onDropColumn(event, column, idx) {
142+
const chunk = this.columns.splice(this.draggingColumnIdx, 1)
143+
this.columns.splice(idx, 0, chunk[0])
144+
145+
// remove selected from all columns
146+
this.columns.forEach(column => column.selected = false)
147+
}
148+
149+
// the event must be prevented for the onDrop method to get called
150+
onDragOverColumn(event, column, idx) {
151+
this.columns[idx].selected = true
152+
event.preventDefault()
153+
}
154+
155+
onDragLeaveColumn(event, column, idx) {
156+
this.columns[idx].selected = false
157+
}
134158
}
135159

136160
export default DataGrid

src/components/compounds/Table/Table.vue

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,18 @@ const Table = {
3030
hasResetBtn: Boolean,
3131
sortable: Boolean,
3232
expandable: Boolean,
33-
draggable: Boolean,
33+
draggableRows: Boolean,
34+
draggableColumns: Boolean,
3435
editable: Boolean
3536
},
3637
3738
setup(props, { emit }) {
38-
// general state
39+
// datagrid instance reference
3940
const data = ref(props.data)
40-
const columnProperties = ref(props.data.columns)
41-
42-
// pagination state
4341
const pagination = ref(props.pagination)
4442
const rowsPerPage = ref(props.rowsPerPage)
4543
const currentPage = ref(0)
46-
47-
for (const colName in props.data.columns) {
48-
columnProperties.value[colName].ascendant = true
49-
}
50-
5144
const search = reactive({})
52-
5345
const expanded = ref(new Set())
5446
5547
// whenever pagination props changes, update the class state accordingly
@@ -74,9 +66,9 @@ const Table = {
7466
props.data.switchPage()
7567
}
7668
77-
const sortColumn = colName => {
78-
props.data.sortByColumn(colName, columnProperties.value[colName].ascendant)
79-
columnProperties.value[colName].ascendant = !columnProperties.value[colName].ascendant
69+
const sortColumn = column => {
70+
props.data.sortByColumn(column.name, column.ascendant)
71+
column.ascendant = !column.ascendant
8072
}
8173
8274
const toggleExpanded = rowId => {
@@ -101,7 +93,7 @@ const Table = {
10193
10294
const countColumns = computed(() => {
10395
return (
104-
Object.keys(data.value.getColumns()).length +
96+
data.value.getColumns().length +
10597
(props.checkable ? 1 : 0) +
10698
(props.expandable ? 1 : 0)
10799
)
@@ -119,7 +111,6 @@ const Table = {
119111
120112
return {
121113
props,
122-
columnProperties,
123114
data,
124115
search,
125116
sortColumn,
@@ -157,14 +148,20 @@ export default Table
157148
<td v-if="checkable" />
158149
<td v-if="expandable" />
159150
<th
160-
v-for="column in data.getColumns()"
161-
:key="column"
162-
:class="column.style"
163-
@click="sortable ? sortColumn(column.name) : null"
151+
v-for="(column, idx) in data.getColumns()"
152+
:key="idx"
153+
:class="{ ...column.style, 'has-text-primary': column.selected }"
154+
@click="sortable ? sortColumn(column) : null"
155+
:draggable="draggableColumns"
156+
@dragstart="data.onDragStartColumn($event, row, idx)"
157+
@dragend="data.onDragEndColumn($event, column, idx)"
158+
@drop="data.onDropColumn($event, column, idx)"
159+
@dragover="data.onDragOverColumn($event, column, idx)"
160+
@dragleave="data.onDragLeaveColumn($event, column, idx)"
164161
>
165162
{{ column.caption }}
166163
<span v-if="sortable">
167-
{{ columnProperties[column.name].ascendant ? '&darr;' : ' &uarr;' }}
164+
{{ column.ascendant ? '&darr;' : ' &uarr;' }}
168165
</span>
169166
</th>
170167
</tr>
@@ -186,12 +183,12 @@ export default Table
186183
</tr>
187184
<template v-for="(row, idx) in data.rows" :key="idx">
188185
<tr
189-
:draggable="draggable"
190-
@dragstart="data.onDragStart($event, row, idx)"
191-
@dragend="data.onDragEnd($event, row, idx)"
192-
@drop="data.onDrop($event, row, idx)"
193-
@dragover="data.onDragOver($event, row, idx)"
194-
@dragleave="data.onDragLeave($event, row, idx)"
186+
:draggable="draggableRows"
187+
@dragstart="data.onDragStartRow($event, row, idx)"
188+
@dragend="data.onDragEndRow($event, row, idx)"
189+
@drop="data.onDropRow($event, row, idx)"
190+
@dragover="data.onDragOverRow($event, row, idx)"
191+
@dragleave="data.onDragLeaveRow($event, row, idx)"
195192
:class="{ 'has-background-primary': row.selected, 'has-text-white': row.selected }"
196193
>
197194
<td v-if="checkable">

0 commit comments

Comments
 (0)