Search results

Get selected items from ListView

Single or many items can be selected by users in the ListView component. An API is used to get selected items from the list items. This is called as the getSelectedItems method.

getSelectedItems method

This is used to get the details of the currently selected item from the list items. It returns the SelectedItem | SelectedCollection

The getSelectedItems method returns the following items from the selected list items.

Return type Purpose
text Returns the text of selected item lists
data Returns the whole data of selected list items, i.e., returns the fields data of selected li.
item Returns the collections of list items
razor
list.cs
@using Syncfusion.EJ2
@using Syncfusion.EJ2.Lists

    @Html.EJS().ListView("element").DataSource((IEnumerable<object>)ViewBag.dataSource).Fields(new ListViewFieldSettings { Text = "text", Id="id"}).ShowCheckBox(true).Render()
    <div id="text">
        @Html.EJS().Button("btn").Content("Get selected Items").Render()
        <table id="val"></table>
    </div>



<style>
    #text {
        margin-left: 10px;
        margin-top: 20px;
    }

    #element {
        max-width: 350px;
        margin: auto;
        margin-top: 10px;
        display: block;
        border: 1px solid #dddddd;
        border-radius: 3px;
    }
</style>
<script>

document.getElementById('btn').addEventListener('click', () => {
    var selecteditem = document.getElementById('element').ej2_instances[0].getSelectedItems();
    var data = document.getElementById('val');
    data.innerHTML = "";
    var row1 = document.createElement('tr');
    var header1 = document.createElement('th');
    header1.innerHTML = 'Text';
    row1.appendChild(header1);
    var header2 = document.createElement('th');
    header2.innerHTML = 'Id';
    row1.appendChild(header2);
    document.getElementById('val').appendChild(row1);
    for (var i= 0; i < (selecteditem["data"]).length; i++) {
        var row2 = document.createElement('tr');
        row2.setAttribute("id", i.toString());
        var data1 = document.createElement('td');
        data1.innerHTML = selecteditem["text"][i];
        row2.appendChild(data1);
        var data2 = document.createElement('td');
        data2.innerHTML = (selecteditem["data"])[i].id.toString();
        row2.appendChild(data2);
        document.getElementById('val').appendChild(row2);
    }

});
</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 = "Hennessey Venom",
                id = "list-01"

            }); listdata.Add(new
            {
                text = "Bugatti Chiron",
                id = "list-02"

            }); listdata.Add(new
            {
                text = "Bugatti Veyron Super Sport",
                id = "list-03"

            }); listdata.Add(new
            {
                text = "SSC Ultimate Aero",
                id = "list-04"

            }); listdata.Add(new
            {
                text = "Koenigsegg CCR",
                id = "list-05"

            }); listdata.Add(new
            {
                text = "McLaren F1",
                id = "list-06"
            }); listdata.Add(new
            {
                text = "Aston Martin One- 77",
                id = "list-07"
            }); listdata.Add(new
            {
                text = "Jaguar XJ220",
                id = "list-08"
            });
            ViewBag.dataSource = listdata;
            return View();
        }       
    }
}