This is a migrated thread and some comments may be shown as answers.

ListView can't get current selected item

3 Answers 708 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Wade
Top achievements
Rank 1
Wade asked on 13 Apr 2021, 03:41 AM

can someone help please
my platform is asp.net mvc 5 using razor pages in vs 2019 using telerik latest controls.
in my patient page I have a kendo listivew inside of a kendo TileLayout.
datasource is a list of patient name populating the listivew 
listView generate the listing of items just fine.
however, when I select on the listivew i can not get the current selected item number, 

which will generate an action
no index number, no way to find where to get the selected item.

 

What Am I Doing Wrong.

// index.cshtml

@model ap_GetMembershipListAllRow

<script type="text/x-kendo-template" id="template">
    <div class="patient-view k-widget">             
        <div class="patientidtemp">#:Patient_ID#</div>
        <div class="fullnametemp">#:LastName#, #:FirstName# #:MiddleName#</h1>
        <div class="bdatetemp">Date Begin: #:kendo.toString(BeginDate,"MMM/dd/yyyy")#</div>       
    </div>
</script>

<script type="text/x-kendo-template" id="template2">
    <div class="patient-view k-widget">             
        <div class="patientidtemp1">#:Patient_ID#</div>
        <div class="fullnametemp1">#:LastName#, #:FirstName# #:MiddleName#</h1>
       <div class="bdatetemp1">Date Begin: #:kendo.toString(BeginDate,"MMM/dd/yyyy")#</div>
    </div>
</script>

<script type="text/x-kendo-template" id="patient-list">
    @(Html.Kendo().ListView<ListViewModel>()
        .Name("PatientListView")
        .TagName("div")
        .ClientTemplateId("template")
        .ClientAltTemplateId("template2")       
        .DataSource(dSource => dSource           
           .Ajax()          
           .Read(read => read.Action("PatientList_Read", "PatientPortal").Type(HttpVerbs.Get))
           .PageSize(11)             
          .Model(model => model.Id("Patient_ID"))          
        )
        .Pageable()
        .Selectable(selectable => selectable.Mode(ListViewSelectionMode.Single))
        .Events(events => events.Change("onChange"))
        .ToClientTemplate()
    )    
</script>


//patientportal.js
// nothing works
function onChange(e) {


    var ds = $("#PatientListView").data("kendoListView");
    var index = ds.select().index(),
        dataItem = ds.dataSource.view()[index];

    var item = dataItem;

    var listViewx = $("#PatientListView").data("kendoListView");
    selectItem = listViewx.dataSource.index();
    listView.select(listView.content.children().first());

    var dataSource = listViewx.dataSource.view();
    if (listViewx) {
        $.each(dataSource, function (index, item) {
            if (item.Code === code) {
                listViewx.select(item);
                selectedCoverageCode = item;
            }
        });
    }
    var index = this.select().index();
    dataItem = this.dataSource.view()[index];

   

}

// controller.cs
 public JsonResult PatientList_Read([DataSourceRequest] DataSourceRequest request)
        {
            var result = patientService.GetMembersListItem();     //default list       
            if (ViewData["PatientID"] != null && ViewData["MultiNames"] as string == "Individual")
            {
                string first = ViewData["FirstName"] as string;
                string last = ViewData["LastName"] as string;
                result = patientService.GetPatientSearchNames(last,first );
            }
            return new JsonResult(result.ToDataSourceResult(request));

        }

// patientPortalService.cs
 public List<ListViewModel> GetMembersListItem()
        {
            string connectionString = _configuration.GetConnectionString("ManagerToolsDBContextConnection");
            ap_GetMembershipListAll listAll = new ap_GetMembershipListAll(connectionString);
            listAll.Clinic_id = Convert.ToInt32(_session.GetString("ClinicNumber")); 
            Collection<ap_GetMembershipListAllRow> row = listAll.Execute();

            return GetPatientListViewMapping(row);
        }
        public List<ListViewModel> GetPatientListViewMapping(Collection<ap_GetMembershipListAllNameRow> ap_Get)
        {
            List<ListViewModel> lv = new List<ListViewModel>();
            foreach (ap_GetMembershipListAllNameRow item in ap_Get)
            {
                ListViewModel nlv = new ListViewModel();
                nlv.FirstName = item.FirstName.ToString();
                nlv.MiddleName = item.MiddleName.ToString();
                nlv.LastName = item.LastName.ToString();
                nlv.Patient_ID = (long)item.Patient_id;
                nlv.BeginDate = (DateTime)item.MembershipStartDate;
                lv.Add(nlv);
            }
            return lv;
        }
//ListViewModel.cs

  public class ListViewModel
    { 
        public long Patient_ID { get; set; }
        public string Greeting { get; set; }
        public string FirstName { get; set; }
        [DefaultValue(true)]
        public string MiddleName { get; set; }
        public string LastName { get; set; }
        [DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy}")]
        public DateTime BeginDate { get; set; }
    }

3 Answers, 1 is accepted

Sort by
0
Tsvetomir
Telerik team
answered on 15 Apr 2021, 04:31 PM

Hi Wade,

I have investigated the provided code snippets and since the selection mode is set to single, there is no need to introduce a loop. In order to obtain the respective data item, you could directly use the "dataItem" method:

function onChange(e){
      var selectedItem = e.sender.select();
     	var dataItem = e.sender.dataItem(selectedItem);
      console.log(dataItem);
    }

Give the suggestion above a try and let me know how it works out for you.

 

Regards,
Tsvetomir
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Wade
Top achievements
Rank 1
answered on 16 Apr 2021, 06:17 PM
thanks for the replay.  I tried your solution and all I got was the very first name in the list.  regardless of which item I selected.
0
Tsvetomir
Telerik team
answered on 20 Apr 2021, 10:12 AM

Hi Wade,

I have performed a few more tests on the solution provided with my previous response and it appears that the correct selected item is being logged in the console. Can you confirm the same based on the short video below?

http://somup.com/crfY66FIQ1

It would be very helpful if you could share a sample where the issue can be observed so that I can debug it locally and get back to you with the respective suggestions.

 

Regards,
Tsvetomir
Progress Telerik

Тhe web is about to get a bit better! 

The Progress Hack-For-Good Challenge has started. Learn how to enter and make the web a worthier place: https://progress-worthyweb.devpost.com.

Tags
ListView
Asked by
Wade
Top achievements
Rank 1
Answers by
Tsvetomir
Telerik team
Wade
Top achievements
Rank 1
Share this question
or