The FlexGrid control can be easily customized to respond to keyboard events. You may need to provide keyboard support in your MVC applications using grid control, for the ease of end users. FlexGrid provides users the flexibility to manage its functionalities and edit or update data using keyboard.
This section discusses few scenarios where the FlexGrid control can be customized to be operated using keyboard.
Assigning Hot Keys in FlexGrid
You can assign appropriate Hot Keys in an MVC application, through code. This can ease many manual tasks and save time. For instance, while editing data in a grid that shows immense data in nearly 500 rows and 10 columns, you might find it appropriate to use Hot keys. In the below example, we will use Ctrl+q keys ,which will serve as Hot Keys, to copy the cell data from one column to another in the active row.
Note that the below code extends application created in Quick Start topic.
Razor |
Copy Code
|
---|---|
<script type=text/JavaScript> var grid; c1.mvc.Utils.documentReady(function () { grid = wijmo.Control.getControl('#gsFlexGrid'); gridCV = grid.collectionView; if (grid) { var host = grid.hostElement; //handle the keydown event host.addEventListener('keydown', function (e) { if (e.key == "q" && e.ctrlKey) { grid.setCellData(grid.selection.row,4, grid.getCellData(grid.selection.row, 3)); console.log("Cell data copied from Country Column to Product Column in the current row"); } }); } }); </script> <div> @(Html.C1().FlexGrid<Sale>() .Id("gsFlexGrid") .AutoGenerateColumns(true) .AllowSorting(true) .Bind(Model) .Columns(bl => { bl.Add(cb => cb.Binding("ID")); bl.Add(cb => cb.Binding("Start")); bl.Add(cb => cb.Binding("Product")); bl.Add(cb => cb.Binding("Amount").Format("c")); bl.Add(cb => cb.Binding("Discount").Format("p0")); bl.Add(cb => cb.Binding("Active")); }) </div> |
Here, we used 'hostElement' property to get the DOM element that is hosting the FlexGrid control. The code handles the 'keydown' event of FlexGrid a to capture 'Ctrl+q' combination as Hot Keys.
While editing data in FlexGrid control, you can easily exploit clipboard shortcuts, if you are hands-on in using them. This can be done by setting AutoClipboard property of your FlexGrid to true, as shown below.
Note that the below code extends application created in Quick Start topic.
Razor |
Copy Code
|
---|---|
@(Html.C1().FlexGrid<Sale>().Id("cdInitMethod") .AutoGenerateColumns(false) .Bind(Model) .AutoClipboard(true) .Columns(bl => { bl.Add(cb => cb.Binding("ID").Header("ID")); bl.Add(cb => cb.Binding("Start").Header("Start").Format("MMM d yy")); bl.Add(cb => cb.Binding("Country").Header("Country")); bl.Add(cb => cb.Binding("Product").Header("Product")); bl.Add(cb => cb.Binding("Discount").Format("p0")); bl.Add(cb => cb.Binding("Active")); bl.Add(cb => cb.Binding("Amount").Format("n0").Visible(false)); }) ) |
The C1 FlexGrid supports following clipboard commands:
Cells that are Read-only, are not affected by paste operations. Moreover, the clipboard operations are only supported for visible rows and columns of a grid.
The C1FlexGrid has built-in capability to move focus to the next cell in the same column of the grid, when a user presses Enter key. However, a user can modify this behavior such that the focus moves across the same row when Enter key is pressed. For example, a user handling sales data in Grid might be comfortable in editing row-wise. He/she would like to switch over to the next cell in the same row once finished editing a cell.
This can be accomplished by handling the keydown event of the hostElement for FlexGrid and setting the selection to the next cell in the same row, as shown below.
Note that the below code extends application created in Quick Start topic.
Razor |
Copy Code
|
---|---|
<script type="text/javascript"> function onClientLoadedRows(e, args) { var g = e.hostElement; $(g).keydown(function (s, args1) { if (s.which == 13) { var flex = wijmo.Control.getControl("#gFlexGrid"); var sel = flex.selection; var row = sel.row - 1; var col = sel.col; if (col < flex.columns.length - 1) { var rng = new wijmo.grid.CellRange(row, col + 1, row, col + 1); flex.select(rng, true); } else { var rng = new wijmo.grid.CellRange(row + 1, 0, row + 1, 0); flex.select(rng, true); } } }); }</script> <div> @(Html.C1().FlexGrid<Sale>() .Id("gFlexGrid") .AutoGenerateColumns(true) .AllowSorting(true) .Bind(Model) .IsReadOnly(false) .SelectionMode(C1.Web.Mvc.Grid.SelectionMode.Cell) .OnClientLoadedRows("onClientLoadedRows") .Columns(bl =>{ bl.Add(cb => cb.Binding("ID")); bl.Add(cb => cb.Binding("Start")); bl.Add(cb => cb.Binding("Product")); bl.Add(cb => cb.Binding("Amount").Format("c")); bl.Add(cb => cb.Binding("Discount").Format("p0")); bl.Add(cb => cb.Binding("Active")); }) </div> |
In the above code, OnClientLoadedRows event is handled. Note that, same behavior is available by default if user presses Tab key once finished editing.
Back to Top
While editing data in FlexGrid, you may find it necessary to lock a particular row, to prevent editing entries in it. For instance, an HR person is managing payroll data of an organization, using a Grid control. He/she can lock a row or two in the grid, such that details related to specific employees are not affected while editing operations are being done on employee data, displayed in grid.
This can be done by handling the OnClientBeginningEdit event of FlexGrid, as shown below.
Note that the below code extends application created in Quick Start topic.
Razor |
Copy Code
|
---|---|
<script type= “text\javascript”> function OnClientBeginningEdit(e, args) { if (args.row == 0) { args.cancel = true; } } </script> <div> @(Html.C1().FlexGrid<Sale>().Id("gFlexGrid") .AutoGenerateColumns(true) .AllowSorting(true) .Bind(Model) .IsReadOnly(false) .SelectionMode(C1.Web.Mvc.Grid.SelectionMode.Cell) .OnClientBeginningEdit("onClientBeginningEdit") .Columns(bl => { bl.Add(cb => cb.Binding("ID")); bl.Add(cb => cb.Binding("Start")); bl.Add(cb => cb.Binding("Product")); bl.Add(cb => cb.Binding("Amount").Format("c")); bl.Add(cb => cb.Binding("Discount").Format("p0")); bl.Add(cb => cb.Binding("Active")); }) </div> |
Here, the event handler cancels the event whenever the specified row enters the edit mode, in this case the first row.