KB - Wijmo 5 - Custom Keyboard Events

This sample shows how to implement custom keyboard events in FlexGrid.

by Joel Parks

HTML

<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script src="http://cdn.wijmo.com/5.20143.24/controls/wijmo.min.js"></script>
<link rel="stylesheet" href="http://cdn.wijmo.com/5.20143.24/styles/wijmo.min.css">
<script src="http://cdn.wijmo.com/5.20143.24/controls/wijmo.grid.min.js"></script>
<script src="http://cdn.wijmo.com/5.20143.24/controls/wijmo.chart.min.js"></script>
<script src="http://cdn.wijmo.com/5.20143.24/interop/angular/wijmo.angular.min.js"></script>
<!-- mark this as an Angular application and give it a controller -->
<div ng-app="app" ng-controller="appCtrl">
    
    <h1>Custom Keyboard Events</h1>

    <p><b>Ctrl+Home:</b> Select the first element in the FlexGrid</p>
		<p><b>Ctrl+End:</b> Select the last element in the FlexGrid</p>
    <wj-flex-grid items-source="data" initialized="initialized(s,a)">
    </wj-flex-grid>
</div>

JavaScript

// define app, include Wijmo 5 directives
var app = angular.module('app', ['wj']);

// controller
app.controller('appCtrl', function ($scope) {

    // create some random data
    var countries = 'US,Germany,UK,Japan,Italy,Greece'.split(','),
        data = [];
    for (var i = 0; i < countries.length; i++) {
        data.push({
            country: countries[i],
            downloads: Math.round(Math.random() * 20000),
            sales: Math.random() * 10000,
            expenses: Math.random() * 5000
        });
    }

    // expose data as a CollectionView to get events
    $scope.data = new wijmo.collections.CollectionView(data);
    
    // custom handling for ctrl+Home and ctrl+End keys
    $scope.initialized = function(s, a) {
        s.hostElement.addEventListener('keydown', function(e) {
            if (e.ctrlKey) {
                if (e.keyCode == wijmo.Key.Home) {
                    s.select(0,0);
                    e.preventDefault();
                } else if (e.keyCode == wijmo.Key.End) {
                    s.select(s.rows.length-1,s.columns.length-1);
                    e.preventDefault();
                }
            }
        }, true);
    }
});