Skip to content

Commit c390dc1

Browse files
author
Kendo Bot
committed
Sync with Kendo UI Professional
1 parent 7aa0bc6 commit c390dc1

File tree

3 files changed

+324
-337
lines changed

3 files changed

+324
-337
lines changed
Lines changed: 142 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
---
2-
title: Selection Export
3-
page_title: Selection Export
2+
title: Selection & Export
3+
page_title: Selection & Export
44
description: "With Telerik UI Grid for {{ site.framework }} you can enable users to select specific cells and export them to Excel or Telerik UI Chart"
55
slug: exportingselection_gridhelper_aspnetcore
66
position: 6
77
---
88

9-
# Selection Export
9+
# Selection & Export
1010

1111
The Grid widget allows users to select specific cells and then to export them to Excel or a Telerik UI Chart for {{ site.framework }} .
1212

@@ -16,6 +16,8 @@ For runnable example, refer to the [Demo on copying/exporting selected cells](ht
1616

1717
The following sections provide step-by-step instructions and examples on getting started with the Grid Selection Export functionality.
1818

19+
> The selection export functionality relies on the client-side Grid API. In this example, a Telerik UI ContextMenu is used to execute Grid methods related to copying and exporting of the selected cells.
20+
1921
### Enabling Excel Export
2022

2123
1. Configure the [Selectable]({% slug htmlhelpers_grid_aspnetcore_selection %}) property and apply the following settings:
@@ -30,9 +32,6 @@ The following sections provide step-by-step instructions and examples on getting
3032

3133
@(Html.Kendo().Grid<OrderViewModel>()
3234
.Name("grid")
33-
.ToolBar(tools => {
34-
tools.Custom().Name("copy").Text("Copy Selected").IconClass("k-icon k-i-copy");
35-
})
3635
.Selectable(selectable => selectable
3736
.Mode(GridSelectionMode.Multiple)
3837
.Type(GridSelectionType.Cell))
@@ -42,149 +41,193 @@ The following sections provide step-by-step instructions and examples on getting
4241
)
4342
)
4443

44+
### Initializing a ContextMenu
45+
46+
1. Add an icon for the ContextMenu.
47+
48+
```html
49+
<span class='k-primary k-bg-primary k-icon k-i-menu contextMenuIcon'></span>
50+
```
51+
52+
1. Create the widget.
53+
54+
@(Html.Kendo().ContextMenu()
55+
.Name("contextmenu")
56+
.Target(".contextMenuIcon")
57+
.ShowOn("click")
58+
.AlignToAnchor(true)
59+
.Items(items =>
60+
{
61+
items.Add().Text("Copy").HtmlAttributes(new { id = "copy" });
62+
items.Add().Text("Copy with Headers").HtmlAttributes(new { id = "copyWithHeaders" });
63+
items.Add().Separator(true);
64+
items.Add().Text("Export").HtmlAttributes(new { id = "export" });
65+
items.Add().Text("Export with Headers").HtmlAttributes(new { id = "exportWithHeaders" });
66+
items.Add().Text("Export to Chart").HtmlAttributes(new { id = "exportToChart" });
67+
})
68+
.Events(ev => ev.Select("onSelect"))
69+
)
70+
71+
1. Define the event handling function.
72+
73+
```javascript
74+
function onSelect(e) {
75+
var item = e.item.id;
76+
77+
switch (item) {
78+
case "copy":
79+
copySelected();
80+
break;
81+
case "copyWithHeaders":
82+
copySelectedWithHeaders();
83+
break;
84+
case "export":
85+
exportSelected();
86+
break;
87+
case "exportWithHeaders":
88+
exportSelectedWithHeaders();
89+
break;
90+
case "exportToChart":
91+
exportToChart();
92+
break;
93+
default:
94+
break;
95+
};
96+
}
97+
```
98+
4599
### Copying Selected Data
46100

47101
To enable users to copy the selected data, call the [`copySelectionToClipboard`](https://docs.telerik.com/kendo-ui/api/javascript/ui/grid/methods/copyselectiontoclipboard) method.
48102

49-
1. Define a custom command in the toolbar:
103+
```javascript
104+
function copySelected() {
105+
let selected = grid.select();
50106

51-
@(Html.Kendo().Grid<OrderViewModel>()
52-
.Name("grid")
53-
.ToolBar(tools => {
54-
tools.Custom().Name("copy").Text("Copy Selected").IconClass("k-icon k-i-copy");
55-
})
56-
.Selectable(selectable => selectable
57-
.Mode(GridSelectionMode.Multiple)
58-
.Type(GridSelectionType.Cell))
59-
.DataSource(dataSource => dataSource
60-
.Ajax()
61-
.Read(read => read.Action("Orders_Read", "Grid"))
62-
)
63-
)
107+
if (selected.length === 0) {
108+
kendo.alert("Please select cells before copying.");
109+
return;
110+
}
111+
112+
grid.copySelectionToClipboard(false);
113+
}
64114

65-
1. On click of the `Copy Selected` button, copy the selected cells:
115+
function copySelectedWithHeaders() {
116+
let selected = grid.select();
66117

67-
<script>
68-
$(".k-grid-copy").on("click", function (e) {
69-
e.preventDefault();
70-
var grid = $("#grid").data("kendoGrid");
71-
let selected = grid.select();
118+
if (selected.length === 0) {
119+
kendo.alert("Please select cells before copying.");
120+
return;
121+
}
72122

73-
/* Set to true in order to copy the headers as well. */
74-
grid.copySelectionToClipboard(false);
75-
});
76-
</script>
123+
grid.copySelectionToClipboard(true);
124+
}
125+
```
77126

78-
### Exporting Selected Data
127+
### Exporting Selected Data to Excel
79128

80129
To enable users to export the selected data, call the [`exportSelectedToExcel`](https://docs.telerik.com/kendo-ui/api/javascript/ui/grid/methods/exportselectedtoexcel) method.
81130

82-
1. Define a custom command in the toolbar.
131+
```javascript
132+
function exportSelected() {
133+
let selected = grid.select();
83134

84-
@(Html.Kendo().Grid<OrderViewModel>()
85-
.Name("grid")
86-
.ToolBar(tools => {
87-
tools.Custom().Name("export").Text("Export Selected").IconClass("k-icon k-i-excel");
88-
})
89-
.Selectable(selectable => selectable
90-
.Mode(GridSelectionMode.Multiple)
91-
.Type(GridSelectionType.Cell))
92-
.DataSource(dataSource => dataSource
93-
.Ajax()
94-
.Read(read => read.Action("Orders_Read", "Grid"))
95-
)
96-
)
135+
if (selected.length === 0) {
136+
kendo.alert("Please select cells before exporting.");
137+
return;
138+
}
139+
grid.exportSelectedToExcel(false);
140+
}
97141

98-
1. On click of the `Export Selected` button, export the selected cells.
142+
function exportSelectedWithHeaders() {
143+
let selected = grid.select();
99144

100-
<script>
101-
$(".k-grid-export").on("click", function (e) {
102-
e.preventDefault();
103-
var grid = $("#grid").data("kendoGrid");
104-
let selected = grid.select();
145+
if (selected.length === 0) {
146+
kendo.alert("Please select cells before exporting.");
147+
return;
148+
}
105149

106-
/* Set to true in order to export the headers as well. */
107-
grid.exportSelectedToExcel(false);
108-
});
109-
</script>
150+
grid.exportSelectedToExcel(true);
151+
}
152+
```
110153

111154
### Exporting Selected Data to Chart
112155

113-
To enable users to export the selected data to a Telerik UI Chart, call the [`getSelectedData`](https://docs.telerik.com/kendo-ui/api/javascript/ui/grid/methods/getselecteddata) method and initialize a Chart widget with the data.
156+
To enable users to export the selected data to a Kendo UI Chart, call the [`getSelectedData`](https://docs.telerik.com/kendo-ui/api/javascript/ui/grid/methods/getselecteddata) method and initialize a Chart widget with the data.
114157

115158
1. Add an empty div before initializing the Grid.
116159

117160
<div id="chart-container"></div>
118161

119-
1. Define a custom command in the toolbar.
120-
121-
@(Html.Kendo().Grid<OrderViewModel>()
122-
.Name("grid")
123-
.ToolBar(tools => {
124-
tools.Custom().Name("exportChart").Text("Export Selected To Chart").IconClass("k-icon k-i-column-clustered");
125-
})
126-
.Selectable(selectable => selectable
127-
.Mode(GridSelectionMode.Multiple)
128-
.Type(GridSelectionType.Cell))
129-
.DataSource(dataSource => dataSource
130-
.Ajax()
131-
.Read(read => read.Action("Orders_Read", "Grid"))
132-
)
133-
)
134-
135-
1. On click of the `Export Selected To Chart` button, initialize a Kendo UI Chart inside a Kendo UI Window with the selected data.
136-
137-
<script>
138-
$(".k-grid-exportChart").on("click", function (e) {
139-
var grid = $('#grid').data('kendoGrid');
162+
1. Define the `exportToChart` method as demonstrated below.
140163

164+
```javascript
165+
function exportToChart() {
141166
var container = $('#chart-container');
142-
143-
/* Get a reference to the Kendo Window widget if it has been initialized. */
144167
var windowInstance = $('#chart-container').data('kendoWindow');
145168
var currInstance = container.find('.k-chart').data('kendoChart');
146-
147-
/* Get the data from the selected cells. */
169+
/* Get the selected data. */
148170
var data = grid.getSelectedData();
149-
var categoryField;
150171

151172
if (!data.length) {
152173
kendo.alert('Please select cells before exporting.');
153174
return;
154175
}
155176

156-
/* Set the categoryField of the chart using one of the grid fields. */
157-
categoryField = data[0].ShipCountry ? 'ShipCountry' : 'ShipCity';
177+
/* If the user selects only a value(Freight) without a category(ShipCountry), set the ShipCountry name to Uknown.*/
178+
let unknownCountries = $.extend(true, [], data);
179+
unknownCountries.forEach(function (item, index, array) {
180+
if (!array[index].ShipCountry) {
181+
array[index].ShipCountry = "Unknown"
182+
}
183+
});
158184

159-
/* If the Kendo Window widget is undefined, initialize it. */
160-
if (!windowInstance) {
161-
windowInstance = container.kendoWindow({ width: 800 }).data('kendoWindow');
185+
/* Destroy the window instance. */
186+
if (windowInstance) {
187+
windowInstance.destroy();
162188
}
163189

164-
/* If the chart widget already exists, destroy it. */
190+
/* Destroy the chart instance. */
165191
if (currInstance) {
166192
currInstance.destroy();
167193
}
168194

169-
/* Empty the div element. */
195+
/* Initialize a new window instance and increase it's width for every row that has been selected. This way the chart can fit properly. */
196+
let windowWidth = data.length > 5 ? data.length * 75 : 500;
197+
windowInstance = container.kendoWindow({ width: windowWidth }).data('kendoWindow');
170198
container.empty();
171-
/* Append an empty div to the container. */
199+
200+
/* Create a chart using the data and append it to the window. */
172201
var element = $('<div></div>').appendTo(container);
173202
windowInstance.open().center();
174-
175-
/* Initialize a Chart from the appended div. */
176203
element.kendoChart({
204+
dataSource: {
205+
data: unknownCountries
206+
},
177207
series: [{
178208
type: "column",
179-
categoryField: categoryField,
180-
field: 'Freight',
181-
data: data
182-
}]
209+
field: 'Freight'
210+
}],
211+
categoryAxis: {field: "ShipCountry"}
183212
});
184-
});
185-
</script>
213+
}
214+
```
215+
216+
## Selection Types
217+
218+
The following selection types are supported:
219+
220+
- Cell selection - the user holds down the `CTRL` key (`Command` key on Mac) and uses the `left-click` of the mouse to select cells.
221+
- Range selection - the user holds down the `left-click` on the mouse and drags across a range of cells.
222+
- Range and Cell selection - the user can combine the two approaches from above and select both a range and separate cells.
223+
- Range combination selection - the user performs a range selection and while holding the `CTRL` key (`Command` key on Mac), they perform another range selection.
224+
225+
## Known Limitations
226+
227+
- The `copySelectionToClipboard`, `exportSelectedToExcel` and `getSelectedData` methods do not work with rows that are persisted across different pages.
228+
- The Export to Chart method does not work with `Range and Cell selection` type.
186229

187230
## See Also
188231

189-
* [Selection Copy/Export by the Grid HtmlHelper for {{ site.framework }} (Demo)](https://demos.telerik.com/{{ site.platform }}/grid/advanced-export)
232+
* [Selection Copy/Export by the Grid HtmlHelper for {{ site.framework }} (Demo)](https://demos.telerik.com/{{ site.platform }}/grid/selection-export)
190233
* [Server-Side API](/api/grid)

0 commit comments

Comments
 (0)