/
SampleCheetahGrid.vue
151 lines (148 loc) · 4.06 KB
/
SampleCheetahGrid.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<template>
<div class="sample-c-grid">
<div v-if="!requested" class="button-wrapper">
<button class="action-button" :disabled="requested" @click="onClick">
Load 1,000,000 records
</button>
<br />
<button :disabled="requested" @click="syncOnClick">
Load 1,000,000 records (sync)
</button>
</div>
<h4 class="status">
{{ message }}
<template v-if="initTimeMs || totalTimeMs">
[
<template v-if="initTimeMs">
init: {{ initTimeMs }}ms
</template>
<template v-if="totalTimeMs">
total: {{ totalTimeMs }}ms
</template>
]
</template>
</h4>
<div v-show="requested" ref="gridWrapper" class="grid-wrapper"></div>
</div>
</template>
<script>
import { streamJsonForVue } from "../stream-json";
import { cheetahGrid } from "vue-cheetah-grid";
export default {
name: "SampleCGrid",
components: {},
data() {
return {
hasData: false,
message: "",
initTimeMs: "",
totalTimeMs: "",
requested: false
};
},
methods: {
onClick() {
this.message = "request...";
this.requested = true;
this.$nextTick(() => this.startGrid());
},
createGrid(records) {
const grid = (this.grid = new cheetahGrid.ListGrid({
parentElement: this.$refs.gridWrapper,
header: [
{
field: "check",
caption: "",
width: 50,
columnType: "check",
action: "check"
},
{ field: "id", caption: "ID", width: 100 },
{
/* multiple header */
caption: "Name",
columns: [
{
field: "fname",
caption: "First Name",
width: "calc(30% - 90px)"
},
{
field: "lname",
caption: "Last Name",
width: "calc(30% - 90px)"
}
]
},
{ field: "email", caption: "Email", width: "calc(40% - 90px)" },
{
caption: "Button",
width: 120,
/* button column */
columnType: new cheetahGrid.columns.type.ButtonColumn({
caption: "SHOW"
}),
action: new cheetahGrid.columns.action.ButtonAction({
action(rec) {
alert(JSON.stringify(rec, null, 2));
}
})
}
],
frozenColCount: 2
}));
grid.records = records;
return grid;
},
startGrid() {
const records = [];
const grid = this.createGrid(records);
let buffer = [];
const startTime = Date.now();
streamJsonForVue(this, "/api/persons", {}, rec => {
buffer.push(rec);
if (
buffer.length >= 10000 ||
(records.length < 10000 && buffer.length >= 1000) ||
(records.length < 1000 && buffer.length >= 100)
) {
records.push(...buffer);
grid.records = records;
buffer = [];
this.message = "loading... (" + records.length + "records)";
this.totalTimeMs = Date.now() - startTime;
if (!this.initTimeMs) {
this.initTimeMs = this.totalTimeMs;
}
}
}).then(() => {
records.push(...buffer);
grid.records = records;
this.message = "";
this.totalTimeMs = Date.now() - startTime;
});
},
syncOnClick() {
this.message = "request...";
this.requested = true;
this.$nextTick(() => {
const records = [];
const grid = this.createGrid(records);
const startTime = Date.now();
fetch("/api/persons/sync")
.then(response => response.json())
.then(records => {
console.timeEnd("initial_time");
grid.records = records;
this.message = "";
this.totalTimeMs = Date.now() - startTime;
});
});
},
recordOnClick(rec) {
alert(JSON.stringify(rec, null, 2));
}
}
};
</script>
<style scoped></style>