-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate-124.html
372 lines (340 loc) · 17.5 KB
/
template-124.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<h1 id="general-concepts-and-row-expansion-apis">General Concepts and Row Expansion APIs</h1>
<p>Row expansion is an extra row that is extended directly from a normal row. It's a great place to show extra information which cannot normally be shown on a single row. </p>
<p>It looks just like a normal row but, unlike a normal row, it has no data from the main data table associated with it. This means that the states or data of the row expansion have to come from the expanded row. Grid will not perform sorting or filtering on the row expansion, as it has no data. </p>
<p>In effect, row expansion is completely different from row grouping, where rows are grouped based on their data. Row expansion is tightly connected to the expanded row and cannot be separated. When the expanded row is moved, the row expansion is also moved. So, be aware that operations like sorting, filtering, or pagination can still have an impact on how row expansion is displayed.</p>
<p>To add a row expansion, call <code>addRowExpansion(rowId)</code> or <code>toggleRowExpansion(rowId)</code> from Grid's data view. <code>rowId</code> is the row ID of the row to be expanded. </p>
<p>To remove all row expansions at once, use <code>removeAllRowExpansions()</code>. </p>
<p>To get existing rows with an expansion, use <code>getRowsWithExpansion()</code>.</p>
<p>To render content on the row expansion, define <code>renderer</code> property of <code>rowExpansion</code> on the grid configuration object, like so:</p>
<pre><code class="language-js">var configObj = {
//...
rowExpansionBinding: function (e) {
var rowIndex = e["rowIndex"];
var section = e["section"];
if(e.rowExpansion) {
// render
var rowData = e.originalRowData;
var cell = section.stretchCell(0, rowIndex);
var str = "expanded from row \"" + rowData["col1"] + ", " + rowData["col2"] + ", " + rowData["col3"] + "\"";
cell.setContent(str);
cell.setStyle("background-color", "lightblue");
} else {
//dispose
var cell = section.stretchCell(0, rowIndex, false);
cell.setStyle("background-color", "");
}
},
//...
};
</code></pre>
<blockquote>
<p>Note: the same cell element is used for both normal row and row expansion due to the virtualization technique. Any custom style has to be removed using the <code>dispose</code> part.</p>
</blockquote>
<p>The example below shows how to create row expansion when clicking on a normal row. It also shows how to save and load row expansion states.</p>
<code-sandbox hash="39c55359"><pre><code class="language-css">efx-grid {
height: 200px;
}
html hr {
margin: 5px;
}
</code></pre>
<pre><code class="language-html"><button id="save_states">Save</button>
<button id="load_states">Load</button>
<button id="clear_row_expan">Clear all row expansions</button>
<hr>
<efx-grid id="grid"></efx-grid>
<hr>
<pre id="msg"></pre>
</code></pre>
<pre><code class="language-javascript">import { halo } from './theme-loader.js'; // This line is only required for demo purpose. It is not relevant for your application.
await halo(); // This line is only required for demo purpose. It is not relevant for your application.
/* ---------------------------------- Note ----------------------------------
DataGenerator, Formatters and extensions are exposed to global scope
in the bundle file to make it easier to create live examples.
Importing formatters and extensions is still required in your application.
Please see the document for further information.
---------------------------------------------------------------------------*/
var fields = ["companyName", "market", "CF_LAST", "CF_NETCHNG", "industry"];
var records = DataGenerator.generateRecords(fields, { seed: 1, numRows: 10 });
function onCellCliked(e) {
var pos = grid.api.getRelativePosition(e);
if (pos.sectionType === "content") { // Prevent clicking on header section
var dv = grid.api.getDataView();
dv.toggleRowExpansion(pos.rowIndex);
}
}
document.getElementById("save_states").addEventListener("click", function () {
var dv = grid.api.getDataView();
var rowWithExpansions = dv.getRowsWithExpansion();
var rowIds = rowWithExpansions.filter(function (id) {
return id;
});
msg.textContent = "Row Ids saved:\n" + JSON.stringify(rowIds, null, 4);
sessionStorage.setItem("row_expansion_data", JSON.stringify(rowIds));
});
document.getElementById("load_states").addEventListener("click", function () {
// Initial row expansion from rowId
var dv = grid.api.getDataView();
var data = sessionStorage.getItem("row_expansion_data");
msg.textContent = "Data loaded: " + data;
const rowIds = JSON.parse(data);
dv.removeAllRowExpansions();
for (var rowId of rowIds) {
dv.addRowExpansion(rowId);
}
});
document.getElementById("clear_row_expan").addEventListener("click", function () {
var dv = grid.api.getDataView();
dv.removeAllRowExpansions();
msg.textContent = "Row expansions are cleared";
});
var configObj = {
sorting: {
sortableColumns: true
},
columns: [
{name: "Company", field: fields[0]},
{name: "Market", field: fields[1], width: 120},
{name: "Last", field: fields[2], width: 100},
{name: "Net. Chng", field: fields[3], width: 100},
{name: "Industry", field: fields[4]}
],
rowExpansionBinding: function(e) {
var rowIndex = e["rowIndex"];
var section = e["section"];
var colCount = section.getColumnCount();
if (e.rowExpansion) {
for (var c = 0; c < colCount; ++c) {
var cell = section.getCell(c, rowIndex);
cell.setContent("Row Expansion_" + c);
cell.setStyle("backgroundColor", "#cc7755");
}
} else {
for (var c = 0; c < colCount; ++c) {
var cell = section.getCell(c, rowIndex);
cell.setStyle("backgroundColor", "");
}
}
},
staticDataRows: records
};
var grid = document.getElementById("grid");
grid.config = configObj;
grid.addEventListener("click", onCellCliked);
</code></pre>
</code-sandbox><h2 id="managing-states-for-row-expansion">Managing states for row expansion</h2>
<p>Since row expansion has no row data to hold its state, the data has to be stored on the expanded row. The example below shows how you can have two different types of content based on the button being clicked.</p>
<code-sandbox hash="e88ab0db"><pre><code class="language-css">efx-grid {
height: 200px;
}
html hr {
margin: 5px;
}
</code></pre>
<pre><code class="language-html"><efx-grid id="grid"></efx-grid>
</code></pre>
<pre><code class="language-javascript">import { halo } from './theme-loader.js'; // This line is only required for demo purpose. It is not relevant for your application.
await halo(); // This line is only required for demo purpose. It is not relevant for your application.
/* ---------------------------------- Note ----------------------------------
DataGenerator, Formatters and extensions are exposed to global scope
in the bundle file to make it easier to create live examples.
Importing formatters and extensions is still required in your application.
Please see the document for further information.
---------------------------------------------------------------------------*/
var fields = ["companyName", "market", "CF_LAST", "CF_NETCHNG", "industry"];
var records = DataGenerator.generateRecords(fields, { numRows: 10 });
var buttonFormatter = function (e) {
var cell = e["cell"];
var content = cell.getContent();
if (!content || !cell._buttons) {
var buttonsContainer = (cell._buttons = document.createElement("div"));
var firstButton = document.createElement("ef-button");
firstButton.icon = "edit";
firstButton.addEventListener("click", onFirstClickButton);
var secondButton = document.createElement("ef-button");
secondButton.icon = "filter";
secondButton.addEventListener("click", onSecondClickButton);
buttonsContainer.appendChild(firstButton);
buttonsContainer.appendChild(secondButton);
}
cell.setContent(cell._buttons);
};
var onFirstClickButton = function (e) {
var pos = grid.api.getRelativePosition(e);
var dv = grid.api.getDataView();
// Save the state to the expanded row
dv.setDataAt(pos.rowIndex, "My_Clicked_Button", 1);
dv.toggleRowExpansion(pos.rowIndex);
};
var onSecondClickButton = function (e) {
var pos = grid.api.getRelativePosition(e);
var dv = grid.api.getDataView();
// Save the state to the expanded row
dv.setDataAt(pos.rowIndex, "My_Clicked_Button", 2);
dv.toggleRowExpansion(pos.rowIndex);
};
var configObj = {
columns: [
{
name: "Buttons",
field: "btnClicked",
binding: buttonFormatter,
width: 80,
alignment: "center"
},
{name: "Company", field: fields[0]},
{name: "Market", field: fields[1], width: 120},
{name: "Last", field: fields[2], width: 100},
{name: "Net. Chng", field: fields[3], width: 100},
{name: "Industry", field: fields[4]}
],
rowExpansionBinding: function(e) {
var rowIndex = e["rowIndex"];
var section = e["section"];
if (e.rowExpansion) {
// render
var cell = section.stretchCell(0, rowIndex, true);
var rowData = e.originalRowData;
if (rowData["My_Clicked_Button"] === 1) {
cell.setContent("First button was clicked");
} else {
cell.setContent("Second button was clicked");
}
cell.setStyle("backgroundColor", "#cc7755");
cell.setStyle("textAlign", "left");
} else {
// dispose
var cell = section.stretchCell(0, rowIndex, false);
cell.setStyle("backgroundColor", "");
cell.setStyle("textAlign", "");
}
},
staticDataRows: records
};
var grid = document.getElementById("grid");
grid.config = configObj;
</code></pre>
</code-sandbox><h2 id="multiple-row-expansions-from-a-single-row">Multiple row expansions from a single row</h2>
<p>You can specify the number of row expansions to be added on a single row by passing the number as the third parameter for <code>toggleRowExpansion(rowId, null, numRows)</code>. </p>
<blockquote>
<p>Note that row expansion has no row data. So the entire data set for multiple row expansions have to be stored on the expanded row. Luckily, any data structure can be stored on the row data. For instance, you can have an array of objects representing states of row expansions, like so:</p>
</blockquote>
<pre><code class="language-js">dv.setData(rowId, "My_Row_Expansion_States", [
{name: "Row 1", value: 1},
{name: "Row 2", value: 2},
// ...
]);
</code></pre>
<h2 id="handling-asynchronous-content">Handling asynchronous content</h2>
<p>Rows can be shifted and data can be updated during the asynchronous process, such as waiting for a response from a server. It is a good idea to always use the row ID for referencing a row and <strong>not</strong> assume that rows or elements stay in the same place. </p>
<p>The example below shows how to render custom content with asynchronous data. The row ID is retrieved and used for referencing after the server response.</p>
<code-sandbox hash="50dbc103"><pre><code class="language-css">efx-grid {
height: 200px;
}
html hr {
margin: 5px;
}
</code></pre>
<pre><code class="language-html"><efx-grid id="grid"></efx-grid>
<hr>
</code></pre>
<pre><code class="language-javascript">import { halo } from './theme-loader.js'; // This line is only required for demo purpose. It is not relevant for your application.
await halo(); // This line is only required for demo purpose. It is not relevant for your application.
/* ---------------------------------- Note ----------------------------------
DataGenerator, Formatters and extensions are exposed to global scope
in the bundle file to make it easier to create live examples.
Importing formatters and extensions is still required in your application.
Please see the document for further information.
---------------------------------------------------------------------------*/
var fields = ["companyName", "market", "CF_LAST", "CF_NETCHNG", "industry"];
var records = DataGenerator.generateRecords(fields, { numRows: 10 });
var onRowExpansionBinding = function(e) {
var rowData = e.originalRowData;
var rowIndex = e["rowIndex"];
var section = e["section"];
if (e.rowExpansion && rowData) {
// render
var cell = section.stretchCell(0, rowIndex, true);
cell.setStyle("backgroundColor", "#cc7755");
section.setRowHeight(rowIndex, 72); // Customized height
var expContent = cell.getContent();
if (!expContent || !expContent._myExpansion) {
expContent = document.createElement("div");
expContent._myExpansion = true;
var firstLine = expContent._firstLine = document.createElement("div");
var secondLine = expContent._secondLine = document.createElement("div");
var loader = expContent._loader = document.createElement("ef-loader");
var button = document.createElement("button");
button.textContent = "Column & Row Index";
button.addEventListener("click", function (e) {
var pos = grid.api.getRelativePosition(e);
alert(pos.colIndex + ", " + pos.rowIndex);
});
secondLine.appendChild(button);
expContent.appendChild(firstLine);
expContent.appendChild(secondLine);
expContent.appendChild(loader);
}
var rowData = e.originalRowData;
if (rowData["My_Expansion_Status"] === "loading") {
expContent._firstLine.style.display = "none";
expContent._secondLine.style.display = "none";
expContent._loader.style.display = "";
} else {
expContent._firstLine.style.display = "";
expContent._secondLine.style.display = "";
expContent._loader.style.display = "none";
}
cell.setContent(expContent);
} else {
// dispose
var cell = section.stretchCell(0, rowIndex, false);
cell.setStyle("backgroundColor", "");
section.setRowHeight(rowIndex, section.getDefaultRowHeight()); // Default height
}
}
var onCellCliked = function(e) {
var pos = grid.api.getRelativePosition(e);
if (pos.sectionType === "content") { // Prevent clicking on header section
var rowIndex = pos.rowIndex;
var rowDef = grid.api.getRowDefinition(rowIndex);
if (rowDef) { // Prevent clicking on expanded row
var rowId = rowDef.getRowId();
var dv = grid.api.getDataView();
if (!dv.isRowExpansion(rowId)) {
if (rowDef.getData("My_Expansion_Status") == null) {
rowDef.setData("My_Expansion_Status", "loading");
// Simulate delay from data request
setTimeout(onServerResponse.bind(null, dv, rowDef), 3000); // Asynchronous
}
dv.toggleRowExpansion(pos.rowIndex);
}
}
}
};
var onServerResponse = function(dv, rowDef) {
if (!dv.isRowExpansion(rowDef.getRowId())) {
rowDef.setData("My_Expansion_Status", "data received");
}
grid.api.getCoreGrid().requestRowRefresh(); // WORKAROUND: Force re-rendering
};
var configObj = {
columnReorder: true,
sorting: {
sortableColumns: true
},
columns: [
{name: "Company", field: fields[0]},
{name: "Market", field: fields[1], width: 120},
{name: "Last", field: fields[2], width: 100},
{name: "Net. Chng", field: fields[3], width: 100},
{name: "Industry", field: fields[4]}
],
rowExpansionBinding: onRowExpansionBinding,
staticDataRows: records
};
var grid = document.getElementById("grid");
grid.config = configObj;
grid.addEventListener("click", onCellCliked);
</code></pre>
</code-sandbox>