Template editing in JavaScript Grid control
18 Nov 20187 minutes to read
Inline or dialog template editing
The dialog/inline template editing provides an option to customize the default behavior of dialog editing. Using the dialog template, you can render your own editors by defining the editSettings.mode as Dialog/Inline and editSetting.template as SCRIPT element ID or HTML string which holds the template.
In some cases, you need to add the new field editors in the dialog which are not present in the column model. In that situation, the dialog template will help you to customize the default edit dialog.
In the following sample, grid enabled with dialog template editing.
The template form editors should have name attribute.
Template context
While using the edit template, you can access the row information inside the Template and you can bind the attributes or values or elements based on this row information.
The following properties will be available at the time of template execution.
| Property Name | Usage |
|---|---|
| isAdd | A Boolean property; it defines whether the current row should be a new record or not. |
In the following code example, the OrderID textbox has been disabled by using the isAdd property.
// The disabled attributes will be added based on the isAdd property.
<input id="OrderID" name="OrderID" type="text" value=${if(isAdd)} '' ${else} ${OrderID} ${/if} ${if(isAdd)}'' ${else} disabled ${/if}/>
The following code example illustrates rendering the OrderID textbox, when a new record is added.
${if(isAdd)}
<div class="form-group col-md-6">
<div class="e-float-input e-control-wrapper">
<input id="OrderID" name="OrderID" type="text" value=${if(isAdd)} '' ${else} ${OrderID} ${/if} />
<span class="e-float-line"></span>
<label class="e-float-text e-label-top" for="OrderID">Order ID</label>
</div>
</div>
${/if}
Render editors as controls
You can convert the form editors to EJ2 controls in the actionComplete event based on the requestType as beginEdit or add.
The following code example illustrates rendering the drop-down list control in the actionComplete event.
Get value from editor
You can read, format, and update the current editor value in the actionBegin event at the time of setting requestType to save.
In the following code example, the freight value has been formatted and updated.
Set focus to editor
By default, the first input element in the dialog will be focused while opening the dialog. If the first input element is in disabled or hidden state, focus the valid input element in the actionComplete event based on requestType as beginEdit.
Disable form validation
If you need to disable the default validation rules in the actionComplete event.
Adding validation rules for custom editors
If you have interested to use our default form validation, the validation rules for the fields which are not present in the column model need to be add in the actionComplete event.
Render tab control inside the dialog template
You can use Tab control inside dialog edit UI using dialog template feature. The dialog template feature can be enabled by defining editSettings.mode as Dialog and editSettingsTemplate as template variable to define the editors.
To include tab controls in the Dialog, please ensure the following steps:
Step 1: To render the Tab control, use the editSettingsTemplate of the Grid. Inside the content template of the tab items define the input elements.
<div>
<div id="edittab"></div>
<div id="tab1">
<div class="form-row" >
<div class="form-group col-md-6">
<div class="e-float-input e-control-wrapper">
<input id="OrderID" name="OrderID" type="text" value=${if(isAdd)} '' ${else} ${OrderID} ${/if} ${if(isAdd)} '' ${else} disabled ${/if} />
<span class="e-float-line"></span>
<label class="e-float-text e-label-top" for="OrderID">Order ID</label>
</div>
</div>
</div>
<div class="form-row" >
<div class="form-group col-md-6">
<div class="e-float-input e-control-wrapper">
<input id="CustomerID" name="CustomerID" type="text" value=${if(isAdd)} '' ${else} ${CustomerID} ${/if} />
<span class="e-float-line"></span>
<label class="e-float-text e-label-top" for="CustomerID">Customer ID</label>
</div>
</div>
</div>
<button id='next' class='e-info e-btn' style="float: right" type='button'> next</button>
</div>
<div id="tab2" style='display: none'>
<div class="form-row" >
<div class="form-group col-md-6">
<input type="text" name="ShipCountry" id="ShipCountry" value=${if(isAdd)} '' ${else} ${ShipCountry} ${/if} />
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<input type="checkbox" name="Verified" id="Verified" ${if(isAdd)} '' ${else} checked ${/if} />
</div>
</div>
<button id='submit' class='e-info e-btn' style="float: right" type='button'> submit</button>
</div>
</div>
Step 2:
To render the tab control, use the actionComplete event of the grid.
The following example, we have rendered tab control inside the edit dialog. The tab control has two tabs and once you fill the first tab and navigate to second one. The validation for first tab was done before navigate to second.