ListView on Change, get "primary key" for selected item

2 posts, 0 answers
  1. Jillian
    Jillian avatar
    16 posts
    Member since:
    Aug 2011

    Posted 25 Jul 2013 Link to this post

    I have a list view that has a change event.  When an item in my list view is selected how can I get the "key" that i set for the item instead of the text displayed?

    <script type="text/x-kendo-tmpl" id="ItemTemplate">
      <div style="margin:5px;">
        #:ItemNo# - #:Description#<br />
      </div>
    </script>
    <%: Html.Kendo().ListView<ItemList>()
    .Name("Items")<br>  .TagName("div"
     .DataSource(ds =>
         ds.Read(read => read.Action("ItemListRead", "Home"))
           .Model(m => m.Id(i => i.ItemKey)))
     .ClientTemplateId("ProjectItemTemplate")
     .Selectable(s => s.Mode(ListViewSelectionMode.Single))
     .Events(events => events.Change("onChange"))
    %>

    <script type="text/javascript">
       function onProjectItemChange(e) {
          var selected = $.map(this.select(), function (item) {
             return $(item).text();
          });
       }
    </script>


  2. Dimiter Madjarov
    Admin
    Dimiter Madjarov avatar
    2155 posts

    Posted 29 Jul 2013 Link to this post

    Hello Jillian,


    To achieve this you could get the uid of the selected item through the data-uid attribute and use the getByUid method of the DataSource to retrieve the model for it.
    E.g.
    function onChange(arg) {
        var selected = this.select();
        var model = this.dataSource.getByUid(selected.attr("data-uid"));
    }

    I hope that this was the information that you were looking for. I wish you a great day!

     

    Regards,
    Dimiter Madjarov
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. UI for ASP.NET MVC is VS 2017 Ready
Back to Top