Search results

Customize ListView as Chat Window

ListView can be customized as chat window. To achieve that, use the ListView template property and Avatar component.

* The Listview template property is used to showcase the ListView as chat window.
* Avatar component is used to design the image of contact person.

Refer the below template code snippet for Template of chat window.

var template = "<div class='settings'>" +
                "${if(chat!=='receiver')}" +
                "<div id='content'>" +
                "<div class='name'>${text}</div>" +
                "<div id='info'>${contact}</div></div>" +
                "${if(avatar!=='')}" +
                "<div id='image'><span class='e-avatar img1 e-avatar-circle'>${avatar}</span></div>" +
                "${else}" +
                "<div id='image'><span class='${pic} img1 e-avatar e-avatar-circle'> </span></div>" +
                "${/if}" +
                "${else}" +
            "${if(avatar!=='')}" +
                "<div id='image2'><span class='e-avatar img2 e-avatar-circle'>${avatar}</span></div>" +
                "${else}" +
                "<div id='image2'><span class='${pic} img2 e-avatar e-avatar-circle'> </span></div>" +
                "${/if}" +
                "<div id='content1'>" +
                "<div class='name1'>${text}</div>" +
                "<div id='info1'>${contact}</div>" +
                "</div>" +
                "${/if}" +
                "</div>";

Chat order in template

In ListView template, we have rendered the list items based on receiver and sender information from dataSource of listview.

Adding messages to chat window

* Use textbox to get message from user.
* Add the textbox message to ListView dataSource using [addItem](https://ej2.syncfusion.com/documentation/api/list-view/#additem) method.
document.getElementById('btn').addEventListener('click', (e) => {
    var value = document.getElementById('name').value;
     document.getElementById('List').ej2_instances[0].addItem([{ text: "Amenda", contact: value, id: "2", avatar: "A", pic: "", chat: "receiver" }]);
  });
razor
list.cs
@{
    var template = "<div class='settings'>" +
                     "${if(chat!=='receiver')}" +
                      "<div id='content'>" +
                      "<div class='name'>${text}</div>" +
                      "<div id='info'>${contact}</div></div>" +
                     "${if(avatar!=='')}" +
                      "<div id='image'><span class='e-avatar img1 e-avatar-circle'>${avatar}</span></div>" +
                      "${else}" +
                      "<div id='image'><span class='${pic} img1 e-avatar e-avatar-circle'> </span></div>" +
                      "${/if}" +
                     "${else}" +
                    "${if(avatar!=='')}" +
                      "<div id='image2'><span class='e-avatar img2 e-avatar-circle'>${avatar}</span></div>" +
                      "${else}" +
                      "<div id='image2'><span class='${pic} img2 e-avatar e-avatar-circle'> </span></div>" +
                      "${/if}" +
                      "<div id='content1'>" +
                      "<div class='name1'>${text}</div>" +
                      "<div id='info1'>${contact}</div>" +
                      "</div>" +
                     "${/if}" +
                      "</div>";
}
@using Syncfusion.EJ2
@using Syncfusion.EJ2.Lists;
@Html.EJS().ListView("List").DataSource((IEnumerable<object>)ViewBag.dataSource).Fields(new ListViewFieldSettings { Text = "text" }).Template(template).HeaderTitle("Chat").Width("350px").ShowHeader(true).Render()
<div style="width: 350px;margin: 0 auto;">
<input id="name" style="width: 275px" class="e-input" type="text" placeholder="Type your message" />
@Html.EJS().Button("btn").Content("Send").Render()

</div>
<style>
    #btn {
        float: right;
    }

    #List {
        margin: 0 auto;
        border: 1px solid #ccc;
    }

    #List .e-list-item {
        height: auto;
        cursor: pointer;
    }

    #List .e-list-header .e-text {
        font-family: sans-serif;
        font-size: 18px;
        line-height: 26px;
    }

    #List #info,
    #List .name {
        font-size: 11px;
        line-height: 20px;
    }

    #List .name {
        padding-top: 3px;
        font-weight: 500;
        padding-left: 150px;
    }

    #List #info {
        float: right;
        margin-right: 10px;
    }

    .pic01 {
        background-image: url("https://ej2.syncfusion.com/demos/src/grid/images/1.png");
    }

    .pic02 {
        background-image: url("https://ej2.syncfusion.com/demos/src/grid/images/3.png");
    }

    .pic03 {
        background-image: url("https://ej2.syncfusion.com/demos/src/grid/images/5.png");
    }

    .pic04 {
        background-image: url("https://ej2.syncfusion.com/demos/src/grid/images/2.png");
    }

    .img2.e-avatar {
        margin-left: 10px;
        margin-top: 2px !important;
        font-size: 13px;
    }

    #List #content1 {
        width: 200px;
        background-color: aliceblue;
        display: inline-block;
        margin: 5px;
    }

    #List #info1,
    #List .name1 {
        font-size: 11px;
        line-height: 20px;
        margin-left: 10px;
    }

    #List .name1 {
        padding-top: 3px;
        font-weight: 500;
    }

    #List #content {
        margin: 5px;
        width: 200px;
        margin-left: 90px;
        background-color: aliceblue;
        display: inline-block;
    }

    #image {
        float: right;
        display: inline-block;
    }

    #image2 {
        float: left;
        display: inline-block;
    }

    .img1.e-avatar {
        margin-right: 10px;
        margin: 5px;
        font-size: 13px;
    }

    .e-listview .e-list-item {
        padding: 0px !important;
    }

    .e-listview .e-list-header {
        color: white !important;
    }

    .e-listview .e-list-header {
        background: rgb(2, 120, 215) !important;
    }

    #List.e-listview .e-list-item.e-hover {
        background-color: transparent;
    }
</style>
<script>
    document.getElementById('btn').addEventListener('click', (e) => {
        let value = document.getElementById('name').value;
        document.getElementById('List').ej2_instances[0].addItem([{ text: "Amenda", contact: value, id: "2", avatar: "A", pic: "", chat: "receiver" }]);
        document.getElementById('name').value = "";
    });
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace WebApplication1.Controllers
{
    public class ListViewController : Controller
    {
        public IActionResult list()
        {   
            List<object> listData = new List<object>();
            listData.Add(new
            {
                text = "Jenifer",
                contact = "Hi",
                id = "1",
                avatar = "",
                pic = "pic01",
                chat = "sender"
            });
            listData.Add(new { text = "Amenda", contact = "Hello", id = "2", avatar = "A", pic = "", chat = "receiver" });
            listData.Add(new
            {
                text = "Jenifer",
                contact = "What Knid of application going to launch",
                id = "4",
                avatar = "",
                pic = "pic02",
                chat = "sender"
            });
            listData.Add(new
            {
                text = "Amenda ",
                contact = "A knid of Emergency broadcast App",
                id = "5",
                avatar = "A",
                pic = "",
                chat = "receiver"
            });
            listData.Add(new
            {
                text = "Jacob",
                contact = "Can you please elaborate",
                id = "6",
                avatar = "",
                pic = "pic04",
                chat = "sender"
            });
            ViewBag.dataSource = listData;
            return View();
        }       
    }
}