Custom Editors

Although FlexGrid provides efficient, Excel-style editing by default, you may want to customize the editing behavior.

This example defines a CustomGridEditor class that allows any Wijmo control to be used as a grid editor:

ID
Date
Time
Country
Product
Amount
0
1/25/2025
12:00 AM
Germany
Gadget
2,157.73
1
2/25/2025
1:01 AM
Germany
Gadget
-192.74
2
3/25/2025
2:02 AM
Italy
Widget
-4,609.29
3
4/25/2025
3:03 AM
Italy
Widget
-540.45
4
5/25/2025
4:04 AM
Italy
Gadget
-2,811.21
5
6/25/2025
5:05 AM
Germany
Doohickey
2,987.86
6
7/25/2025
6:06 AM
Italy
Doohickey
3,713.49
7
8/25/2025
7:07 AM
Greece
Widget
-2,739.37
8
9/25/2025
8:08 AM
Italy
Gadget
-4,776.52
9
10/25/2025
9:09 AM
Japan
Widget
1,193.70
10
11/25/2025
10:10 AM
Greece
Gadget
-1,784.42
11
12/25/2025
11:11 AM
Germany
Gadget
-807.94
12
1/25/2025
12:12 PM
US
Widget
171.01
13
2/25/2025
1:13 PM
Greece
Doohickey
846.08
14
3/25/2025
2:14 PM
UK
Doohickey
-4,926.17
15
4/25/2025
3:15 PM
Greece
Widget
3,241.86
16
5/25/2025
4:16 PM
Greece
Gadget
872.03
17
6/25/2025
5:17 PM
Greece
Gadget
813.96
18
7/25/2025
6:18 PM
US
Doohickey
-730.23
19
8/25/2025
7:19 PM
Italy
Gadget
-3,026.26
20
9/25/2025
8:20 PM
Greece
Doohickey
593.23
21
10/25/2025
9:21 PM
Italy
Gadget
4,806.72
22
11/25/2025
10:22 PM
Japan
Widget
2,476.75
23
12/25/2025
11:23 PM
US
Gadget
1,529.08
24
1/25/2025
12:24 AM
Germany
Doohickey
4,986.02
25
2/25/2025
1:25 AM
US
Widget
2,926.64
26
3/25/2025
2:26 AM
Italy
Gadget
4,942.59
27
4/25/2025
3:27 AM
Italy
Widget
-1,060.53
28
5/25/2025
4:28 AM
Italy
Widget
824.39
29
6/25/2025
5:29 AM
Greece
Doohickey
-3,077.55
30
7/25/2025
6:30 AM
Italy
Gadget
2,579.67
31
8/25/2025
7:31 AM
UK
Gadget
-1,331.18
32
9/25/2025
8:32 AM
Greece
Doohickey
2,548.88
33
10/25/2025
9:33 AM
Italy
Gadget
-4,033.63
34
11/25/2025
10:34 AM
Greece
Widget
682.16
35
12/25/2025
11:35 AM
Germany
Gadget
3,707.05
36
1/25/2025
12:36 PM
UK
Doohickey
1,414.63
37
2/25/2025
1:37 PM
Japan
Widget
1,490.34
38
3/25/2025
2:38 PM
UK
Doohickey
-2,113.99
39
4/25/2025
3:39 PM
US
Gadget
-3,755.44
40
5/25/2025
4:40 PM
US
Widget
3,743.32
41
6/25/2025
5:41 PM
Japan
Widget
1,869.11
42
7/25/2025
6:42 PM
Germany
Doohickey
-0.83
43
8/25/2025
7:43 PM
US
Widget
4,188.82
44
9/25/2025
8:44 PM
Greece
Widget
-805.55
45
10/25/2025
9:45 PM
UK
Widget
-2,756.55
46
11/25/2025
10:46 PM
US
Widget
-718.63
47
12/25/2025
11:47 PM
Italy
Gadget
4,150.14
48
1/25/2025
12:48 AM
Greece
Gadget
474.40
49
2/25/2025
1:49 AM
US
Widget
-2,688.64
50
3/25/2025
2:50 AM
Japan
Gadget
-4,236.17
51
4/25/2025
3:51 AM
Greece
Doohickey
432.28
52
5/25/2025
4:52 AM
Germany
Gadget
3,053.47
53
6/25/2025
5:53 AM
Germany
Widget
3,289.04
54
7/25/2025
6:54 AM
Greece
Gadget
-1,799.09
55
8/25/2025
7:55 AM
Italy
Gadget
-2,376.15
56
9/25/2025
8:56 AM
Germany
Doohickey
3,300.70
57
10/25/2025
9:57 AM
US
Doohickey
3,071.67
58
11/25/2025
10:58 AM
US
Gadget
-2,388.73
59
12/25/2025
11:59 AM
Greece
Gadget
3,969.78
60
1/25/2025
12:00 PM
UK
Gadget
4,436.94
61
2/25/2025
1:01 PM
Japan
Gadget
-3,620.64
62
3/25/2025
2:02 PM
US
Gadget
-728.71
63
4/25/2025
3:03 PM
Japan
Widget
2,167.08
64
5/25/2025
4:04 PM
Italy
Doohickey
495.66
65
6/25/2025
5:05 PM
US
Gadget
4,307.53
66
7/25/2025
6:06 PM
Greece
Widget
-3,574.29
67
8/25/2025
7:07 PM
US
Doohickey
3,686.91
68
9/25/2025
8:08 PM
US
Doohickey
-228.07
69
10/25/2025
9:09 PM
Italy
Gadget
-625.86
70
11/25/2025
10:10 PM
UK
Gadget
-4,000.43
71
12/25/2025
11:11 PM
Japan
Doohickey
1,921.84
72
1/25/2025
12:12 AM
US
Widget
-262.76
73
2/25/2025
1:13 AM
Germany
Gadget
4,951.98
74
3/25/2025
2:14 AM
Germany
Doohickey
-1,058.00
75
4/25/2025
3:15 AM
Japan
Doohickey
-1,926.24
76
5/25/2025
4:16 AM
UK
Gadget
2,090.33
77
6/25/2025
5:17 AM
Germany
Gadget
-1,505.16
78
7/25/2025
6:18 AM
Greece
Doohickey
-3,663.22
79
8/25/2025
7:19 AM
Italy
Widget
1,729.71
80
9/25/2025
8:20 AM
Italy
Doohickey
-4,476.58
81
10/25/2025
9:21 AM
Japan
Widget
-4,485.46
82
11/25/2025
10:22 AM
Japan
Doohickey
-441.99
83
12/25/2025
11:23 AM
Italy
Widget
-4,524.64
84
1/25/2025
12:24 PM
Greece
Doohickey
-2,932.15
85
2/25/2025
1:25 PM
UK
Doohickey
-1,286.20
86
3/25/2025
2:26 PM
Italy
Widget
-299.93
87
4/25/2025
3:27 PM
Germany
Widget
396.75
88
5/25/2025
4:28 PM
Greece
Doohickey
-4,189.21
89
6/25/2025
5:29 PM
Japan
Doohickey
4,209.52
90
7/25/2025
6:30 PM
US
Gadget
3,639.01
91
8/25/2025
7:31 PM
Greece
Widget
-3,664.78
92
9/25/2025
8:32 PM
Japan
Doohickey
3,907.40
93
10/25/2025
9:33 PM
US
Gadget
4,923.48
94
11/25/2025
10:34 PM
Japan
Doohickey
-706.36
95
12/25/2025
11:35 PM
Italy
Gadget
2,292.36
96
1/25/2025
12:36 AM
Greece
Gadget
-3,869.26
97
2/25/2025
1:37 AM
US
Gadget
3,086.96
98
3/25/2025
2:38 AM
US
Widget
2,278.58
99
4/25/2025
3:39 AM
US
Doohickey
2,107.86
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// This file locates: "Scripts/Lesson/C1FlexGrid/CustomEditors.js".
c1.documentReady(function () {
    // create some random data
    var countries = 'US,Germany,UK,Japan,Italy,Greece'.split(',');
    var products = [
        { id: 0, name: 'Widget', unitPrice: 23.43 },
        { id: 1, name: 'Gadget', unitPrice: 12.33 },
        { id: 2, name: 'Doohickey', unitPrice: 53.07 },
    ];
    var data = [];
    var dt = new Date();
    for (var i = 0; i < 100; i++) {
        data.push({
            id: i,
            date: new Date(dt.getFullYear(), i % 12, 25, i % 24, i % 60, i % 60),
            time: new Date(dt.getFullYear(), i % 12, 25, i % 24, i % 60, i % 60),
            country: countries[Math.floor(Math.random() * countries.length)],
            product: products[Math.floor(Math.random() * products.length)].name,
            amount: Math.random() * 10000 - 5000,
            discount: Math.random() / 4
        });
    }
 
    var theGrid = wijmo.Control.getControl('#theGrid');
    theGrid.itemsSource = data;
 
    // add custom editors to the grid
    new CustomGridEditor(theGrid, 'date', wijmo.input.InputDate, {
        format: 'd'
    });
    new CustomGridEditor(theGrid, 'time', wijmo.input.InputTime, {
        format: 't',
        min: new Date(2000, 1, 1, 7, 0),
        max: new Date(2000, 1, 1, 22, 0),
        step: 30
    });
    new CustomGridEditor(theGrid, 'country', wijmo.input.ComboBox, {
        itemsSource: countries
    });
    new CustomGridEditor(theGrid, 'amount', wijmo.input.InputNumber, {
        format: 'n2',
        step: 10
    });
 
    // create an editor based on a ComboBox
    var multiColumnEditor = new CustomGridEditor(theGrid, 'product', wijmo.input.ComboBox, {
        headerPath: 'name',
        displayMemberPath: 'name',
        itemsSource: products
    });
 
    // customize the ComboBox to show multiple columns
    var combo = multiColumnEditor.control;
    combo.listBox.formatItem.addHandler(function (s, e) {
        e.item.innerHTML = '<table><tr>' +
            '<td style="width:30px;text-align:right;padding-right:6px">' + e.data.id + '</td>' +
            '<td style="width:100px;padding-right:6px"><b>' + e.data.name + '</b></td>' +
            '<td style="width:80px;text-align:right;padding-right:6px">' +
                wijmo.Globalize.format(e.data.unitPrice, 'c') +
            '</td>' +
        '</tr></table>';
    });
});
 
// *** CustomGridEditor class (transpiled from TypeScript) ***
var CustomGridEditor = (function () {
    /**
     * Initializes a new instance of a CustomGridEditor.
     */
    function CustomGridEditor(flex, binding, edtClass, options) {
        var _this = this;
        // save references
        this._grid = flex;
        this._col = flex.columns.getColumn(binding);
        // create editor
        this._ctl = new edtClass(document.createElement('div'), options);
        // connect grid events
        this._grid.beginningEdit.addHandler(this._beginningEdit, this);
        this._grid.scrollPositionChanged.addHandler(this._closeEditor, this);
        // connect editor events
        this._ctl.addEventListener(this._ctl.hostElement, 'keydown', function (e) {
            switch (e.keyCode) {
                case wijmo.Key.Tab:
                case wijmo.Key.Enter:
                    _this._closeEditor(true);
                    _this._grid.focus();
                    // forward event to the grid so it will move the selection
                    var evt = document.createEvent('HTMLEvents');
                    evt.initEvent('keydown', true, true);
                    evt['ctrlKey'] = e.ctrlKey;
                    evt['shiftKey'] = e.shiftKey;
                    evt['keyCode'] = e.keyCode;
                    _this._grid.hostElement.dispatchEvent(evt);
                    break;
                case wijmo.Key.Escape:
                    _this._closeEditor(false);
                    _this._grid.focus();
                    break;
            }
        });
        this._ctl.lostFocus.addHandler(function () {
            setTimeout(function () {
                if (!_this._ctl.containsFocus()) {
                    _this._closeEditor(true);
                }
            });
        });
        // open drop-down on f4/alt-down
        this._grid.addEventListener(this._grid.hostElement, 'keydown', function (e) {
            _this._openDropDown = false;
            if (e.keyCode == wijmo.Key.F4 ||
                (e.altKey && (e.keyCode == wijmo.Key.Down || e.keyCode == wijmo.Key.Up))) {
                _this._openDropDown = true;
                _this._grid.startEditing(true);
                e.preventDefault();
            }
        }, true);
        // close editor when user resizes the window
        window.addEventListener('resize', function () {
            if (_this._ctl.containsFocus()) {
                _this._closeEditor(true);
                _this._grid.focus();
            }
        });
    }
    Object.defineProperty(CustomGridEditor.prototype, "control", {
        // gets an instance of the control being hosted by this grid editor
        get: function () {
            return this._ctl;
        },
        enumerable: true,
        configurable: true
    });
    // handle the grid's beginningEdit event by canceling the built-in editor,
    // initializing the custom editor and giving it the focus.
    CustomGridEditor.prototype._beginningEdit = function (grid, args) {
        var _this = this;
        // check that this is our column
        if (grid.columns[args.col] != this._col) {
            return;
        }
        // check that this is not the Delete key
        // (which is used to clear cells and should not be messed with)
        var evt = args.data;
        if (evt && evt.keyCode == wijmo.Key.Delete) {
            return;
        }
        // cancel built-in editor
        args.cancel = true;
        // save cell being edited
        this._rng = args.range;
        // initialize editor host
        var rc = grid.getCellBoundingRect(args.row, args.col);
        wijmo.setCss(this._ctl.hostElement, {
            position: 'absolute',
            left: rc.left - 1 + pageXOffset,
            top: rc.top - 1 + pageYOffset,
            width: rc.width + 1,
            height: grid.rows[args.row].renderHeight + 1,
            borderRadius: '0px'
        });
        // initialize editor content
        if (!wijmo.isUndefined(this._ctl['text'])) {
            this._ctl['text'] = grid.getCellData(this._rng.row, this._rng.col, true);
        }
        else {
            throw 'Can\'t set editor value/text...';
        }
        // start editing item
        var ecv = wijmo.tryCast(grid.collectionView, 'IEditableCollectionView'), item = grid.rows[args.row].dataItem;
        if (ecv && item) {
            setTimeout(function () {
                ecv.editItem(item);
            }, 210); // FlexGrid commits edits 200ms after losing focus
        }
        // activate editor
        document.body.appendChild(this._ctl.hostElement);
        this._ctl.focus();
        setTimeout(function () {
            // get the key that triggered the editor
            var key = (evt && evt.charCode > 32)
                ? String.fromCharCode(evt.charCode)
                : null;
            // send key to editor
            if (key) {
                var input = _this._ctl.hostElement.querySelector('input');
                if (input instanceof HTMLInputElement) {
                    input.value = key;
                    wijmo.setSelectionRange(input, key.length, key.length);
                    var evtInput = document.createEvent('HTMLEvents');
                    evtInput.initEvent('input', true, false);
                    input.dispatchEvent(evtInput);
                }
            }
            else {
                _this._ctl.focus();
            }
            // open drop-down on F4/alt-down
            if (_this._openDropDown && _this._ctl instanceof wijmo.input.DropDown) {
                _this._ctl.isDroppedDown = true;
            }
        }, 50);
    };
    // close the custom editor, optionally saving the edits back to the grid
    CustomGridEditor.prototype._closeEditor = function (saveEdits) {
        if (this._rng) {
            var grid = this._grid, ctl = this._ctl, host = ctl.hostElement;
            // raise grid's cellEditEnding event
            var e = new wijmo.grid.CellEditEndingEventArgs(grid.cells, this._rng);
            grid.onCellEditEnding(e);
            // save editor value into grid
            if (saveEdits) {
                if (!wijmo.isUndefined(ctl['value'])) {
                    this._grid.setCellData(this._rng.row, this._rng.col, ctl['value']);
                }
                else if (!wijmo.isUndefined(ctl['text'])) {
                    this._grid.setCellData(this._rng.row, this._rng.col, ctl['text']);
                }
                else {
                    throw 'Can\'t get editor value/text...';
                }
                this._grid.invalidate();
            }
            // close editor and remove it from the DOM
            if (ctl instanceof wijmo.input.DropDown) {
                ctl.isDroppedDown = false;
            }
            host.parentElement.removeChild(host);
            this._rng = null;
            // raise grid's cellEditEnded event
            grid.onCellEditEnded(e);
        }
    };
    return CustomGridEditor;
}());
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// This file locates: "Content/css/Lesson/C1FlexGrid/CustomEditors.css".
.wj-flexgrid {
  max-height: 200px;
  margin-bottom: 12px;
}
 
.modal-content {
  width: 400px;
}
/* style our custom 'edit buttons' */
.wj-rowheaders .wj-cell .wj-glyph-pencil {
  opacity: .3;
  transform: scale(.5);
}
.wj-rowheaders .wj-cell:hover .wj-glyph-pencil {
  opacity: 1;
  color: #000080;
  transform: scale(1);
  transition: all linear 300ms;
}
 
/* wj-labeled-input: adapted from MDL styles */
 
/* label input container */
.wj-labeled-input {
    position: relative;
    font-size: 16px;
    display: inline-block;
    box-sizing: border-box;
    width: 300px;
    max-width: 100%;
    margin: 0 20px;
    padding: 20px 0;
}
 
    /* Wijmo control in the container */
    .wj-labeled-input .wj-control.wj-content {
        margin: 0;
        width: 100%;
        background-color: transparent;
        border: none;
        border-bottom: 1px solid rgba(0,0,0,.1);
    }
    .wj-labeled-input .wj-control.wj-content button {
        opacity: 0.75;
        border-color: rgba(0,0,0,.1);
    }
    .wj-labeled-input .wj-input-group .wj-form-control {
        float: none;
    }
 
    /* label in the container (must come after the control) */
    .wj-labeled-input label {
        font-size: 16px;
        top: 24px;
        bottom: 0;
        margin: 0;
        pointer-events: none;
        position: absolute;
        display: block;
        width: 100%;
        overflow: hidden;
        white-space: nowrap;
        text-align: left;
        color: rgba(0, 0, 0, 0.258824);
        transition-duration: .2s;
        transition-timing-function: cubic-bezier(.4,0,.2,1);
    }
     
    /* move label out of the way when control is focused or not empty */
    .wj-static-labels .wj-labeled-input :not(.wj-state-focused) + label,
    .wj-labeled-input .wj-state-focused + label,
    .wj-labeled-input :not(.wj-state-empty) + label {
        font-size: 12px;
        top: 4px;
        color: rgb(63,81,181);
        visibility: visible;
    }
    .wj-labeled-input .wj-state-invalid + label {
        color: #d50000;
    }
 
    /* underline label */
    .wj-labeled-input label:after {
        background-color: rgb(63,81,181);
        bottom: 20px;
        content: '';
        height: 2px;
        left: 45%;
        position: absolute;
        transition-duration: .2s;
        transition-timing-function: cubic-bezier(.4,0,.2,1);
        visibility: hidden;
        width: 10px;
    }
 
    /* show underline when focused */
    .wj-labeled-input .wj-state-focused + label:after {
        left: 0;
        visibility: visible;
        width: 100%;
    }
    .wj-labeled-input .wj-state-invalid + label:after {
        background-color: #d50000;
    }
 
    /* validation message */
    .wj-labeled-input .wj-error {
        color: #d50000;
        position: absolute;
        font-size: 12px;
        margin-top: 3px;
        visibility: hidden;
        display: block;
    }
    .wj-labeled-input .wj-control.wj-state-invalid ~ .wj-error {
        visibility: visible;
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
using System.Web.Mvc;
 
namespace LearnMvcClient.Controllers
{
    public partial class C1FlexGridController : Controller
    {
        // GET: CustomEditors
        public ActionResult CustomEditors()
        {
            return View();
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<h1>
    @Html.Raw(Resources.C1FlexGrid.CustomEditors_Title)
</h1>
<p>
    @Html.Raw(Resources.C1FlexGrid.CustomEditors_Text1)
</p>
<p>
    @Html.Raw(Resources.C1FlexGrid.CustomEditors_Text2)
</p>
@(Html.C1().FlexGrid().Id("theGrid")
    .AutoGenerateColumns(false)
    .Columns(cs=>
    {
        cs.Add().Binding("id").Header("ID").Width("40").IsReadOnly(true);
        cs.Add().Binding("date").Header("Date").Format("d");
        cs.Add().Binding("time").Header("Time").Format("t");
        cs.Add().Binding("country").Header("Country");
        cs.Add().Binding("product").Header("Product");
        cs.Add().Binding("amount").Header("Amount").Format("n2");
    })
)