Handling Events In Kendo SpreadSheet Using jQuery

Introduction

 
Basically, JavaScript interacts with HTML though events, and different types of events will be fired when the HTML page loads. Similarly, when the user interacts with Kendo spreadsheets different types of events will be fired. We will see mostly used events in Kendo spreadsheets.
 
This article is in continuation of my previous article, where I have explained how to do remote data binding in Kendo spreadsheets.
 
Kendo SpreadSheet.html
  1. <!DOCTYPE html>    
  2. <html>    
  3. <head>    
  4.     
  5.     <style>    
  6.         html {    
  7.             font-size: 14px;    
  8.             font-family: Arial, Helvetica, sans-serif;    
  9.         }    
  10.     </style>    
  11.     <title></title>    
  12.     <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.3.1017/styles/kendo.common-material.min.css" />    
  13.     <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.3.1017/styles/kendo.material.min.css" />    
  14.     <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.3.1017/styles/kendo.material.mobile.min.css" />    
  15.     
  16.     <script src="https://kendo.cdn.telerik.com/2018.3.1017/js/jquery.min.js"></script>    
  17.     <script src="https://kendo.cdn.telerik.com/2018.3.1017/js/jszip.min.js"></script>    
  18.     <script src="https://kendo.cdn.telerik.com/2018.3.1017/js/kendo.all.min.js"></script>    
  19.     
  20.     
  21. </head>    
  22. <body>    
  23.     <div id="example">    
  24.         <div class="box wide">    
  25.             <div class="box-col">    
  26.                 <h4>Save data changes</h4>    
  27.                 <ul class="options">    
  28.                     <li>    
  29.                         <button class="k-button" id="save">Save changes</button>    
  30.                         <button class="k-button" id="cancel">Cancel changes</button>    
  31.                     </li>    
  32.                 </ul>    
  33.             </div>    
  34.         </div>    
  35.     
  36.         <div id="spreadsheet" style="width: 100%"></div>    
  37.         <script>    
  38.             $(function () {    
  39.                 var dataSource = new kendo.data.DataSource({    
  40.                     transport: {    
  41.                         read: onRead,    
  42.                     },    
  43.                     schema: {    
  44.                         model: {    
  45.                             id: "ProductID",    
  46.                             fields: {    
  47.                                 productID: { type: "number" },    
  48.                                 productName: { type: "string" },    
  49.                                 price: { type: "number" },    
  50.                                 tax: { type: "number" }    
  51.                             }    
  52.                         }    
  53.                     }    
  54.                 });    
  55.     
  56.                 $("#spreadsheet").kendoSpreadsheet({    
  57.                     columns: 20,    
  58.                     rows: 100,    
  59.                     toolbar: false,    
  60.                     sheetsbar: false,    
  61.                     sheets: [{    
  62.                         name: "Products",    
  63.                         dataSource: dataSource,    
  64.                         rows: [{    
  65.                             height: 40,    
  66.                             cells: [    
  67.                                 {    
  68.                                     bold: "true",    
  69.                                     background: "#9c27b0",    
  70.                                     textAlign: "center",    
  71.                                     color: "white",    
  72.                                     title: "ID"    
  73.                                 }, {    
  74.                                     bold: "true",    
  75.                                     background: "#9c27b0",    
  76.                                     textAlign: "center",    
  77.                                     color: "white",    
  78.                                     title:"Product Name"    
  79.                                 }, {    
  80.                                     bold: "true",    
  81.                                     background: "#9c27b0",    
  82.                                     textAlign: "center",    
  83.                                     color: "white",    
  84.                                     title:"Price"    
  85.                                 }, {    
  86.                                     bold: "true",    
  87.                                     background: "#9c27b0",    
  88.                                     textAlign: "center",    
  89.                                     color: "white"    
  90.                                 }, ]    
  91.                         }],    
  92.                         columns: [    
  93.                             { width: 100 },    
  94.                             { width: 415 },    
  95.                             { width: 145 },    
  96.                             { width: 145 },    
  97.                             { width: 145 }    
  98.                         ]    
  99.                     }]    
  100.                 });    
  101.     
  102.                 function onRead(options) {    
  103.                     $.ajax({    
  104.                         url: "http://localhost:11207/api/Products/ProductDetails",    
  105.                         dataType: "json",    
  106.                         success: function (result) {    
  107.                             options.success(result);    
  108.                         },    
  109.                         error: function (result) {    
  110.                             options.error(result);    
  111.                         }    
  112.                     });    
  113.                 }    
  114.     
  115.               
  116.         </script>    
  117.     </div>    
  118.     
  119.     
  120. </body>    
  121. </html>    
SpreadSheet in browser
 
Handling Events In Kendo SpreadSheet Using jQuery
 

Events

 
render
 
This event will be fired when the spreadsheet is completely rendered in the page.
  1. $("#spreadsheet").kendoSpreadsheet({  
  2.                    render: onRender,                    
  3.                    columns: 20,  
  4.                    rows: 100,  
  5.                    toolbar: false,  
  6.                    sheetsbar: false,  
  7.                    sheets: [{  
  8.                        name: "Products",  
  9.                        dataSource: dataSource,  
  10.                        filter: {  
  11.                            ref: "A1:D3",  
  12.                            columns: []  
  13.                        },  
  14.                        rows: [{  
  15.                            height: 40,  
  16.   
  17.                            cells: [  
  18.                                {  
  19.                                    bold: "true",  
  20.                                    background: "#9c27b0",  
  21.                                    textAlign: "center",  
  22.                                    color: "white",  
  23.                                    title: "ID"  
  24.                                }, {  
  25.                                    bold: "true",  
  26.                                    background: "#9c27b0",  
  27.                                    textAlign: "center",  
  28.                                    color: "white",  
  29.                                    title: "Product Name"  
  30.                                }, {  
  31.                                    bold: "true",  
  32.                                    background: "#9c27b0",  
  33.                                    textAlign: "center",  
  34.                                    color: "white",  
  35.                                    title: "Price"  
  36.                                }, {  
  37.                                    bold: "true",  
  38.                                    background: "#9c27b0",  
  39.                                    textAlign: "center",  
  40.                                    color: "white"  
  41.                                },]  
  42.                        }],  
  43.                        columns: [  
  44.                            { width: 100 },  
  45.                            { width: 415 },  
  46.                            { width: 145 },  
  47.                            { width: 145 },  
  48.                            { width: 145 }  
  49.                        ]  
  50.                    }]  
  51.                });  
  52.                

  1. function onRender(e) {  
  2. console.log("Render Event")  
  3. }  
From the above code it is obvious the onRender function will be fired, when the spreadsheet renders in the page. 
 
select 
 
This event will be fired when there is a selection change in the spreadsheet.
  1. $("#spreadsheet").kendoSpreadsheet({                   
  2.                    select: onSelect,                                     
  3.                    columns: 20,  
  4.                    rows: 100,  
  5.                    toolbar: false,  
  6.                    sheetsbar: false,  
  7.                    sheets: [{  
  8.                        name: "Products",  
  9.                        dataSource: dataSource,  
  10.                        filter: {  
  11.                            ref: "A1:D3",  
  12.                            columns: []  
  13.                        },  
  14.                        rows: [{  
  15.                            height: 40,  
  16.   
  17.                            cells: [  
  18.                                {  
  19.                                    bold: "true",  
  20.                                    background: "#9c27b0",  
  21.                                    textAlign: "center",  
  22.                                    color: "white",  
  23.                                    title: "ID"  
  24.                                }, {  
  25.                                    bold: "true",  
  26.                                    background: "#9c27b0",  
  27.                                    textAlign: "center",  
  28.                                    color: "white",  
  29.                                    title: "Product Name"  
  30.                                }, {  
  31.                                    bold: "true",  
  32.                                    background: "#9c27b0",  
  33.                                    textAlign: "center",  
  34.                                    color: "white",  
  35.                                    title: "Price"  
  36.                                }, {  
  37.                                    bold: "true",  
  38.                                    background: "#9c27b0",  
  39.                                    textAlign: "center",  
  40.                                    color: "white"  
  41.                                },]  
  42.                        }],  
  43.                        columns: [  
  44.                            { width: 100 },  
  45.                            { width: 415 },  
  46.                            { width: 145 },  
  47.                            { width: 145 },  
  48.                            { width: 145 }  
  49.                        ]  
  50.                    }]  
  51.                });  
  52.                
  53.          

  1. function onSelect(e) {  
  2. console.log("on select event the cell value ", e.range.value())  
  3. }  
onSelect function will be fired when there is a selection change in the spreadsheet. 
 
change 
 
This event will be fired when there is any change in the spreadsheet from its initial state.
  1. $("#spreadsheet").kendoSpreadsheet({                                        
  2.                    change: onChange,                      
  3.                    columns: 20,  
  4.                    rows: 100,  
  5.                    toolbar: false,  
  6.                    sheetsbar: false,  
  7.                    sheets: [{  
  8.                        name: "Products",  
  9.                        dataSource: dataSource,  
  10.                        filter: {  
  11.                            ref: "A1:D3",  
  12.                            columns: []  
  13.                        },  
  14.                        rows: [{  
  15.                            height: 40,  
  16.   
  17.                            cells: [  
  18.                                {  
  19.                                    bold: "true",  
  20.                                    background: "#9c27b0",  
  21.                                    textAlign: "center",  
  22.                                    color: "white",  
  23.                                    title: "ID"  
  24.                                }, {  
  25.                                    bold: "true",  
  26.                                    background: "#9c27b0",  
  27.                                    textAlign: "center",  
  28.                                    color: "white",  
  29.                                    title: "Product Name"  
  30.                                }, {  
  31.                                    bold: "true",  
  32.                                    background: "#9c27b0",  
  33.                                    textAlign: "center",  
  34.                                    color: "white",  
  35.                                    title: "Price"  
  36.                                }, {  
  37.                                    bold: "true",  
  38.                                    background: "#9c27b0",  
  39.                                    textAlign: "center",  
  40.                                    color: "white"  
  41.                                },]  
  42.                        }],  
  43.                        columns: [  
  44.                            { width: 100 },  
  45.                            { width: 415 },  
  46.                            { width: 145 },  
  47.                            { width: 145 },  
  48.                            { width: 145 }  
  49.                        ]  
  50.                    }]  
  51.                });  
  52.       
  53.                
  1. function onChange(e) {  
  2.         console.log("on change event the cell value ", e.range.value())  
  3.         }  
onChange() will be fired when there is a change in cell data; we are passing the updated event object to the function to get an updated value when there is a change. 
 
Column Events
 
a. hideColumn – This event will be fired when the column is hidden using the context menu in the spreadsheet.
b. deleteColumn – This event will be fired when column is deleted using the context menu in the spreadsheet.
  1. $("#spreadsheet").kendoSpreadsheet({                                                       
  2.                    hideColumn: onHideColumn,  
  3.                    deleteColumn: onDeleteColumn,                     
  4.                    columns: 20,  
  5.                    rows: 100,  
  6.                    toolbar: false,  
  7.                    sheetsbar: false,  
  8.                    sheets: [{  
  9.                        name: "Products",  
  10.                        dataSource: dataSource,  
  11.                        filter: {  
  12.                            ref: "A1:D3",  
  13.                            columns: []  
  14.                        },  
  15.                        rows: [{  
  16.                            height: 40,  
  17.   
  18.                            cells: [  
  19.                                {  
  20.                                    bold: "true",  
  21.                                    background: "#9c27b0",  
  22.                                    textAlign: "center",  
  23.                                    color: "white",  
  24.                                    title: "ID"  
  25.                                }, {  
  26.                                    bold: "true",  
  27.                                    background: "#9c27b0",  
  28.                                    textAlign: "center",  
  29.                                    color: "white",  
  30.                                    title: "Product Name"  
  31.                                }, {  
  32.                                    bold: "true",  
  33.                                    background: "#9c27b0",  
  34.                                    textAlign: "center",  
  35.                                    color: "white",  
  36.                                    title: "Price"  
  37.                                }, {  
  38.                                    bold: "true",  
  39.                                    background: "#9c27b0",  
  40.                                    textAlign: "center",  
  41.                                    color: "white"  
  42.                                },]  
  43.                        }],  
  44.                        columns: [  
  45.                            { width: 100 },  
  46.                            { width: 415 },  
  47.                            { width: 145 },  
  48.                            { width: 145 },  
  49.                            { width: 145 }  
  50.                        ]  
  51.                    }]  
  52.                });  
  1.       function onHideColumn(e) {  
  2.                console.log("Hide column event")  
  3.            }  
  4.            function onDeleteColumn(e) {  
  5.                console.log("Delete column event")  
  6.            }  
 onHideColumn() and onDeleteColumn() will be fired when we use hide and delete the column from the context menu in the spreadsheet. 
 
Row events
 
Similar to column we have row events,
 
a. hideRow – This event will be fired when the row is hidden using the context menu in the spreadsheet.
b. deleteRow – This event will be fired when row is deleted using the context menu in the spreadsheet.
  1. $("#spreadsheet").kendoSpreadsheet({                                                           
  2.                    hideRow: onHideRow,  
  3.                    deleteRow: onDeleteRow,  
  4.                    columns: 20,  
  5.                    rows: 100,  
  6.                    toolbar: false,  
  7.                    sheetsbar: false,  
  8.                    sheets: [{  
  9.                        name: "Products",  
  10.                        dataSource: dataSource,  
  11.                        filter: {  
  12.                            ref: "A1:D3",  
  13.                            columns: []  
  14.                        },  
  15.                        rows: [{  
  16.                            height: 40,  
  17.   
  18.                            cells: [  
  19.                                {  
  20.                                    bold: "true",  
  21.                                    background: "#9c27b0",  
  22.                                    textAlign: "center",  
  23.                                    color: "white",  
  24.                                    title: "ID"  
  25.                                }, {  
  26.                                    bold: "true",  
  27.                                    background: "#9c27b0",  
  28.                                    textAlign: "center",  
  29.                                    color: "white",  
  30.                                    title: "Product Name"  
  31.                                }, {  
  32.                                    bold: "true",  
  33.                                    background: "#9c27b0",  
  34.                                    textAlign: "center",  
  35.                                    color: "white",  
  36.                                    title: "Price"  
  37.                                }, {  
  38.                                    bold: "true",  
  39.                                    background: "#9c27b0",  
  40.                                    textAlign: "center",  
  41.                                    color: "white"  
  42.                                },]  
  43.                        }],  
  44.                        columns: [  
  45.                            { width: 100 },  
  46.                            { width: 415 },  
  47.                            { width: 145 },  
  48.                            { width: 145 },  
  49.                            { width: 145 }  
  50.                        ]  
  51.                    }]  
  52.                });  
  53.                
  1. function onHideRow(e) {  
  2.                console.log("Hide Row event")  
  3.            }  
  4.            function onDeleteRow(e) {  
  5.                console.log("Delete Row event")  
  6.            }  
onHideRow() and onDeleteRow() will be fired when the user hides or deletes the row using the context menu in the spreadsheet as shown in the below figure. 
 
pdfExport
 
This event will be fired when the user initiates the PDF export
  1. $("#spreadsheet").kendoSpreadsheet({                   
  2.                     pdfExport: onPdfExport,  
  3.                     columns: 20,  
  4.                     rows: 100,  
  5.                     toolbar: false,  
  6.                     sheetsbar: false,  
  7.                     sheets: [{  
  8.                         name: "Products",  
  9.                         dataSource: dataSource,  
  10.                         filter: {  
  11.                             ref: "A1:D3",  
  12.                             columns: []  
  13.                         },  
  14.                         rows: [{  
  15.                             height: 40,  
  16.   
  17.                             cells: [  
  18.                                 {  
  19.                                     bold: "true",  
  20.                                     background: "#9c27b0",  
  21.                                     textAlign: "center",  
  22.                                     color: "white",  
  23.                                     title: "ID"  
  24.                                 }, {  
  25.                                     bold: "true",  
  26.                                     background: "#9c27b0",  
  27.                                     textAlign: "center",  
  28.                                     color: "white",  
  29.                                     title: "Product Name"  
  30.                                 }, {  
  31.                                     bold: "true",  
  32.                                     background: "#9c27b0",  
  33.                                     textAlign: "center",  
  34.                                     color: "white",  
  35.                                     title: "Price"  
  36.                                 }, {  
  37.                                     bold: "true",  
  38.                                     background: "#9c27b0",  
  39.                                     textAlign: "center",  
  40.                                     color: "white"  
  41.                                 },]  
  42.                         }],  
  43.                         columns: [  
  44.                             { width: 100 },  
  45.                             { width: 415 },  
  46.                             { width: 145 },  
  47.                             { width: 145 },  
  48.                             { width: 145 }  
  49.                         ]  
  50.                     }]  
  51.                 });  
  1. function onPdfExport(e) {  
  2.               console.log("Export to PDF event, file name ", e.options.pdf.fileName)  
  3.           }  
  4.          
onPdfExport() will be fired when the pdf export happens from the spreadsheet, from the event object we can get all the spreadsheet details, from the above which we used to print pdf filename in console. 
 
Complete Code 
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     
  5.     <style>  
  6.         html {  
  7.             font-size: 14px;  
  8.             font-family: Arial, Helvetica, sans-serif;  
  9.         }  
  10.     </style>  
  11.     <title></title>  
  12.     <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.3.1017/styles/kendo.common-material.min.css" />  
  13.     <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.3.1017/styles/kendo.material.min.css" />  
  14.     <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.3.1017/styles/kendo.material.mobile.min.css" />  
  15.   
  16.     <script src="https://kendo.cdn.telerik.com/2018.3.1017/js/jquery.min.js"></script>  
  17.     <script src="https://kendo.cdn.telerik.com/2018.3.1017/js/jszip.min.js"></script>  
  18.     <script src="https://kendo.cdn.telerik.com/2018.3.1017/js/kendo.all.min.js"></script>  
  19.   
  20.   
  21. </head>  
  22. <body>  
  23.     <div id="example">  
  24.         <button id="exportPDF" class="k-button">Export to PDF</button>  
  25.   
  26.         <div id="spreadsheet" style="width: 100%"></div>  
  27.         <script>  
  28.   
  29.             function onRender(e) {  
  30.                 console.log("Render Event")  
  31.             }  
  32.             function onSelect(e) {  
  33.                 console.log("on select event the cell value ", e.range.value())  
  34.             }  
  35.             function onChange(e) {  
  36.                 console.log("on change event the cell value ", e.range.value())  
  37.             }  
  38.             function onPdfExport(e) {  
  39.                 console.log("Export to PDF event, file name ", e.options.pdf.fileName)  
  40.             }  
  41.            
  42.             function onHideColumn(e) {  
  43.                 console.log("Hide column event")  
  44.             }  
  45.             function onDeleteColumn(e) {  
  46.                 console.log("Delete column event")  
  47.             }  
  48.                 
  49.             function onHideRow(e) {  
  50.                 console.log("Hide Row event")  
  51.             }  
  52.             function onDeleteRow(e) {  
  53.                 console.log("Delete Row event")  
  54.             }  
  55.               
  56.   
  57.             $(function () {  
  58.                 var dataSource = new kendo.data.DataSource({  
  59.                     transport: {  
  60.                         read: onRead,  
  61.   
  62.                     },  
  63.                     schema: {  
  64.                         model: {  
  65.                             id: "ProductID",  
  66.                             fields: {  
  67.                                 productID: { type: "number" },  
  68.                                 productName: { type: "string" },  
  69.                                 price: { type: "number" },  
  70.                                 tax: { type: "number" }  
  71.                             }  
  72.                         }  
  73.                     }  
  74.                 });  
  75.   
  76.                 $("#spreadsheet").kendoSpreadsheet({  
  77.                     render: onRender,  
  78.                     select: onSelect,  
  79.                     change: onChange,  
  80.                     pdfExport: onPdfExport,                     
  81.                     hideColumn: onHideColumn,  
  82.                     deleteColumn: onDeleteColumn,                      
  83.                     unhideRow: onUnhideRow,  
  84.                     hideRow: onHideRow,  
  85.                     deleteRow: onDeleteRow,                      
  86.                     pdfExport: onPdfExport,  
  87.                     columns: 20,  
  88.                     rows: 100,  
  89.                     toolbar: false,  
  90.                     sheetsbar: false,  
  91.                     sheets: [{  
  92.                         name: "Products",  
  93.                         dataSource: dataSource,  
  94.                         filter: {  
  95.                             ref: "A1:D3",  
  96.                             columns: []  
  97.                         },  
  98.                         rows: [{  
  99.                             height: 40,  
  100.   
  101.                             cells: [  
  102.                                 {  
  103.                                     bold: "true",  
  104.                                     background: "#9c27b0",  
  105.                                     textAlign: "center",  
  106.                                     color: "white",  
  107.                                     title: "ID"  
  108.                                 }, {  
  109.                                     bold: "true",  
  110.                                     background: "#9c27b0",  
  111.                                     textAlign: "center",  
  112.                                     color: "white",  
  113.                                     title: "Product Name"  
  114.                                 }, {  
  115.                                     bold: "true",  
  116.                                     background: "#9c27b0",  
  117.                                     textAlign: "center",  
  118.                                     color: "white",  
  119.                                     title: "Price"  
  120.                                 }, {  
  121.                                     bold: "true",  
  122.                                     background: "#9c27b0",  
  123.                                     textAlign: "center",  
  124.                                     color: "white"  
  125.                                 },]  
  126.                         }],  
  127.                         columns: [  
  128.                             { width: 100 },  
  129.                             { width: 415 },  
  130.                             { width: 145 },  
  131.                             { width: 145 },  
  132.                             { width: 145 }  
  133.                         ]  
  134.                     }]  
  135.                 });  
  136.                 
  137.   
  138.                 
  139.   
  140.                 function onRead(options) {  
  141.                     $.ajax({  
  142.                         url: "http://localhost:11207/api/Products/ProductDetails",  
  143.                         dataType: "json",  
  144.                         success: function (result) {  
  145.                             options.success(result);  
  146.                         },  
  147.                         error: function (result) {  
  148.                             options.error(result);  
  149.                         }  
  150.                     });  
  151.                 }  
  152.   
  153.   
  154.             });  
  155.   
  156.             $("#exportPDF").on('click'function (e) {  
  157.                 exportPDF(e);  
  158.   
  159.             })  
  160.             function exportPDF(e) {  
  161.                 var spreadsheet = $("#spreadsheet").data("kendoSpreadsheet");  
  162.                 spreadsheet.saveAsPDF({ area: "selection" });  
  163.             }  
  164.              //   
  165.         </script>  
  166.     </div>  
  167.   
  168.   
  169. </body>  
  170. </html>  

Summary

 
We have seen how to initialize Kendo spreadsheet control and handle the events in the control. 
 
I hope you have enjoyed this article. Your valuable feedback, questions, or comments about this article are always welcomed.