Delete Rows with Ctrl+Delete

You can handle keyboard events in the FlexGrid by adding HTML event listeners to hostElement of the grid.

For example, the grid below listens to the 'keydown' event. If you press Ctrl+Delete, it shows a prompt and deletes the current row. Note that:

  1. The event listener is added with the capture parameter set to true, so that it is called before the grid gets it.
  2. The event handler calls the event's preventDefault method, so that the grid does not handle the Delete key as it normally would.

Press Ctrl+Delete to remove the current row:

Id
Country
Sales
Expenses
Active
0
US
7,369.65
3,843.53
1
Canada
1,542.22
3,775.07
2
Mexico
7,503.82
2,068.35
3
Germany
9,308.80
4,960.89
4
UK
9,176.59
3,063.97
5
France
8,420.85
4,562.03
6
Italy
5,656.32
1,702.22
7
Greece
5,842.60
1,603.82
8
Holland
3,282.82
2,602.94
9
Japan
4,191.17
762.12
10
Korea
9,985.49
2,886.48
11
China
9,281.86
963.77
12
US
6,239.23
642.95
13
Canada
9,910.35
2,581.93
14
Mexico
6,384.64
2,218.76
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
// This file locates: "Scripts/Lesson/C1Input/PopupDialogs.js".
c1.documentReady(function () {
    var theGrid = wijmo.Control.getControl('#theGrid');
    var theDialog = wijmo.Control.getControl('#theDialog');
 
    theGrid.itemsSource = getData();
 
    // use ctrl+Delete to delete the current row
    theGrid.hostElement.addEventListener('keydown', function (e) {
        var view = theGrid.collectionView;
 
        // looking for ctrl+Delete
        if (e.ctrlKey && e.keyCode == wijmo.Key.Delete && view.currentItem) {
 
            // prevent the grid from getting the key
            e.preventDefault();
 
            // confirm row deletion
            theDialog.show(true, function (sender) {
 
                // delete the row
                if (sender.dialogResult == 'wj-hide-ok') {
                    view.remove(view.currentItem);
                }
 
                // return focus to the grid
                theGrid.focus();
            });
        }
    }, true); // grab the event before the grid
 
    // generate some random data
    function getData() {
        var countries = 'US,Canada,Mexico,Germany,UK,France,Italy,Greece,Holland,Japan,Korea,China'.split(','),
        data = [];
        for (var i = 0; i < 1000; i++) {
            data.push({
                id: i,
                country: countries[i % countries.length],
                sales: Math.random() * 10000,
                expenses: Math.random() * 5000,
                active: i % 3 == 0
            });
        }
        return data;
    }
});
1
2
3
4
5
6
7
8
9
10
11
12
13
using System.Web.Mvc;
 
namespace LearnMvcClient.Controllers
{
    public partial class C1InputController : Controller
    {
        // GET: PopupDialogs
        public ActionResult PopupDialogs()
        {
            return View();
        }
    }
}
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
<h1>
    @Html.Raw(Resources.C1Input.PopupDialogs_Title)
</h1>
<p>
    @Html.Raw(Resources.C1Input.PopupDialogs_Text1)
</p>
<p>
    @Html.Raw(Resources.C1Input.PopupDialogs_Text2)
</p>
<ol>
    <li>
        @Html.Raw(Resources.C1Input.PopupDialogs_Text3)
    </li>
    <li>
        @Html.Raw(Resources.C1Input.PopupDialogs_Text4)
    </li>
</ol>
 
<p>
    @Html.Raw(Resources.C1Input.PopupDialogs_Text5)
</p>
 
@Html.C1().FlexGrid().Id("theGrid").AllowAddNew(true).Height(300)
<div id="theDialog" style="display:none">
    <div class="wj-dialog-header">
        @Html.Raw(Resources.C1Input.PopupDialogs_Text6)
    </div>
    <div class="wj-dialog-body" tabindex="-1">
        @Html.Raw(Resources.C1Input.PopupDialogs_Text7)
    </div>
    <div class="wj-dialog-footer">
        <button class="btn btn-primary wj-hide-ok">@Resources.Resource.Btn_Yes</button>
        <button class="btn btn-default wj-hide">@Resources.Resource.Btn_No</button>
    </div>
</div>
@Html.C1().Popup("#theDialog")