-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoo-datatable.html
225 lines (191 loc) · 6.03 KB
/
oo-datatable.html
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<link rel="import" href="../font-roboto/roboto.html">
<link rel="import" href="../iron-flex-layout/iron-flex-layout.html">
<link rel="import" href="../paper-styles/paper-styles.html">
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../vaadin-grid/vaadin-grid.html">
<link rel="import" href="../vaadin-grid/vaadin-grid-column.html">
<link rel="import" href="../vaadin-grid/vaadin-grid-sorter.html">
<link rel="import" href="../vaadin-material-theme/vaadin-grid.html">
<dom-module id="oo-datatable">
<template>
<style>
:host {
display: block;
@apply --paper-font-common-base;
@apply --layout-vertical;
}
#container {
@apply --shadow-elevation-2dp;
@apply --layout-vertical;
@apply --layout-flex;
}
#content {
@apply --layout-vertical;
@apply --layout-flex;
}
#grid {
@apply --layout-flex;
}
</style>
<div id="container">
<slot name="header"></slot>
<div id="content">
<vaadin-grid
id="grid"
multi-sort>
<vaadin-grid-column width="60px" flex-grow="0">
<template class="header">#</template>
<template>[[_calculateItemIndex(index, pageNumber)]]</template>
</vaadin-grid-column>
<vaadin-grid-column>
<template class="header">
<vaadin-grid-sorter path="firstName">
First Name
</vaadin-grid-sorter>
</template>
<template>[[item.firstName]]</template>
</vaadin-grid-column>
<vaadin-grid-column>
<template class="header">
<vaadin-grid-sorter path="lastName">
Last Name
</vaadin-grid-sorter>
</template>
<template>[[item.lastName]]</template>
</vaadin-grid-column>
<vaadin-grid-column width="45%">
<template class="header">
<vaadin-grid-sorter path="address">
Address
</vaadin-grid-sorter>
</template>
<template>
<p style="white-space: normal">[[item.address.street]], [[item.address.city]]</p>
</template>
</vaadin-grid-column>
</vaadin-grid>
</div>
<slot name="footer"></slot>
</div>
</template>
<script>
(function () {
class OoDatatable extends Polymer.Element {
static get is() {
return "oo-datatable"
}
static get properties() {
return {
items: {
type: Array,
value: () => [],
readOnly: true
},
pageNumber: {
type: Number,
value: () => 1
},
numberOfPages: {
type: Number,
value: () => 1
},
}
}
get service() {
const event = new CustomEvent("oo-datatable__inject-service", {
detail: {
service: null
},
bubbles: true,
composed: true
})
this.dispatchEvent(event)
return requireNotNil(event.detail.service)
}
ready() {
super.ready()
this._log("ready")
this._log(this.$.grid.pageSize)
}
_calculateItemIndex(index, pageNumber) {
// pageNumber is 1-based, index is 0-based.
return ((pageNumber - 1) * 10) + index + 1
}
refreshItems(items) {
this._log("refresh items:", items)
const grid = this.$.grid
grid.set("items", items)
this._setItems(items)
}
refreshPaginatedItems(page, pageSize) {
this._log("refresh paginated items:", page, pageSize)
// by default dataProvider is using an arrayDataProvider.
const grid = this.$.grid
if (!grid.dataProvider) {
// data provider is called right after we set it.
this._log("initialize custom data provider")
grid.set("dataProvider", this._createCustomDataProvider())
}
// update grid items only when grid.clearCache() is calledis called.
grid.set("size", pageSize)
this.pageNumber = page || 1
grid.__data.pageSize = pageSize
grid._scrollToIndex(0)
grid.clearCache()
}
_createCustomDataProvider() {
let index = 0
return (params, callback) => {
const callIndex = index++
const requestParams = {
page: this.pageNumber - 1,
pageSize: params.pageSize,
sortOrders: params.sortOrders
}
this._log(`(${callIndex}) request items:`, requestParams)
this._requestItems(requestParams)
.then(items => {
this._log(`(${callIndex}) response items:`, items)
callback(items, items.length)
this._setItems(items)
})
}
}
async _requestItems(requestParams) {
return this.service.queryItems(
requestParams.page, requestParams.pageSize, requestParams.sortOrders)
}
_log(...messages) {
console.log(`[${this.localName}]`, ...messages)
}
}
function requireNotNil(value) {
if (value === null || value === undefined) {
throw new Error("illegal argument: value is nil")
}
return value
}
window.customElements.define(OoDatatable.is, OoDatatable)
}())
</script>
</dom-module>
<dom-module id="oo-datatable__vaadin-grid" theme-for="vaadin-grid">
<template>
<style>
:host {
font-size: 15px;
border: none;
min-height: 0;
}
[part~="cell"] {
font-size: 15px;
padding: 0px 12px;
}
[part~="header-cell"] ::slotted(*),
[part~="footer-cell"] ::slotted(*) {
font-size: 15px;
font-weight: normal;
}
</style>
</template>
</dom-module>