Contents
- Adding message
- Edit messages
- Scroll to Bottom
Having trouble getting help?
Contact Support
Contact Support
Methods in EJ2 JavaScript Chat UI control
17 Dec 202416 minutes to read
Adding message
You can use the addMessage public method to add the messages in the Chat UI. You can add it either as a string
or MessageModel
collection. It programmatically adds a new message to the chat.
The below sample demonstrates adding a new message as string and as a MessageModel
object.
Here is an example of how to use the addMessage method:
ej.base.enableRipple(true);
let currentUserModel = {
id: "user1",
user: "Albert"
};
let michaleUserModel = {
id: "user2",
user: "Michale Suyama"
};
let chatMessages = [
{
author: currentUserModel,
text: "Hi Michale, are we on track for the deadline?"
},
{
author: michaleUserModel,
text: "Yes, the design phase is complete."
},
{
author: currentUserModel,
text: "I’ll review it and send feedback by today."
}
];
// Initializes the Chat UI control
let chatUI = new ej.interactivechat.ChatUI({
messages: chatMessages,
user: currentUserModel,
height: '360px'
});
// Render initialized Chat UI.
chatUI.appendTo('#message');
document.addEventListener('click', function (event) {
if (event.target && event.target.id === 'addString') {
chatUI.addMessage('Also, let me know if there are any blockers we should address before the next phase.');
}
});
<!DOCTYPE html>
<html lang="en">
<head>
<title>EJ2 Chat UI</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="TypeScript Chat UI Control" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-inputs/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-navigations/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-interactive-chat/styles/material.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="https://cdn.syncfusion.com/ej2/29.2.4/dist/ej2.min.js" type="text/javascript"></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' style="height: 380px; width: 450px;">
<button id="addString" style="margin-bottom: 10px" class="e-btn e-primary">Add Message as string</button>
<div id="message"></div>
</div>
</body>
</html>
ej.base.enableRipple(true);
let currentUserModel = {
id: "user1",
user: "Albert"
};
let michaleUserModel = {
id: "user2",
user: "Michale Suyama"
};
let chatMessages = [
{
author: currentUserModel,
text: "Hi Michale, are we on track for the deadline?"
},
{
author: michaleUserModel,
text: "Yes, the design phase is complete."
},
{
author: currentUserModel,
text: "I’ll review it and send feedback by today."
}
];
// Initializes the Chat UI control
let chatUI = new ej.interactivechat.ChatUI({
messages: chatMessages,
user: currentUserModel,
height: '360px'
});
// Render initialized Chat UI.
chatUI.appendTo('#messageModel');
document.addEventListener('click', function (event) {
if (event.target && event.target.id === 'addMessageModel') {
chatUI.addMessage(
{
author: michaleUserModel,
text: "Great! Let me know if there’s anything that needs adjustment."
}
);
}
});
<!DOCTYPE html>
<html lang="en">
<head>
<title>EJ2 Chat UI</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="TypeScript Chat UI Control" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-inputs/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-navigations/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-interactive-chat/styles/material.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="https://cdn.syncfusion.com/ej2/29.2.4/dist/ej2.min.js" type="text/javascript"></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' style="height: 380px; width: 450px;">
<button id="addMessageModel" style="margin-bottom: 10px" class="e-btn e-primary">Add Message as Object</button>
<div id="messageModel"></div>
</div>
</body>
</html>
Edit messages
You can use the updateMessage public method to update the messages in the ChatUI to modify an existing message within the chat, useful for editing or correcting sent messages.
ej.base.enableRipple(true);
let currentUserModel = {
id: "user1",
user: "Albert"
};
let michaleUserModel = {
id: "user2",
user: "Michale Suyama"
};
let chatMessages = [
{
id: "msg1",
author: currentUserModel,
text: "Hi Michale, are we on track for the deadline?"
},
{
id: "msg2",
author: michaleUserModel,
text: "Yes, the design phase is complete."
},
{
id: "msg3",
author: currentUserModel,
text: "I’ll review it and send feedback by today."
}
];
// Initializes the Chat UI control
let chatUI = new ej.interactivechat.ChatUI({
messages: chatMessages,
user: currentUserModel,
height: '360px'
});
// Render initialized Chat UI.
chatUI.appendTo('#edit-message');
document.addEventListener('click', function (event) {
if (event.target && event.target.id === 'updateMessage') {
chatUI.updateMessage({text: "Hi Michael, are we still on schedule to meet the deadline?", author: currentUserModel},'msg1');
}
});
<!DOCTYPE html>
<html lang="en">
<head>
<title>EJ2 Chat UI</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="TypeScript Chat UI Control" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-inputs/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-navigations/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-interactive-chat/styles/material.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="https://cdn.syncfusion.com/ej2/29.2.4/dist/ej2.min.js" type="text/javascript"></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' style="height: 380px; width: 450px;">
<button id="updateMessage" style="margin-bottom: 10px" class="e-btn e-primary">Update Message</button>
<div id="edit-message"></div>
</div>
</body>
</html>
Scroll to Bottom
You can use the scrollToBottom public method to scroll the chat view to the latest message, ensuring users see the new content updated.
ej.base.enableRipple(true);
let currentUserModel = {
id: "user1",
user: "Albert"
};
let michaleUserModel = {
id: "user2",
user: "Michale Suyama"
};
let chatMessages = [
{
author: currentUserModel,
text: "Want to get coffee tomorrow?"
},
{
author: michaleUserModel,
text: "Sure! What time?"
},
{
author: currentUserModel,
text: "How about 10 AM?"
},
{
id: "msg4",
author: michaleUserModel,
text: "Perfect"
},
{
id: "msg5",
author: currentUserModel,
text: "See you!"
},
{
id: "msg6",
author: michaleUserModel,
text: "Bye!"
}
];
// Initializes the Chat UI control
let chatUI = new ej.interactivechat.ChatUI({
messages: chatMessages,
user: currentUserModel,
height: '360px'
});
// Render initialized Chat UI.
chatUI.appendTo('#chat-scroll');
document.addEventListener('click', function (event) {
if (event.target && event.target.id === 'scrollToBottom') {
chatUI.scrollToBottom();
}
});
<!DOCTYPE html>
<html lang="en">
<head>
<title>EJ2 Chat UI</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="TypeScript Chat UI Control" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-inputs/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-navigations/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-interactive-chat/styles/material.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="https://cdn.syncfusion.com/ej2/29.2.4/dist/ej2.min.js" type="text/javascript"></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' style="height: 380px; width: 450px;">
<button id="scrollToBottom" style="margin-bottom: 10px" class="e-btn e-primary">Scroll to bottom</button>
<div id="chat-scroll"></div>
</div>
</body>
</html>