Although the FlexGrid control provides efficient, Excel-style editing by default, you may want to customize the grid's editing behavior. If you don't like the Excel-style editing and prefer to add editing buttons to the rows of your FlexGrid, you can accomplish that using the formatItem event and a little bit of code.
In this blog, we will outline how to perform inline editing with the FlexGrid control. We will create buttons that users can use to edit and delete rows inside the FlexGrid and insert input elements in the grid when the user begins editing the data.
Wijmo offers the fastest, most flexible JavaScript DataGrid with features including sorting, grouping, searching, Excel-like filtering, DataMaps, custom CellTemplates, sparklines, rich editing, Excel/PDF export, validation, DetailsRows, and more.
How to Import the Required Modules for FlexGrid
Before we can create our FlexGrid control, we’ll need to import the required Wijmo files into our JavaScript file:
The above code will import Wijmo's main JavaScript module and the FlexGrid module; it also includes Wijmo's CSS file so that our control is styled correctly.
Creating the FlexGrid Control
First, we’ll need to create our FlexGrid control. We’ll be tying two events to our FlexGrid control as well: the formatItem event and the click event.
We'll come back to these events later in the blog. Before that, we'll need to create our buttons that these events will use.
Adding Inline Editing Buttons
Next, we’ll need to create the button templates that our grid will use for inline editing. Both sets of buttons will be set to “display:none”, because these are just templates that we will be using in JavaScript, so we don’t want them to show up on-screen before then:
Implementing Inline Editing
Now that our FlexGrid has been created and we have our button templates, we can add code to the events that we have tied to the grid to implement inline editing. The formatItem event will handle what is being displayed in the FlexGrid, while the ‘click’ event listener will handle the buttons’ click events:
You’ll notice a couple of custom functions that we call inside of our click event: editItem(), commitEdit(), and cancelEdit(). These are called based on which button the user clicks, and will update the FlexGrid accordingly based on the user’s selection.
There's one final thing that we need to do. The buttons and input controls that we place inside the FlexGrid control are slightly taller than the cells they will be placed in. This means that we'll need to increase the height of our rows:
And that’s it! Your FlexGrid now supports inline editing through the use of buttons placed in the grid.
You can check out this sample live here.