Getting started in EJ2 TypeScript In place editor control
3 Nov 202317 minutes to read
This section briefly explains about how to create a simple In-place Editor using TypeScript and configure its properties using the Essential JS 2 quickstart seed repository.
This application is integrated with the
webpack.config.js
configuration and uses the latest version of the webpack-cli. It requires nodev14.15.0
or higher. For more information about webpack and its features, refer to the webpack documentation.
Dependencies
The following is the list of dependencies required to use the In-place Editor control in your application.
|-- @syncfusion/ej2-inplace-editor
|-- @syncfusion/ej2-base
|-- @syncfusion/ej2-data
|-- @syncfusion/ej2-inputs
|-- @syncfusion/ej2-popups
|-- @syncfusion/ej2-buttons
|-- @syncfusion/ej2-dropdowns
|-- @syncfusion/ej2-calendars
|-- @syncfusion/ej2-richtexteditor
Set up development environment
Open the command prompt from the required directory, and run the following command to clone the Syncfusion JavaScript (Essential JS 2) quickstart project from GitHub.
git clone https://github.com/SyncfusionExamples/ej2-quickstart-webpack- ej2-quickstart
After cloning the application in the ej2-quickstart
folder, run the following command line to navigate to the ej2-quickstart
folder.
cd ej2-quickstart
Add Syncfusion JavaScript packages
Syncfusion JavaScript (Essential JS 2) packages are available on the npmjs.com public registry. You can install all Syncfusion JavaScript (Essential JS 2) controls in a single @syncfusion/ej2 package or individual packages for each control.
The quickstart application is preconfigured with the dependent @syncfusion/ej2 package in the ~/package.json
file. Use the following command to install the dependent npm packages from the command prompt.
npm install
Import the Syncfusion CSS styles
In-place Editor CSS files are available in the ej2-inplace-editor and its sub-control package folder. This can be referenced in the ~/src/styles/styles.css
file of your application using the following code.
@import '../../node_modules/@syncfusion/ej2-base/styles/material.css';
@import '../../node_modules/@syncfusion/ej2-lists/styles/material.css';
@import '../../node_modules/@syncfusion/ej2-inputs/styles/material.css';
@import '../../node_modules/@syncfusion/ej2-popups/styles/material.css';
@import '../../node_modules/@syncfusion/ej2-buttons/styles/material.css';
@import '../../node_modules/@syncfusion/ej2-dropdowns/styles/material.css';
@import '../../node_modules/@syncfusion/ej2-calendars/styles/material.css';
@import '../../node_modules/@syncfusion/ej2-navigations/styles/material.css';
@import '../../node_modules/@syncfusion/ej2-splitbuttons/styles/material.css';
@import '../../node_modules/@syncfusion/ej2-richtexteditor/styles/material.css';
@import '../../node_modules/@syncfusion/ej2-inplace-editor/styles/material.css';
- If you want to use the combined CSS files of entire controls, you can avail it from the root folder of Essential JS 2 package and reference it with the following code.
@import '../../node_modules/@syncfusion/ej2/material.css';
Add the In-place Editor with Textbox
By default, the Essential JS 2 TextBox control is rendered in In-place Editor with the type
property sets as Text.
- Add the HTML div tag with its ID attribute as
element
in yourindex.html
file to initialize the In-place Editor.
[src/index.html]
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 In-place Editor</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
<meta name="description" content="Essential JS 2" />
<meta name="author" content="Syncfusion" />
<link rel="shortcut icon" href="resources/favicon.ico" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div style="margin: 50px;">
<!--element which is going to render-->
<div id="element">
</div>
</body>
</html>
- Import the In-place Editor control to your
app.ts
file and initialize it to the#element
as shown as follows.
[src/app/app.ts]
import { InPlaceEditor } from '@syncfusion/ej2-inplace-editor';
let editObj: InPlaceEditor = new InPlaceEditor({
type: 'Text',
value: 'Andrew',
model: {
placeholder: 'Enter employee name'
}
});
editObj.appendTo('#element');
In the above sample code,
#element
is theid
of the HTML element in a page to which the In-place Editor is initialized.
Configuring DropDownList
You can render the Essential JS 2 DropDownList by changing the type
property as DropDownList
and configure its properties and methods using the model
property.
In the following sample, type
and model values are configured to render the DropDownList
control.
[src/app/app.ts]
import { InPlaceEditor } from '@syncfusion/ej2-inplace-editor';
let genderData = ['Male', 'Female']
let editObj: InPlaceEditor = new InPlaceEditor({
type: 'DropDownList',
mode: 'Inline',
model: {
dataSource: genderData,
placeholder: 'Select gender'
}
});
editObj.appendTo('#element');
Integrate DatePicker
You can render the Essential JS2 DatePicker by changing the type
property as Date
and also configure its properties and methods using the model
property.
In the following sample, type
and model
values are configured to render the DatePicker control.
[src/app/app.ts]
import { InPlaceEditor } from '@syncfusion/ej2-inplace-editor';
let editObj: InPlaceEditor = new InPlaceEditor({
type: 'Date',
value: new Date('04/12/2018'),
model: {
showTodayButton: true
}
});
editObj.appendTo('#element');
Run the application
- Run the application in the browser using the following command.
npm start
import { InPlaceEditor } from '@syncfusion/ej2-inplace-editor';
let genderData = ['Male', 'Female'];
let editObj: InPlaceEditor = new InPlaceEditor({
type: 'Date',
mode: 'Inline',
value: new Date('04/12/2018'),
model: {
showTodayButton: true,
placeholder: 'Select date of birth'
}
});
editObj.appendTo('#dateofbirth');
let editObj: InPlaceEditor = new InPlaceEditor({
value: 'Andrew',
mode: 'Inline',
model: {
placeholder: 'Enter your name'
}
});
editObj.appendTo('#element');
let editObj: InPlaceEditor = new InPlaceEditor({
type: 'DropDownList',
value: 'Male',
mode: 'Inline',
model: {
dataSource: genderData,
placeholder: 'Select gender'
}
});
editObj.appendTo('#gender');
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 In-place Editor</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript In-place Editor Control" />
<meta name="author" content="Syncfusion" />
<link href="https://cdn.syncfusion.com/ej2/27.1.48/material.css" rel="stylesheet" />
<link href="index.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>LOADING....</div>
<div id='container'>
<div class="control-group">
<h3> Modify Basic Details </h3>
<table>
<tr>
<td>Name</td>
<td class='left'>
<div id='element'></div>
</td>
</tr>
<tr>
<td>Date of Birth</td>
<td class='left'>
<div id='dateofbirth'></div>
</td>
</tr>
<tr>
<td>Gender</td>
<td class='left'>
<div id='gender'></div>
</td>
</tr>
</table>
</div>
</div>
</div>
</body>
</html>
Submitting data to the server (save)
You can submit editor value to the server by configuring the url
, adaptor
and primaryKey
property.
Property | Usage |
---|---|
url |
Gets the URL for server submit action. |
adaptor |
Specifies the adaptor type that is used by DataManager to communicate with DataSource. |
primaryKey |
Defines the unique primary key of the editable field which can be used for saving data in the data-base. |
The
primaryKey
property is mandatory. If it’s not set, edited data are not sent to the server.
Refresh with modified value
The edited data is submitted to the server and you can see the new values getting reflected in the In-place Editor.
import { InPlaceEditor, ActionEventArgs, MultiSelect } from '@syncfusion/ej2-inplace-editor';
InPlaceEditor.Inject(MultiSelect);
let serviceUrl: string = 'https://ej2services.syncfusion.com/development/web-services/api/Editor/UpdateData';
let frameWorkList: string[] = ['Andrew Fuller', 'Janet Leverling', 'Laura Callahan', 'Margaret Hamilt', 'Michael Suyama', 'Nancy Davloio', 'Robert King'];
let newEle: HTMLElement = document.getElementById('newValue');
let oldEle: HTMLElement = document.getElementById('oldValue');
let editObj: InPlaceEditor = new InPlaceEditor({
mode: 'Inline',
type: 'DropDownList',
value: 'Andrew Fuller',
name: 'Employee',
url: serviceUrl,
primaryKey: "Employee",
adaptor: 'UrlAdaptor',
actionSuccess: function(e) {
oldEle.textContent = newEle.textContent;
newEle.textContent = e.value;
},
model: {
dataSource: frameWorkList,
popupHeight: '200px',
placeholder: 'Select employee'
}
});
editObj.appendTo('#element');
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 In-place Editor</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript In-place Editor Control" />
<meta name="author" content="Syncfusion" />
<link href="https://cdn.syncfusion.com/ej2/27.1.48/material.css" rel="stylesheet" />
<link href="index.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>LOADING....</div>
<div id='container'>
<div class="control-group">
Best Employee of the year: <div id='element'></div>
</div>
<table style="margin:auto;width:50%">
<tr>
<td style="text-align: left">
Old Value :
</td>
<td id="oldValue" style="text-align: left">
</td>
</tr>
<tr>
<td style="text-align: left">
New Value :
</td>
<td id="newValue" style="text-align: left">
Andrew Fuller
</td>
</tr>
</table>
</div>
</body>
</html>